extern double Lot = 0.1; // размер лота
extern int StartBar = 0; // начальный бар, с которого идет отсчет двух одинаковых палок
extern int TakeProfit = 20;
extern int StopLoss = 10;
extern int Trailing = 10; // Величина трейлинга
extern int MaxOrders = 1; // максимальное количество одновремено открытых ордеров
datetime last;
int start()
  {
   double ao1,ao2,ao3;
if(OrdersTotal()>0) TrailingControl();
if((Time[0]-last)>=Period()*60)
{
   ao1=iAO(Symbol(),0,StartBar);
   ao2=iAO(Symbol(),0,StartBar+1);
   ao3=iAO(Symbol(),0,StartBar+2);
   if(ao1<ao2 && ao2<ao3) OrderOp(OP_SELL);
   if(ao2>ao3 && ao1>ao2) OrderOp(OP_BUY);
   last=Time[0];
}
   return(0);
  }
int OrderOp(int ord)
{
   double TP,SL,ticket;
if(OrdersTotal()<MaxOrders)
{
   if(ord==OP_BUY)
   {
      TP=Bid+TakeProfit*Point; if(TakeProfit==0) TP=0;
      SL=Ask-StopLoss*Point;   if(StopLoss==0) SL=0;
      ticket=OrderSend(Symbol(),OP_BUY,Lot,Ask,3,SL,TP,"Покупаем",16384,0,White);
   }
   if(ord==OP_SELL)
   {
      TP=Ask-TakeProfit*Point; if(TakeProfit==0) TP=0;
      SL=Bid+StopLoss*Point;   if(StopLoss==0) SL=0;
      ticket=OrderSend(Symbol(),OP_SELL,Lot,Bid,3,SL,TP,"Продаем",16384,0,Yellow);
   }
}
}

int OrderCl(int ord)
{
   int cnt;
   for(cnt=OrdersTotal();cnt>0;cnt--)
   {
      OrderSelect(cnt-1, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()==OP_BUY  && ord==OP_BUY)  OrderClose(OrderTicket(),OrderLots(),Bid,3,White);
      if(OrderType()==OP_SELL && ord==OP_SELL) OrderClose(OrderTicket(),OrderLots(),Ask,3,Yellow);
   }
}

int TrailingControl()
{
   int total=OrdersTotal(),cnt;
   
   for(cnt=0;cnt<total;cnt++)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL && OrderSymbol()==Symbol())
        {
         if(OrderType()==OP_BUY)
           {
            if(Trailing>0)  
              {                 
               if(Bid-OrderOpenPrice()>Point*Trailing)
                 {
                  if(OrderStopLoss()<Bid-Point*Trailing)
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*Trailing,OrderTakeProfit(),0);
                     return(0);
                    }
                 }
              }
           }
         else
           {
            if(Trailing>0)  
              {                 
               if((OrderOpenPrice()-Ask)>(Point*Trailing))
                 {
                  if(OrderStopLoss()>(Ask+Point*Trailing))
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*Trailing,OrderTakeProfit(),0);
                     return(0);
                    }
                 }
              }
           }
        }
     }


}