//+------------------------------------------------------------------+
//|                                                   Донджиан +.mq4 |
//|                                                         Samuliak |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Samuliak"
#property link      ""

extern double Lot        = 0.1;
extern int    Indent     = 5;
extern string QQQ        = "Настройки канала Донжиана";
extern int HISTORY_DEPTH = 24;


double Tp, Sl, UP_CANAL, DOWN_CANAL;
double timeprev;
double openBuyStop, openSellStop;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
     if( Digits == 3 || Digits == 5)
  {
   Indent       *= 10;
  }  



   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {



   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
  if(timeprev == Time[0]) return(0);
  timeprev = Time[0];
 
  
  UP_CANAL = iCustom(Symbol(), 0,"azzx_donchian_1_0", HISTORY_DEPTH, 0, 0);
  DOWN_CANAL = iCustom(Symbol(), 0,"azzx_donchian_1_0", HISTORY_DEPTH, 1, 0);  
  
  Tp = NormalizeDouble(UP_CANAL - DOWN_CANAL, Digits);
  Sl = NormalizeDouble(UP_CANAL - DOWN_CANAL, Digits);
  
bool existsBS = false;
bool existsSS = false;
int i, ordtot = OrdersTotal();

openBuyStop = NormalizeDouble(UP_CANAL + Indent*Point, Digits);
openSellStop = NormalizeDouble(DOWN_CANAL - Indent*Point, Digits);

for(i=0; i<ordtot; i++){
  if(OrderSelect(i,SELECT_BY_POS)){
  
    if(OrderType() == OP_BUYSTOP){
      existsBS = true;                                               // отмечаем, что Бай Стоп имеется
      if(MathAbs(OrderOpenPrice() - UP_CANAL) > 0.0001*Point){      // если БC не на границе
        OrderModify(OrderTicket(), openBuyStop, 0, Tp, 0);         // поправляем
      }
    }

    if(OrderType() == OP_SELLSTOP){
      existsSS = true;                                                 // отмечаем, что SELL Стоп имеется
      if(MathAbs(OrderOpenPrice() - DOWN_CANAL) > 0.0001*Point){      // если CC не на границе
        OrderModify(OrderTicket(), openSellStop , 0, Tp, 0);         // поправляем
      }
    }
  }
}

if(existsBS == false)
  {
     OrderSend(Symbol(), OP_BUYSTOP, Lot, openBuyStop, 0, 0, Tp);
  }

if(existsBS == false)
  {
     OrderSend(Symbol(), OP_SELLSTOP, Lot, openSellStop, 0, 0, Tp);
  }
  

  
  
   return(0);
  }
//+------------------------------------------------------------------+