Trailing stop ONLY on the remaining open positions.

 
//+------------------------------------------------------+  
//Moving Average Trailing Stop Function                  |
//+------------------------------------------------------+   
void MA_Trail()

{

   double ATR = iATR(NULL,60,14,1);
   double MA = iMA(NULL,60,MA_Period,0,1,0,1);
   
   double BuyStopPriceMath = MA - ATR;
   double SellStopPriceMath = MA + ATR;
   
   double BuyStopPrice = NormalizeDouble(BuyStopPriceMath,5);
   double SellStopPrice = NormalizeDouble(SellStopPriceMath,5);

   for(int b=OrdersTotal()-1; b>=0; b--)
     {
      if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
          if( OrderMagicNumber() == MagicNumber1 || OrderMagicNumber()== MagicNumber2 || OrderMagicNumber()== MagicNumber3 )
            if(OrderSymbol() == Symbol())
               {
                  if( OrderMagicNumber() == MagicNumber1 ){
                    Selected_Trade_0 = OrderTicket(); }
                  if( OrderMagicNumber() == MagicNumber2 ){
                    Selected_Trade_1 = OrderTicket(); }
                  if( OrderMagicNumber() == MagicNumber3 ){
                    Selected_Trade_2 = OrderTicket();} 
               
                 
               if( OrderType()==OP_BUY && OrderStopLoss() - BuyStopPrice > Point / 2. )break;
                 if( OrderType() == OP_BUY && BuyStopPrice - OrderStopLoss() > Point / 2.) 
                  { 
                    if( OpenOrdersThisPair(Symbol()) == 3 ){ 
                     bool BuyModify1 = OrderModify(Selected_Trade_0,OrderOpenPrice(),BuyStopPrice,btp1,0,CLR_NONE);
                     bool BuyModify2 = OrderModify(Selected_Trade_1,OrderOpenPrice(),BuyStopPrice,btp2,0,CLR_NONE);
                     bool BuyModify3 = OrderModify(Selected_Trade_2,OrderOpenPrice(),BuyStopPrice,btp3,0,CLR_NONE);
                     }
                     
                    if( OpenOrdersThisPair(Symbol()) == 2 ){ 
                     bool BuyModify2 = OrderModify(Selected_Trade_1,OrderOpenPrice(),BuyStopPrice,btp2,0,CLR_NONE);
                     bool BuyModify3 = OrderModify(Selected_Trade_2,OrderOpenPrice(),BuyStopPrice,btp3,0,CLR_NONE);
                     }
                     
                    if( OpenOrdersThisPair(Symbol()) == 1 ){ 
                     bool BuyModify3 = OrderModify(Selected_Trade_2,OrderOpenPrice(),BuyStopPrice,btp3,0,CLR_NONE);
                     }  
                  }

...
Now, this works, but I feel it's inefficient how I've written it. Any ideas how I can just work on the open trades remaining within this for loop and trail the stop accordingly? I apologize if I am asking a stupid question as I have it working now, but I'm convinced I have written this poorly despite it working...

Cheers.
 
Not sure if this is 100% correct after-all. Can anyone tell me where I am going wrong. I am getting errors? Bascially, once first trade has hit its first target, I want the other 2 to move to break even. What this is doing though is also removing the take profit target too :s?
 

Not checked or tested or even compiled, but this makes more sense to me

   for(int b=OrdersTotal()-1; b>=0; b--)
     {
      if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
          if( OrderMagicNumber() == MagicNumber1 || OrderMagicNumber()== MagicNumber2 || OrderMagicNumber()== MagicNumber3 )
            {
            if(OrderSymbol() != Symbol())
               continue;
            if( OrderType()==OP_BUY && OrderStopLoss() - BuyStopPrice > Point / 2. )break;
            if( OrderType() == OP_BUY && BuyStopPrice - OrderStopLoss() > Point / 2.) 
               {
               if( OrderMagicNumber() == MagicNumber1 )
                  bool BuyModify1 = OrderModify(OrderTicket(),OrderOpenPrice(),BuyStopPrice,btp1,0,CLR_NONE);
               if( OrderMagicNumber() == MagicNumber2 )
                  bool BuyModify2 = OrderModify(OrderTicket(),OrderOpenPrice(),BuyStopPrice,btp2,0,CLR_NONE);
               if( OrderMagicNumber() == MagicNumber3 )
                  bool BuyModify3 = OrderModify(OrderTicket(),OrderOpenPrice(),BuyStopPrice,btp3,0,CLR_NONE);
               }
             }  
     }
 
DomGilberto:
Not sure if this is 100% correct after-all. Can anyone tell me where I am going wrong. I am getting errors? Bascially, once first trade has hit its first target, I want the other 2 to move to break even. What this is doing though is also removing the take profit target too :s?
Maybe change btp1 etc to OrderTakeProfit()
 
//+----------------------------------------------------------------------------------------------------------------------------------------+  
//Moving Average Trailing Stop Function
//+----------------------------------------------------------------------------------------------------------------------------------------+   
void MA_Trail()

{

   double ATR = iATR(NULL,60,14,1);
   double MA = iMA(NULL,60,MA_Period,0,1,0,1);
   
   double BuyStopPriceMath = MA - ATR;
   double SellStopPriceMath = MA + ATR;
   
   double BuyStopPrice = NormalizeDouble(BuyStopPriceMath,5);
   double SellStopPrice = NormalizeDouble(SellStopPriceMath,5);

   for(int b=OrdersTotal()-1; b>=0; b--)
     {
      if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
          if( OrderMagicNumber() == MagicNumber1 || OrderMagicNumber()== MagicNumber2 || OrderMagicNumber()== MagicNumber3 )
            if(OrderSymbol() == Symbol())
               {

               if(OrderType()==OP_BUY)
                  {
                  if(OrderStopLoss() - BuyStopPrice > Point / 2.)continue;
                  if(BuyStopPrice - OrderStopLoss() > Point / 2.)
                  bool BuyModify = OrderModify(OrderTicket(),OrderOpenPrice(),BuyStopPrice,OrderTakeProfit(),0,CLR_NONE);
                     
          
                   }     

    
               if(OrderType()==OP_SELL)
                  {
                  if(SellStopPrice - OrderStopLoss() > Point / 2. )continue;  
                  if(OrderStopLoss() - SellStopPrice > Point / 2. )
                     bool SellModify = OrderModify(OrderTicket(),OrderOpenPrice(),SellStopPrice,OrderTakeProfit(),0,CLR_NONE);
                     
                   
                  }
               }   
    
     }
}
How come this works better? I have 3 open positions but I only use OrderModify once? Yet it moves all three of the positions trailing stops in unison? 
 
void Move_To_BreakEven()
  {
   for(int b=OrdersTotal()-1; b>=0; b--)
     {
      if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
          if( OrderMagicNumber()== MagicNumber2 || OrderMagicNumber()== MagicNumber3 )
            if(OrderSymbol() == Symbol())
             {            
               
               if(OrderType() == OP_BUY && Bid - btp1 > Point / 2. ) 
               if(OrderStopLoss() - (OrderOpenPrice() + (PipsToLockIn*pips)) > Point / 2. )break;

               {
                   Buy_StopLoss_Break_Even1 = OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+(PipsToLockIn*pips),OrderTakeProfit(),0,CLR_NONE);   
                   if(Buy_StopLoss_Break_Even1!=True)Print("BUY B/E Stop Loss Failed: ", GetLastError(), " On: ", OrderSymbol());
               }     
  
               if( OrderType() == OP_SELL && stp1 - Ask > Point / 2. )     
               if(OrderOpenPrice()-((PipsToLockIn*pips) - OrderStopLoss()) > Point / 2. )break;

               { 
                   Sell_StopLoss_Break_Even1 = OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-(PipsToLockIn*pips),OrderTakeProfit(),0,CLR_NONE);
                   if(Sell_StopLoss_Break_Even1!=True)Print("SELL B/E Stop Loss Failed: ", GetLastError(), " On: ", OrderSymbol());              
               }
      
            }
    
    }
}
Sorry for being a little bit spammy here, but would you also be kind enough to help me in understanding what I am doing wrong above? 
 
DomGilberto:
How come this works better? I have 3 open positions but I only use OrderModify once? Yet it moves all three of the positions trailing stops in unison? 

It's because of

 if( OrderMagicNumber() == MagicNumber1 || OrderMagicNumber()== MagicNumber2 || OrderMagicNumber()== MagicNumber3 )

When OrderMagicNumber1 is selected, it will modify that order

the the loop continues and when OrderMagicNumber2 is found and selected, it will modify that order

etc 

 
DomGilberto:
Sorry for being a little bit spammy here, but would you also be kind enough to help me in understanding what I am doing wrong above? 

You have

               if(OrderType() == OP_BUY && Bid - btp1 > Point / 2. ) 
               if(OrderStopLoss() - (OrderOpenPrice() + (PipsToLockIn*pips)) > Point / 2. )break;

               {
                   Buy_StopLoss_Break_Even1 = OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+(PipsToLockIn*pips),OrderTakeProfit(),0,CLR_NONE);   
                   if(Buy_StopLoss_Break_Even1!=True)Print("BUY B/E Stop Loss Failed: ", GetLastError(), " On: ", OrderSymbol());
               }     
  

 

 I think that you intend

               
               if(OrderStopLoss() - (OrderOpenPrice() + (PipsToLockIn*pips)) > Point / 2. )break;
               if(OrderType() == OP_BUY && Bid - btp1 > Point / 2. ) 
               {
                   Buy_StopLoss_Break_Even1 = OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+(PipsToLockIn*pips),OrderTakeProfit(),0,CLR_NONE);   
                   if(Buy_StopLoss_Break_Even1!=True)Print("BUY B/E Stop Loss Failed: ", GetLastError(), " On: ", OrderSymbol());
               }     
  

 

 You also mentioned in an earlier post that the order's TP was being modified to 0

