//+------------------------------------------------------------------+
//|                                               Stoploss Index.mq4 |
//|                                              Indicator by Jas Wu |
//|                     https://www.mql5.com/en/market/product/52403 |
//+------------------------------------------------------------------+
#property description "Stoploss Index"
#property description "Indicator Made By Jas Wu"
#property copyright "Try out my Ultimate NNFX Backtester here"
#property link      "https://www.mql5.com/en/market/product/52403"
#property version   "1.00"
#property strict
#property indicator_separate_window
#property indicator_buffers 4


extern string Notes = "Try out my Ultimate NNFX Backtester here"; //-
extern string Notess = "https://www.mql5.com/en/market/product/52403"; //-
extern string Notesss = "https://discord.gg/NZ49Pwj"; //Join our discord here for more indicators
enum MMode {
Histogram = 0, //Mode Histogram
TwoLine = 1, //Mode Two Line Cross

};
extern MMode mmode = Histogram;
extern int LookBack = 10; //Candles to lookback for SL
extern int ATRP = 14; //ATR Period
extern double ATRM = 1.5; //ATR Multiplier

extern int Break = 10; //Two Line Cross Period
enum MAOption {
SMA = 0, //SMA
EMA = 1, //EMA
SMMA = 2, //SMMA
LWMA = 3, //LWMA
};
input MAOption MaMethod = EMA; //MA Matheod

extern color Bulll = clrSpringGreen; //Color for Bull
extern color Bearr = clrCrimson; //Color for Bear

double Bull[], Bear[], BullBreak[], BearBreak[]; int mamethod;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
 ChartSetInteger(0,CHART_EVENT_MOUSE_MOVE,true);
 IndicatorShortName("Stoploss Index");
 IndicatorBuffers(4);  
 
if(mmode == Histogram)
{
   SetIndexStyle(0,DRAW_HISTOGRAM, STYLE_SOLID, 4, Bulll);
   SetIndexBuffer(0,Bull);
   SetIndexLabel(0, "Bull");
   
   SetIndexStyle(1,DRAW_HISTOGRAM, STYLE_SOLID, 3, Bearr);
   SetIndexBuffer(1,Bear); 
   SetIndexLabel(1, "Bear"); 
   
   SetIndexStyle(2,DRAW_NONE, STYLE_SOLID, 1, Bulll);
   SetIndexBuffer(2,BullBreak);
   SetIndexLabel(2, "BullBreak");
   
   SetIndexStyle(3,DRAW_NONE, STYLE_SOLID, 1, Bearr);
   SetIndexBuffer(3,BearBreak); 
   SetIndexLabel(3, "BearBreak");     
}
  
if(mmode == TwoLine)
{   
   SetIndexStyle(0,DRAW_NONE, STYLE_SOLID, 4, Bulll);
   SetIndexBuffer(0,Bull);
   SetIndexLabel(0, "Bull");
   
   SetIndexStyle(1,DRAW_NONE, STYLE_SOLID, 3, Bearr);
   SetIndexBuffer(1,Bear); 
   SetIndexLabel(1, "Bear");     
   
   SetIndexStyle(2,DRAW_LINE, STYLE_SOLID, 1, Bulll);
   SetIndexBuffer(2,BullBreak);
   SetIndexLabel(2, "BullBreak");
   
   SetIndexStyle(3,DRAW_LINE, STYLE_SOLID, 1, Bearr);
   SetIndexBuffer(3,BearBreak); 
   SetIndexLabel(3, "BearBreak");  
}
   
   mamethod = MaMethod; int mamethod;   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   
 int ExtCountedBars=IndicatorCounted();
 if (ExtCountedBars<0) return(-1);
 int pos;
 int limit=Bars-ATRP-LookBack-2;
 if(ExtCountedBars>2) limit=Bars-ExtCountedBars-1;
 pos=limit;
 
      for(int t=0; t<=limit; t++)//Main Loop
      { 
         double GreenSL = 0, RedSL = 0;
         for(int p=t+1; p<=t+LookBack; p++)
         {
            if( MathAbs(Close[t] - High[p]) > (iATR(_Symbol, _Period, ATRP, t)*ATRM) ) RedSL += 1;
            if( MathAbs(Close[t] - Low[p]) > (iATR(_Symbol, _Period, ATRP, t)*ATRM) ) GreenSL += 1;
            
         }
         
         Bull[t] = GreenSL; Bear[t] = RedSL;
      }   
   
      for(int t=0; t<=limit; t++)//Main Loop
      { 
        BullBreak[t] = iMAOnArray(Bull, 0, Break, 0, mamethod, t);
        BearBreak[t] = iMAOnArray(Bear, 0, Break, 0, mamethod, t);
      
      }   
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//---
   
  }
//+------------------------------------------------------------------+
