//+------------------------------------------------------------------+
//|                                Expert for Fractals5+Diapazon.mq4 |
//+------------------------------------------------------------------+
#property copyright "© Maloma"

extern double Lots      = 0.1;
extern int    SL        = 15;
extern int    TP        = 30;
extern double SARPeriod = 0.01;
       
       double SigUP=0, SigDN=0;
       double ParStat=0;
       int    CBars=0;
       int    magic=579345;
       double SAR;

void GetFractalSignal()
{
  
 if(High[3]>High[4] && High[3]>High[5] && High[3]>High[2] && High[3]>High[1])
  {SigUP=1;}
 else
  {SigUP=0;}
 
 if(Low[3]<Low[4] && Low[3]<Low[5] && Low[3]<Low[2] && Low[3]<Low[1])
  {SigDN=1;}
 else
  {SigDN=0;}
 
 return(0);
}

void Check4Close()
{
 int j=OrdersTotal();
 for (int i=j;i>=0;i--)
  {
   OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
   if (Symbol()==OrderSymbol() && magic==OrderMagicNumber())
    if (OrderType()==OP_BUY && SigUP==1)
     OrderClose(OrderTicket(),OrderLots(),Bid,3,CLR_NONE);
    if (OrderType()==OP_SELL && SigDN==1)
     OrderClose(OrderTicket(),OrderLots(),Ask,3,CLR_NONE);
  }
 return(0);
}

void start()


{
// if (Period()!=15) {Comment("Работаем только на М15");return(1);}
 if (Bars==CBars) {return(2);}
 SAR=iSAR(Symbol(),0,SARPeriod,0.2,1);
 GetFractalSignal();
 Check4Close();
 if (SigUP==1 && SAR<Open[0]) OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-SL*Point,Ask+TP*Point,"e-F5S",magic,0,Green);
 if (SigDN==1 && SAR>Open[0]) OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+SL*Point,Bid-TP*Point,"e-F5S",magic,0,Red);
 CBars=Bars;
 return(0);
}