Problem in trailing Stop function

 

can anyone tell me what is wrong in my trailing function it works with Buy only !!!!!


//+------------------------------------------------------------------+
//| (My)Trailing Stop                                                         |
//+------------------------------------------------------------------+

void TrailingStop()
  {
   for(int i=0; i<=OrdersTotal(); i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false){GetLastError();}
        {
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==magic_no)
           {
            if(OrderType()==OP_BUY)
              {
               if((iClose(Symbol(),0,0)>=(OrderOpenPrice()+TrailingStop*Point*p2p) && (OrderStopLoss()<=OrderOpenPrice() || OrderStopLoss()==0)))
                 {
                  if(OrderModify(OrderTicket(),OrderOpenPrice(),(iClose(Symbol(),0,0)-(TrailingStop-Profit_lock)*Point*p2p),OrderTakeProfit(),0,Red)==false)GetLastError();
                 }
               else if((iClose(Symbol(),0,0)>=(OrderStopLoss()+TrailingStop*Point*p2p) && (OrderStopLoss()>=OrderOpenPrice())))
                 {
                  if(OrderModify(OrderTicket(),OrderOpenPrice(),(iClose(Symbol(),0,0)-TrailingStop*Point*p2p),OrderTakeProfit(),0,Red)==false)GetLastError();
                 }
              }
            if(OrderType()==OP_SELL)
              {
               if((iClose(Symbol(),0,0)<=(OrderOpenPrice()-TrailingStop*Point*p2p) && (OrderStopLoss()>=OrderOpenPrice() || OrderStopLoss()==0)))
                 {
                  if(OrderModify(OrderTicket(),OrderOpenPrice(),(iClose(Symbol(),0,0)+(TrailingStop-Profit_lock)*Point*p2p),OrderTakeProfit(),0,Red)==false)GetLastError();
                 }
               else if((iClose(Symbol(),0,0)<=(OrderStopLoss()-TrailingStop*Point*p2p) && (OrderStopLoss()<=OrderOpenPrice())))
                 {
                  if(OrderModify(OrderTicket(),OrderOpenPrice(),(iClose(Symbol(),0,0))+((TrailingStop)*Point*p2p),OrderTakeProfit(),0,Red)==false)GetLastError();
                 }
              }
           }
        }
     }
  }
 

You can try this :

//+------------------------------------------------------------------+
//| (My)Trailing Stop                                                |
//+------------------------------------------------------------------+

void TrailingStop()
 {
   for(int i=0; i<=OrdersTotal(); i++)
      {
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false){GetLastError();}
            {
               if(OrderSymbol()==Symbol() && OrderMagicNumber()==magic_no)
                  {
                     if(OrderType()==OP_BUY)
                        {
                           if((OrderClosePrice()>=(OrderOpenPrice()+TrailingStop*Point*p2p) && (OrderStopLoss()<=OrderOpenPrice() || OrderStopLoss()==0)))
                              {
                                 if(OrderModify(OrderTicket(),OrderOpenPrice(),(OrderClosePrice()-(TrailingStop-Profit_lock)*Point*p2p),OrderTakeProfit(),0,Red)==false)GetLastError();
                              }
                           else if((OrderClosePrice()>=(OrderStopLoss()+TrailingStop*Point*p2p) && (OrderStopLoss()>=OrderOpenPrice())))
                              {
                                 if(OrderModify(OrderTicket(),OrderOpenPrice(),(OrderClosePrice()-TrailingStop*Point*p2p),OrderTakeProfit(),0,Red)==false)GetLastError();
                              }
                        }
                     if(OrderType()==OP_SELL)
                        {
                           if((OrderClosePrice()<=(OrderOpenPrice()-TrailingStop*Point*p2p) && (OrderStopLoss()>=OrderOpenPrice() || OrderStopLoss()==0)))
                              {
                                 if(OrderModify(OrderTicket(),OrderOpenPrice(),(OrderClosePrice()+(TrailingStop-Profit_lock)*Point*p2p),OrderTakeProfit(),0,Red)==false)GetLastError();
                              }
                           else if((OrderClosePrice()<=(OrderStopLoss()-TrailingStop*Point*p2p) && (OrderStopLoss()<=OrderOpenPrice()) && OrderStopLoss() > 0 ))
                              {
                                 if(OrderModify(OrderTicket(),OrderOpenPrice(),(OrderClosePrice()+TrailingStop*Point*p2p),OrderTakeProfit(),0,Red)==false)GetLastError();
                              }
                        }
                  }
            }
      }
 }
Reason: