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

extern int dDay;
//extern bool fl;

int init()
  {
      return(0);
  }

int deinit()
  {
      return(0);
  }

int start()
  {
      double MA2,MA3;
      double Fr,Fr1;
      double SAR;
      double prof;
      int total,cnt;
      
      MA2 = iMA(NULL,0,2,0,MODE_SMA,PRICE_CLOSE,0);
      MA3 = iMA(NULL,0,5,0,MODE_SMA,PRICE_CLOSE,0);
      Fr = iForce(NULL,0,2,MODE_SMA,PRICE_CLOSE,0);
      Fr1 = iForce(NULL,0,2,MODE_SMA,PRICE_CLOSE,1);
      
      //SAR = iSAR(NULL,0,0.2,2,0);
      
      if((Hour() == 22) && (OrdersTotal() == 0) && (dDay != Day()))
      {
         //if(Open[0] > MA) // BUY
         if(MA3 < MA2)
         //if(SAR < Open[0])
         {
            OrderSend(Symbol(),OP_BUY,1,Ask,3,0,Ask+0.0010,"",2,0,Blue);
            //fl = true;
         }
         //if(Open[0] < MA) // SELL
         if(MA3 > MA2)
         //if(SAR > Open[0])
         {
            OrderSend(Symbol(),OP_SELL,1,Bid,3,0,Bid-0.0010,"",1,0,Red);
            //fl = true;
         }
      
         dDay = Day();
      }
      
      /*if((Hour() == 8) && (OrdersTotal() == 1) && fl)
      {
         fl = false;
         if(OrdersTotal() != 0)
         {
            total = OrdersTotal();
            for(cnt=0;cnt<total;cnt++)
            {  
               OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);         
               prof = OrderProfit() / 10;
               if(prof <= -10)
               {
                  if(OrderType()==OP_BUY)
                  {
                     OrderClose(OrderTicket(), OrderLots(), Bid, 3, Yellow);   
                     OrderSend(Symbol(),OP_SELL,1,Bid,3,0,Bid-0.0010,"",1,0,Red);
                  }
                  if(OrderType()==OP_SELL)
                  {
                     OrderClose(OrderTicket(), OrderLots(), Ask, 3, Red);
                     OrderSend(Symbol(),OP_BUY,1,Ask,3,0,Ask+0.0010,"",2,0,Blue);
                  }
               }
            }
         }
      }*/
  
      
      if(OrdersTotal() != 0)
      {
         total = OrdersTotal();
         for(cnt=0;cnt<total;cnt++)
         {  
            OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);                     
            prof = OrderProfit() / 10;
            if(prof >= 4)
            {
               if(OrderType()==OP_BUY)
               {
                  OrderClose(OrderTicket(), OrderLots(), Bid, 3, Yellow);   
               }
               if(OrderType()==OP_SELL)
               {
                  OrderClose(OrderTicket(), OrderLots(), Ask, 3, Red);
               }
            }
         }
      }

      return(0);
  }

