Trade/Pending Order Trail error

 

As part of my trading system I have a buy stop and sell stop pending order concurrently.  I have a stop loss trail code for when either trade is activated.

In the event that the price fluctuates and activates both trades then the stop loss trailing code works.

However if only one of the pending orders is activated then my stop loss trailing code starts to trail the opposite pending order stop loss but leaves the active trades stop loss in place.

I have attached the code.

Can any one help to see what I've done wrong and how to fix it?

The orderlots function just ensures that the order has the correct lot amount relative to other trades.

//+------------------------------------------------------------------+
//| Trailing Stop Function                                           |
//+------------------------------------------------------------------+
void RegularTrail1(){
double Multiplier=(100.0-BreakenTrailAmount)/BreakenTrailAmount;
   for(int i=OrdersTotal()-1;i>=0;i--){
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
         if(OrderMagicNumber()==magic){
            if(OrderType()==OP_BUY)
               {if(OrderLots()>=((TotalOpenBuyLots()*Multiplier))/(2*Multiplier)){
               if(Bid-OrderOpenPrice() > WhenToTrail1*pips)
                  if(OrderStopLoss() < Bid-TrailAmount1*pips)
                     if(!OrderModify(OrderTicket(),OrderOpenPrice(),Bid-(TrailAmount1*pips),0,0,CLR_NONE)){
                        int err = GetLastError();
                        Print("Encountered an error during modification!"+(string)err+" "+ErrorDescription(err)  );   
                     }//ordermodify
            }}//buy order 
            else if (OrderType()==OP_SELL){
            {if(OrderLots()>=((TotalOpenSellLots()*Multiplier)/(2*Multiplier))){
               if(OrderOpenPrice()-Ask > WhenToTrail1*pips)
                  if(OrderStopLoss() > Ask+TrailAmount1*pips || OrderStopLoss()==0)
                     if(!OrderModify(OrderTicket(),OrderOpenPrice(),Ask+(TrailAmount1*pips),0,0,CLR_NONE)){
                        int err = GetLastError();
                         Print("Encountered an error during modification!"+(string)err+" "+ErrorDescription(err)  );
                     }//ordermodiy    
             }}//sell order
      }//select an order
      else{//in case it fails to select an order.
         int err = GetLastError();
         Print("Encountered an error during order selection in: "+ __FUNCTION__+"!"+(string)err+" "+ErrorDescription(err)  );
      }}
   }//for loop
}
}
 
Russfox:

As part of my trading system I have a buy stop and sell stop pending order concurrently.  I have a stop loss trail code for when either trade is activated.

In the event that the price fluctuates and activates both trades then the stop loss trailing code works.

However if only one of the pending orders is activated then my stop loss trailing code starts to trail the opposite pending order stop loss but leaves the active trades stop loss in place.

I have attached the code.

Can any one help to see what I've done wrong and how to fix it?

The orderlots function just ensures that the order has the correct lot amount relative to other trades.

void RegularTrail1(){
double Multiplier=(100.0-BreakenTrailAmount)/BreakenTrailAmount;
   for(int i=OrdersTotal()-1;i>=0;i--){
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
         if(OrderMagicNumber()==magic){
            if(OrderType()==OP_BUY)
               {if(OrderLots()>=((TotalOpenBuyLots()*Multiplier))/(2*Multiplier)){
               if(Bid-OrderOpenPrice() > (WhenToTrail1*pips)*Point)
                  if(OrderStopLoss() < Bid-(TrailAmount1*pips)*Point)
                     if(!OrderModify(OrderTicket(),OrderOpenPrice(),Bid-(TrailAmount1*pips*Point),0,0,CLR_NONE)){
                        int err = GetLastError();
                        Print("Encountered an error during modification!"+(string)err+" "+ErrorDescription(err)  );   
                     }//ordermodify
            }}//buy order 
            else if (OrderType()==OP_SELL){
            {if(OrderLots()>=((TotalOpenSellLots()*Multiplier)/(2*Multiplier))){
               if(OrderOpenPrice()-Ask > (WhenToTrail1*pips)*Point)
                  if(OrderStopLoss() > Ask+(TrailAmount1*pips)*Point || OrderStopLoss()==0)
                     if(!OrderModify(OrderTicket(),OrderOpenPrice(),Ask+(TrailAmount1*pips)*Point,0,0,CLR_NONE)){
                        int err = GetLastError();
                         Print("Encountered an error during modification!"+(string)err+" "+ErrorDescription(err)  );
                     }//ordermodiy    
             }}//sell order
      }//select an order
      else{//in case it fails to select an order.
         int err = GetLastError();
         Print("Encountered an error during order selection in: "+ __FUNCTION__+"!"+(string)err+" "+ErrorDescription(err)  );
      }}
   }//for loop
}
}
 
Thanks Mehmet, the pips function already accounts for turning the values into points.