#property copyright "DREW"
#property link      "joinme@yandex.ru"

extern int tHour = 10;
extern int TFrame = PERIOD_H1;
extern int dDay = 0;
extern int nomln = 0;


int init()
{
   return(0);
}

int deinit()
{
   return(0);
}

int start()
{
   int total,cnt;
   int quantOTL = 0;

   total = OrdersTotal();
   for(cnt=0;cnt<total;cnt++)
   {  
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()==OP_BUY) quantOTL++;
      if(OrderType()==OP_SELL) quantOTL++;
   }

   if(quantOTL == 1)
   {
      total = OrdersTotal();
      for(cnt=0;cnt<total;cnt++)
      {  
         OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);         
         if(OrderType()==OP_BUYSTOP)
         {
            OrderDelete(OrderTicket());
         }
         if(OrderType()==OP_SELLSTOP)
         {
            OrderDelete(OrderTicket());
         }
      }
   }
   
   
   if(Hour() >= 23)
   {
      total = OrdersTotal();
      for(cnt=0;cnt<total;cnt++)
      {  
         OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);         
         if(OrderType()==OP_BUYSTOP)
         {
            OrderDelete(OrderTicket());
         }
         if(OrderType()==OP_SELLSTOP)
         {
            OrderDelete(OrderTicket());
         }
      }
   }

   // дубляж из-за особенностей тестера
   if(Hour() >= 23)
   {
      total = OrdersTotal();
      for(cnt=0;cnt<total;cnt++)
      {  
         OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);         
         if(OrderType()==OP_BUYSTOP)
         {
            OrderDelete(OrderTicket());
         }
         if(OrderType()==OP_SELLSTOP)
         {
            OrderDelete(OrderTicket());
         }
      }
   }
   
   
   
   if(Hour() != tHour) return(0);
   
   double BUYlev, SELLlev;

   nomln++;

   if(dDay != Day())
   {
      // Блок I: BUYlev и SELLlev - расчетные цены отложек
      double OpenPrice = iOpen(NULL, TFrame, tHour - 9);
      double ClosePrice = iOpen(NULL, TFrame, tHour - 9);
      double CurPrice = iOpen(NULL, TFrame, tHour - 10);
      int ATR = iATR(NULL,TFrame,15,tHour-10) * 10000;
      int ATR_end = ATR;
      
      int Diff = MathAbs(OpenPrice - ClosePrice) * 10000;
      if(ATR > 16) ATR_end = ATR * 3;
            
      if(Diff > 50)
      {
         BUYlev = ATR_end * 0.0001 + ClosePrice;
         SELLlev = ClosePrice - ATR_end * 0.0001;
      }
      else
      {
         BUYlev = (OpenPrice + ClosePrice) / 2 + (ATR_end * 0.0001);
         BUYlev = MathRound(BUYlev * 10000) * 0.0001;
         SELLlev = (OpenPrice + ClosePrice) / 2 - (ATR_end * 0.0001);
         SELLlev = MathRound(SELLlev * 10000) * 0.0001;
      }

      BUYlev = BUYlev - 0.0015;
      SELLlev = SELLlev + 0.0015;
      
      
      // Блок II: расчет TP и SL
      double stepPlusMinus;
      if((ATR * 3) > 45) stepPlusMinus = 45 * 0.0001;
      else stepPlusMinus = (ATR * 3) * 0.0001;
      
      
      double slBUY = CurPrice - stepPlusMinus;
      double slSELL = CurPrice + stepPlusMinus;
      double tpBUY = CurPrice + 0.0019;
      double tpSELL = CurPrice - 0.0019;
      
      double SL, TP;
      if(CurPrice == BUYlev) SL = slBUY;
      else SL = slSELL; 
      if(CurPrice == SELLlev) TP = tpBUY;
      else TP = tpSELL; 
      
      double TPfact = MathAbs(TP - CurPrice);
      double SLfact = MathAbs(CurPrice - SL);
      
      /*ObjectCreate("LineTPBUY"+nomln,OBJ_TREND,0,iTime(NULL,PERIOD_H1,0),BUYlev+TPfact,iTime(NULL,PERIOD_H1,2),TP);
      ObjectSet("LineTPBUY"+nomln,OBJPROP_TIME1,iTime(NULL,PERIOD_H1,0));
      ObjectSet("LineTPBUY"+nomln,OBJPROP_TIME2,iTime(NULL,PERIOD_H1,2));
      ObjectSet("LineTPBUY"+nomln,OBJPROP_PRICE1,BUYlev+TPfact);
      ObjectSet("LineTPBUY"+nomln,OBJPROP_PRICE2,BUYlev+TPfact);
      ObjectSet("LineTPBUY"+nomln,OBJPROP_COLOR,Blue);
      ObjectSet("LineTPBUY"+nomln,OBJPROP_WIDTH,2);
      ObjectSet("LineTPBUY"+nomln,OBJPROP_RAY,0);
      
      ObjectCreate("LineSLBUY"+nomln,OBJ_TREND,0,iTime(NULL,PERIOD_H1,0),BUYlev-SLfact,iTime(NULL,PERIOD_H1,2),SL);
      ObjectSet("LineSLBUY"+nomln,OBJPROP_TIME1,iTime(NULL,PERIOD_H1,0));
      ObjectSet("LineSLBUY"+nomln,OBJPROP_TIME2,iTime(NULL,PERIOD_H1,2));
      ObjectSet("LineSLBUY"+nomln,OBJPROP_PRICE1,BUYlev-SLfact);
      ObjectSet("LineSLBUY"+nomln,OBJPROP_PRICE2,BUYlev-SLfact);
      ObjectSet("LineSLBUY"+nomln,OBJPROP_COLOR,Blue);
      ObjectSet("LineSLBUY"+nomln,OBJPROP_WIDTH,2);
      ObjectSet("LineSLBUY"+nomln,OBJPROP_RAY,0);

      ObjectCreate("LineTPSELL"+nomln,OBJ_TREND,0,iTime(NULL,PERIOD_H1,0),SELLlev-TPfact,iTime(NULL,PERIOD_H1,2),TP);
      ObjectSet("LineTPSELL"+nomln,OBJPROP_TIME1,iTime(NULL,PERIOD_H1,0));
      ObjectSet("LineTPSELL"+nomln,OBJPROP_TIME2,iTime(NULL,PERIOD_H1,2));
      ObjectSet("LineTPSELL"+nomln,OBJPROP_PRICE1,BUYlev-TPfact);
      ObjectSet("LineTPSELL"+nomln,OBJPROP_PRICE2,BUYlev-TPfact);
      ObjectSet("LineTPSELL"+nomln,OBJPROP_COLOR,Red);
      ObjectSet("LineTPSELL"+nomln,OBJPROP_WIDTH,2);
      ObjectSet("LineTPSELL"+nomln,OBJPROP_RAY,0);
      
      ObjectCreate("LineSLSELL"+nomln,OBJ_TREND,0,iTime(NULL,PERIOD_H1,0),SELLlev+SLfact,iTime(NULL,PERIOD_H1,2),SL);
      ObjectSet("LineSLSELL"+nomln,OBJPROP_TIME1,iTime(NULL,PERIOD_H1,0));
      ObjectSet("LineSLSELL"+nomln,OBJPROP_TIME2,iTime(NULL,PERIOD_H1,2));
      ObjectSet("LineSLSELL"+nomln,OBJPROP_PRICE1,BUYlev+SLfact);
      ObjectSet("LineSLSELL"+nomln,OBJPROP_PRICE2,BUYlev+SLfact);
      ObjectSet("LineSLSELL"+nomln,OBJPROP_COLOR,Red);
      ObjectSet("LineSLSELL"+nomln,OBJPROP_WIDTH,2);
      ObjectSet("LineSLSELL"+nomln,OBJPROP_RAY,0);*/

      ObjectCreate("LineBUY"+nomln,OBJ_TREND,0,iTime(NULL,PERIOD_H1,0),BUYlev,iTime(NULL,PERIOD_H1,5),BUYlev);
      ObjectSet("LineBUY"+nomln,OBJPROP_TIME1,iTime(NULL,PERIOD_H1,0));
      ObjectSet("LineBUY"+nomln,OBJPROP_TIME2,iTime(NULL,PERIOD_H1,5));
      ObjectSet("LineBUY"+nomln,OBJPROP_PRICE1,BUYlev);
      ObjectSet("LineBUY"+nomln,OBJPROP_PRICE2,BUYlev);
      ObjectSet("LineBUY"+nomln,OBJPROP_COLOR,Blue);
      ObjectSet("LineBUY"+nomln,OBJPROP_WIDTH,2);
      ObjectSet("LineBUY"+nomln,OBJPROP_RAY,0);
      
      ObjectCreate("LineSELL"+nomln,OBJ_TREND,0,iTime(NULL,PERIOD_H1,0),SELLlev,iTime(NULL,PERIOD_H1,5),SELLlev);
      ObjectSet("LineSELL"+nomln,OBJPROP_TIME1,iTime(NULL,PERIOD_H1,0));
      ObjectSet("LineSELL"+nomln,OBJPROP_TIME2,iTime(NULL,PERIOD_H1,5));
      ObjectSet("LineSELL"+nomln,OBJPROP_PRICE1,SELLlev);
      ObjectSet("LineSELL"+nomln,OBJPROP_PRICE2,SELLlev);
      ObjectSet("LineSELL"+nomln,OBJPROP_COLOR,Red);
      ObjectSet("LineSELL"+nomln,OBJPROP_WIDTH,2);
      ObjectSet("LineSELL"+nomln,OBJPROP_RAY,0);
      
      
      total = OrdersTotal();
      for(cnt=0;cnt<total;cnt++)
      {  
         OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);         
         if(OrderType()==OP_BUY) OrderClose(OrderTicket(), OrderLots(), Bid, 3, Yellow);   
         if(OrderType()==OP_SELL) OrderClose(OrderTicket(), OrderLots(), Ask, 3, Yellow);
      }


      // открытие позиций
      if(OrdersTotal() == 0)
      {
         //OrderSend(Symbol(),OP_BUYSTOP,1,BUYlev,3,BUYlev-0.0025,BUYlev+0.0010,"",0,0,Blue);
         //OrderSend(Symbol(),OP_SELLSTOP,1,SELLlev,3,SELLlev+0.0025,SELLlev-0.0010,"",0,0,Red); 
         OrderSend(Symbol(),OP_BUYSTOP,1,BUYlev,3,0,BUYlev+0.0024,"",0,0,Blue);
         OrderSend(Symbol(),OP_SELLSTOP,1,SELLlev,3,0,SELLlev-0.0024,"",0,0,Red); 
      }
   
      dDay = Day();
   }
   
   return(0);
}


