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

int TF = PERIOD_H4;
int prevTH = 0;

int init()
{
   return(0);
}

int deinit()
{
   return(0);
}

int start()
{
   int qBUY = 0;
   int qSELL = 0;
   int total,cnt;   
   total = OrdersTotal();
   for(cnt=0;cnt<total;cnt++)
   {  
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);         
      if(OrderType()==OP_BUY)
      {
         qBUY++;
      }
      if(OrderType()==OP_SELL)
      {
         qSELL++;
      }
   }
   
   if((qBUY + qSELL) == 0)
   {
      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());
         }
      }
   }
   

   int TH = TimeHour(iTime(NULL,TF,0));
   if((TH <= 8) || (TH >= 19)) return;
   if(prevTH == TH) return;
   prevTH = TH;

   
   double x = 0.0;
   double h = 0.0;
   double l = 0.0;
   double o = 0.0;
   double c = 0.0;

   h = iHigh(NULL,TF,0);
   l = iLow(NULL,TF,0);
   o = iOpen(NULL,TF,0);
   c = iClose(NULL,TF,0);

   if(c < o) x = (h + l + c + l) / 2;
   else if(c > o) x = (h + l + c + h) / 2;
   else if(c == o) x = (h + l) / 2;

   
   double middleCur = ((o + c) / 2);
   double middleR = (((x - l) + (x - h)) / 2);
   
   if(middleCur < middleR)
   {
      if(OrdersTotal() == 0)
      {
         OrderSend(Symbol(),OP_BUY,1,Ask,3,0,Ask+0.0010,"",11,0,Blue);
         OrderSend(Symbol(),OP_SELLSTOP,1,Ask-0.0040,3,0,Ask-0.0040-0.0010,"",0,0,Orange); 
      }
   }
   if(middleCur > middleR)
   {
      if(OrdersTotal() == 0)
      {
         OrderSend(Symbol(),OP_SELL,1,Bid,3,0,Bid-0.0010,"",11,0,Red);
         OrderSend(Symbol(),OP_BUYSTOP,1,Bid+0.0040,3,0,Bid+0.0040+0.0010,"",0,0,Aqua);
      }
   }

   return(0);
}



