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

extern int dDay = 0;

int init()
{
   return(0);
}

int deinit()
{
   return(0);
}

int start()
{
   //if(dDay == Day()) return(0);
   //if(Hour() < 9) return(0);
   //if(Hour() > 20) return(0);

   int total,cnt;   
   
   double RegChan = iCustom(NULL, PERIOD_H1, "[drw-i] Regression chanel in separate window", 0, 0);
   
   total = OrdersTotal();
   for(cnt=0;cnt<total;cnt++)
   {  
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);         
      if((OrderType()==OP_BUY) && (RegChan > -15))
      {
         OrderClose(OrderTicket(), OrderLots(), Ask, 3, Red);
      }
      if((OrderType()==OP_SELL) && (RegChan < 15))
      {
         OrderClose(OrderTicket(), OrderLots(), Bid, 3, Yellow);
      }
   }

   if(RegChan < -15)
   {
      total = OrdersTotal();
      for(cnt=0;cnt<total;cnt++)
      {  
         OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);         
         if(OrderType()==OP_SELL)
         {
            //OrderClose(OrderTicket(), OrderLots(), Bid, 3, Yellow);
         }
      }

      //if(dDay != Day()) 
      if(OrdersTotal() == 0) OrderSend(Symbol(),OP_BUY,1,Ask,3,0,Ask+0.0050,"",2,0,Blue);
      //dDay = Day();
   }

   if(RegChan > 15)
   {
      total = OrdersTotal();
      for(cnt=0;cnt<total;cnt++)
      {  
         OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);         
         if(OrderType()==OP_BUY)
         {
            //OrderClose(OrderTicket(), OrderLots(), Ask, 3, Red);
         }
      }

      //if(dDay != Day()) 
      if(OrdersTotal() == 0) OrderSend(Symbol(),OP_SELL,1,Bid,3,0,Bid-0.0050,"",1,0,Red);
      //dDay = Day();
   }

   return(0);
}



