The difference between Trailing stop for pending orders & positions

 

Hi all 

Below the code I have attached a trailing stop code that only trails when its in profit only. For positions trades it trails ALL the Time which works perfectly fine, as for pending orders not all the time. Sometimes it will trail and sometimes it won't. I have tried using get last error code and I get nothing. Please help if you know why I encounter this problem for pending orders ONLY? 

void Trailing() 
 {
  double AskLevel=Ask+StopLevel*Pips();
  double BidLevel=Bid-StopLevel*Pips();
  
  double SLBuy=NormalizeDouble(BidLevel-TrailStop*Pips(),Digits);
  
  double SLSell=NormalizeDouble(BidLevel+TrailStop*Pips(),Digits);
  
  for(int j=OrdersTotal()-1;j>=0;j--)
  {
   if(OrderSelect(j,SELECT_BY_POS,MODE_TRADES))
   if(OrderSymbol()==Symbol())
   if(OrderMagicNumber()==MagicNumber)
   if(OrderType()==OP_BUY)
   
   if(Bid-SLBuy>StopLevel*Pips())
   if(SLBuy>OrderOpenPrice()&&(OrderStopLoss()==0||SLBuy>OrderStopLoss()))
   {
    bool ModBuyTrail=OrderModify(OrderTicket(),OrderOpenPrice(),SLBuy,OrderTakeProfit(),0,clrNONE);
    
   } 
  }
   
  for(int k=OrdersTotal()-1;k>=0;k--)
  {
   if(OrderSelect(k,SELECT_BY_POS,MODE_TRADES))
   if(OrderSymbol()==Symbol())
   if(OrderMagicNumber()==MagicNumber)
   if(OrderType()==OP_SELL)
   
   if(SLSell-Ask>StopLevel*Pips())
   if(SLSell<OrderOpenPrice()&&(OrderStopLoss()==0||SLSell<OrderStopLoss()))
   {
    bool ModSellTrail=OrderModify(OrderTicket(),OrderOpenPrice(),SLSell,OrderTakeProfit(),0,clrNONE);
    
   } 
  }  
 }
 
  1.    if(OrderType()==OP_BUY)
    

    A pending order is not an OP_BUY. That is an open order.

  2. There is no need to create pending orders in code.

    1. The pending has the slight advantage, A) you are closer to the top of the queue (filled quicker), B) there's no round trip network delay (filled quicker.)

      Don't worry about it unless you're scalping M1 or trading news.

    2. Humans can't watch the screen 24/7, so they use pending orders; EAs can, so no need for pending orders, have it wait until the market reaches the trigger price and just open an order.

 
William Roeder #:
  1. A pending order is not an OP_BUY. That is an open order.

  2. There is no need to create pending orders in code.

    1. The pending has the slight advantage, A) you are closer to the top of the queue (filled quicker), B) there's no round trip network delay (filled quicker.)

      Don't worry about it unless you're scalping M1 or trading news.

    2. Humans can't watch the screen 24/7, so they use pending orders; EAs can, so no need for pending orders, have it wait until the market reaches the trigger price and just open an order.

1. I'm fully aware of that, I want my trail stop to trail in profit as soon as price hits my stops (buy stop & sell stop), all the time not SOMETIMES.

2. I want to use my pending orders for a strategy that I have and it requires pending orders.