#property copyright "Drew Nya"
#property link      "joinme@yandex.ru"

extern int dDay;
extern int       EMA1=3;
extern int       Shift1=0;
extern int       EMA2=5;
extern int       Shift2=0;
extern int       EMA3=8;
extern int       Shift3=0;

int init()
  {
      return(0);
  }

int deinit()
  {
      return(0);
  }

int start()
  {
      int total,cnt;
      double prof;
      
      if((OrdersTotal() == 0) && (dDay != Day()))
      {
         if((Close[0] + 0.0005) >= High[1])
         {
            if(!flat()) OrderSend(Symbol(),OP_BUY,1,Ask,3,0,Ask+0.0010,"",2,0,Blue);
            if(flat()) OrderSend(Symbol(),OP_SELL,1,Bid,3,0,Bid-0.0010,"",1,0,Red);
            dDay = Day();
         }
         if((Close[0] - 0.0005) <= Low[1]) 
         {
            if(flat()) OrderSend(Symbol(),OP_BUY,1,Ask,3,0,Ask+0.0010,"",2,0,Blue);
            if(!flat()) OrderSend(Symbol(),OP_SELL,1,Bid,3,0,Bid-0.0010,"",1,0,Red);
            dDay = Day();
         }         
      }

      /*if((OrdersTotal() > 0) && (dDay != Day()))
      {
         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, Red);
            }
         }
      }*/

      /*total = OrdersTotal();
      for(cnt=0;cnt<total;cnt++)
      {  
         OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);         
         prof = OrderProfit() / 10;
         if(prof >= 6)
         {
            if(OrderType()==OP_BUY)
            {
               OrderClose(OrderTicket(), OrderLots(), Bid, 3, Yellow);   
            }
            if(OrderType()==OP_SELL)
            {
               OrderClose(OrderTicket(), OrderLots(), Ask, 3, Red);
            }
         }
      }*/

      return(0);
  }

bool flat()
{
   double e1=iMA(Symbol(),0,EMA1,Shift1,MODE_EMA,PRICE_MEDIAN,1);
   double e2=iMA(Symbol(),0,EMA2,Shift3,MODE_EMA,PRICE_MEDIAN,1);
   double e3=iMA(Symbol(),0,EMA3,Shift3,MODE_EMA,PRICE_MEDIAN,1);
   if(e1>e2 && e2>e3) return(false);
   if(e1<e2 && e2<e3) return(false);
   return(true);
}

