Struggling to get Trailing Stop to work with multiple positions.

 

Hey!


So I've created an EA that works by looking at a condition at the close of a 4hr candle and if it meets the signal criteria then places TWO OrderSend commands.

The first one I have creates and order with a 1R take profit level. That's fine.

The second order I wish to have a trailing stop loss of 2x the risk level.

With the current criteria, in a long trend there could be multiple positions open so what I'm struggling to do is work out how to code the Trailing Stop Loss for each individual position that is open.

Her'es my current TSL code:

int TrailingStopOrder = OrderSelect(0, SELECT_BY_POS);
int TrailingStopOrderModify =0;

if (OrderType() == OP_BUY) 
         {
         if (OrderStopLoss() < Bid-Point*LongTrailingStop) 
                     {
                     TrailingStopOrderModify = OrderModify(OrderTicket(),0,Bid-Point*LongTrailingStop,OrderTakeProfit(),0,Red);
                     Print("Bid - ",Bid," TrailingStop - ",LongTrailingStop," OrderStopLoss - ",OrderStopLoss()," Bid-LongTrailingStop*Point - ",DoubleToStr(Bid-LongTrailingStop*Point,6)); 
                     }
         }
         
if (OrderType() == OP_SELL) 
         {
         if (OrderStopLoss() > Bid+Point*ShortTrailingStop) 
                     {
                     TrailingStopOrderModify = OrderModify(OrderTicket(),0,Bid+Point*ShortTrailingStop,OrderTakeProfit(),0,Red);
                     Print("Bid - ",Bid," TrailingStop - ",ShortTrailingStop," OrderStopLoss - ",OrderStopLoss()," Bid-ShortTrailingStop*Point - ",DoubleToStr(Bid-ShortTrailingStop*Point,6)); 
                     }
         }
         
if(TrailingStopOrder<0)
                                             {
                                             // OrderSend error.
                                             Print("Trailing Stop Order failed with error #",GetLastError()); 
                                             }
                                             
if(TrailingStopOrderModify<0)
                                             {
                                             // OrderSend error.
                                             Print("Trailing Stop Order Modify failed with error #",GetLastError()); 
                                             }

// LongTrailingStop and ShortTrailingStop is calculated when the order conditions are met. 
// LongTrailingStop = ((EntryPrice - StopLossPrice)*2)
// However, I realise that if there is two open long orders, then LongTrailingStop should be the value from the last order, but when I Print the error then it reads 0.0 
// hence why the TSL function isn't amending my order and returning error 130

I'm struggling to somehow make each order write it's entry and stop loss values somewhere then for the TSL code to be able to find the open positions and read the values, even if this means 4, 5, 6 or more open positions.


Any advice how to store this data and read back from it?


Many Thanks...