This may mean that the value of btpl etc is being set to 0 somewhere in your code 

 
Ah yes of course! I am using the OR statement so it's looping through to find them all. Trailing seems to be up and running but this has got me trumped. All I want to do is simply store the first trade (1st closest target out of the 3 triggered ones) and use that first target price to trigger the other 2 to break even... I simply cannot get this working?

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
  
   if(IsNewCandle())
      {
      CheckForMaTrade();
      }
      
      if( OpenOrdersThisPair(Symbol()) > 0 ){
      MA_Trail();
      Move_To_BreakEven(); <<<<<<<<<<<<<<<<<<
      }

...


//+----------------------------------------------------------------------------------------------------------------------------------------+
// Move to break even function  
//+----------------------------------------------------------------------------------------------------------------------------------------+ 

void Move_To_BreakEven()
  {
   for(int b=OrdersTotal()-1; b>=0; b--)
     {
      if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
          if( OrderMagicNumber()==MagicNumber1 && OrderMagicNumber()== MagicNumber2 && OrderMagicNumber()== MagicNumber3 )
            if(OrderSymbol() == Symbol())
             {            
               
               if(OrderMagicNumber() == MagicNumber1)TakeProfit_1 = NormalizeDouble(OrderTakeProfit(),Digits);
               Print("TakeProfit_1 = ", TakeProfit_1);
               
               if( OrderStopLoss() - (OrderOpenPrice() + (PipsToLockIn*pips)) > Point / 2. )break;              
               if( OrderType() == OP_BUY && Bid - TakeProfit_1 > Point / 2. ) 
               if( OrderMagicNumber()== MagicNumber2 || OrderMagicNumber()== MagicNumber3 )
               {
                   Buy_StopLoss_Break_Even1 = OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+(PipsToLockIn*pips),OrderTakeProfit(),0,CLR_NONE);   
                   if(Buy_StopLoss_Break_Even1!=True)Print("BUY B/E Stop Loss Failed: ", GetLastError(), " On: ", OrderSymbol());
               }     
               
               if(OrderOpenPrice()-((PipsToLockIn*pips) - OrderStopLoss()) > Point / 2. )break;  
               if( OrderType() == OP_SELL && TakeProfit_1 - Ask > Point / 2. )     
               if( OrderMagicNumber()== MagicNumber2 || OrderMagicNumber()== MagicNumber3 )
               { 
                   Sell_StopLoss_Break_Even1 = OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-(PipsToLockIn*pips),OrderTakeProfit(),0,CLR_NONE);
                   if(Sell_StopLoss_Break_Even1!=True)Print("SELL B/E Stop Loss Failed: ", GetLastError(), " On: ", OrderSymbol());              
               }
      
            }
    
    }
}
 
