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

extern string Type = "EMPTY";
int TFrame = PERIOD_M1;
int TFrame2 = PERIOD_M5;

int init()
  {
      return(0);
  }
//-------------------------------------------------------------------------------
int deinit()
  {
      return(0);
  }
//-------------------------------------------------------------------------------
int start()
  {
      // ставим отложку ± 50 (otlStep) и ставим стоп у основной позиции на ± 70 (mainSLoss)
      // таким образом у отложки ставим т.профит на 100 (otlTProf)
  
  
      int Al;
      int total,cnt;
      double MA2,MA1;
      double K;
      
      double otlStep = 0.0040;
      double mainSLoss = 0.0040;
      double otlTProf = 0.0060;
      
      
      
      MA1 = iMA(NULL,TFrame2,13,0,MODE_SMA,PRICE_CLOSE,1);
      MA2 = iMA(NULL,TFrame2,13,0,MODE_SMA,PRICE_CLOSE,0);
      
      K = (MA2 - MA1)/0.05;
      
      Al = Alligator();      
           
     
      if(OrdersTotal() == 0)
      {
         if(Al == 1)
         {  
            if(Type != "BUY")
            {
               if((Hour() >= 9) && (Hour() <= 20)) 
                  if(MathAbs(K) <= 0.01)
                  {
                     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);   
                        }
                     }

                     OrderSend(Symbol(),OP_SELL,1,Bid,3,0,Bid-0.0013,"",1,0,Red);
                     //OrderSend(Symbol(),OP_BUYSTOP,1,Bid+otlStep,3,0,Bid+otlStep+otlTProf,"",0,0,Aqua);
                  }
               if((Hour() >= 9) && (Hour() <= 20))
                  if(MathAbs(K) > 0.01)
                  {
                     total = OrdersTotal();
                     for(cnt=0;cnt<total;cnt++)
                     {  
                        OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);         
                        if(OrderType()==OP_SELL)
                        {
                           OrderClose(OrderTicket(), OrderLots(), Ask, 3, Red);
                        }
                     }

                     OrderSend(Symbol(),OP_BUY,1,Ask,3,0,Ask+0.0013,"",2,0,Blue);
                     //OrderSend(Symbol(),OP_SELLSTOP,1,Ask-otlStep,3,0,Ask-otlStep-otlTProf,"",0,0,Orange); 
                  }
               Type = "BUY";
               //Print("K: ", MathAbs(K));
               //Print("Time", TimeToStr(Time[0]));
               //Print("-------------------");
            }
         }
      }
      

      if(OrdersTotal() == 0)
      {
         if(Al == 2)
         {
            if(Type != "SELL")
            {               
               if((Hour() >= 9) && (Hour() <= 20))
                  if(MathAbs(K) <= 0.01)
                  {
                     total = OrdersTotal();
                     for(cnt=0;cnt<total;cnt++)
                     {  
                        OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);         
                        if(OrderType()==OP_SELL)
                        {
                           OrderClose(OrderTicket(), OrderLots(), Ask, 3, Red);
                        }
                     }

                     OrderSend(Symbol(),OP_BUY,1,Ask,3,0,Ask+0.0013,"",2,0,Blue);
                     //OrderSend(Symbol(),OP_SELLSTOP,1,Ask-otlStep,3,0,Ask-otlStep-otlTProf,"",0,0,Orange); 
                  }
               if((Hour() >= 9) && (Hour() <= 20))
                  if(MathAbs(K) > 0.01)
                  {
                     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);   
                        }
                     }

                     OrderSend(Symbol(),OP_SELL,1,Bid,3,0,Bid-0.0013,"",1,0,Red);
                     //OrderSend(Symbol(),OP_BUYSTOP,1,Bid+otlStep,3,0,Bid+otlStep+otlTProf,"",0,0,Aqua);
                  }
               Type = "SELL";
               //Print("K: ", MathAbs(K));
               //Print("Time", TimeToStr(Time[0]));
               //Print("-------------------");
            }
         }
      }
      
      /*if(OrdersTotal() != 0)
      {
         //if(Al == 3)
         //{  
            total = OrdersTotal();
            for(cnt=0;cnt<total;cnt++)
            {  
               OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);         
               if(OrderType()==OP_BUY)
               {
                  if((OrderProfit() / 10) >= 13)
                  {
                     OrderClose(OrderTicket(), OrderLots(), Bid, 3, Yellow);   
                  }
               }
               if(OrderType()==OP_SELL)
               {
                  if((OrderProfit() / 10) >= 13)
                  {
                     OrderClose(OrderTicket(), OrderLots(), Ask, 3, Red);
                  }
               }
            }
         //}
      }*/


      /*if(OrdersTotal() == 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(OrdersTotal() != 0)
      {
         OrderSelect(0,SELECT_BY_POS,MODE_TRADES);
         //if(Tralingstop()) return(0);         
      }*/

      return(0);
  }
//-------------------------------------------------------------------------------
// 1 - BUY
// 2 - SELL
// 3 - FLAT
int Alligator()
  {
      double Jaw,Teeth,Lips;
      
      Jaw = iAlligator(NULL,TFrame,13,13,8,8,5,5,MODE_SMMA,PRICE_MEDIAN,MODE_GATORJAW,1);
      Teeth = iAlligator(NULL,TFrame,13,13,8,8,5,5,MODE_SMMA,PRICE_MEDIAN,MODE_GATORTEETH,1);
      Lips = iAlligator(NULL,TFrame,13,13,8,8,5,5,MODE_SMMA,PRICE_MEDIAN,MODE_GATORLIPS,1);
      
      if((Jaw < Teeth) && (Teeth < Lips))
      {
         return(1);      
      }
      else
      if((Jaw > Teeth) && (Teeth > Lips))
      {
         return(2);
      }
      else return(3);
  }
//-------------------------------------------------------------------------------
/*bool Tralingstop()//необходимо чтобы ордер был выбран
   {
      int TralingStop = 10;
      if(OrderType()==OP_BUY)
      {
         if(Open[0]-TralingStop*Point>OrderStopLoss()){OrderModify(OrderTicket(),OrderOpenPrice(),Open[0]-TralingStop*Point-0.0010,Open[0]+TralingStop*Point,0);return(true);}
      }
      if(OrderType()==OP_SELL)
         if(Open[0]+TralingStop*Point<OrderStopLoss()){OrderModify(OrderTicket(),OrderOpenPrice(),Open[0]+TralingStop*Point+0.0010,Open[0]-TralingStop*Point,0);return(true);}
      return(false);
   }*/
  