//+--------------------------------------  
//Moving Average Trailing Stop Function
//+--------------------------------------  
void MA_Trail()

{
   ATR = iATR(NULL,60,14,1);
   MA = iMA(NULL,60,MA_Period,0,1,0,1);
   
   double BuyStopPriceMath = MA - ATR;
   double SellStopPriceMath = MA + ATR;
   
   double new_BSL = NormalizeDouble(BuyStopPriceMath,Digits);
   double new_SSL = NormalizeDouble(SellStopPriceMath,Digits);

   for(int b=OrdersTotal()-1; b>=0; b--)
     {
     if( !OrderSelect(b,SELECT_BY_POS,MODE_TRADES))continue;
      if( OrderMagicNumber() == MagicNumber1 || OrderMagicNumber()== MagicNumber2 || 
          OrderMagicNumber()== MagicNumber3 || OrderMagicNumber()== MagicNumber4 )
        if(OrderSymbol() == Symbol())
        {

         if( OrderType()==OP_BUY && new_BSL > OrderStopLoss() - Point )
           {
            bool BuyModify = OrderModify(OrderTicket(),OrderOpenPrice(),new_BSL,OrderTakeProfit(),0,CLR_NONE);
           }     

         if( OrderType()==OP_SELL && OrderStopLoss() > new_SSL - Point )
           {
            bool SellModify = OrderModify(OrderTicket(),OrderOpenPrice(),new_SSL,OrderTakeProfit(),0,CLR_NONE);
           }
        }   
     }
}
No idea what I am doing wrong here? There are 4 open orders that require their stops to be trailed. I get spammed with OrderModify error 1? (there are loads of prints but I took them out so its easier to read).

Any ideas? Still unresolved...
 
How can this not work? It's being called OnTick() if there are OpenOrdersThisPair > 0. This loop selects any one of those trades with the corresponding MagicNumber, if its a buy or sell, modify it and then loop again for the remaining MagicNumbers...? 

I feel like there is one tiny thing I need to change but simply cannot see it... It's riddles with OrderModify error 1...
Reason: