Incorrect S/L and T/P for second order

 
When two instances of our expert advisor are running at the same time we sometimes encounter an issue with one of them not opening a second order with the correct T/P and S/L.

Here's what happens the first instance of the EA is attached to a EUR/USD H4 chart and the EA's magic number is set to 111111. The second instance of the EA is also attached to a EUR/USD H4 chart, magic number is set to 222222 but, this EA is set to open orders in the opposite direction (instead of a buy order it will open a sell order).

Both instances of the EA open the first order without any problems. For example the first instance of the expert advisor opens a buy order Ticket 13614875, Time 2007.08.27 00:00, Type buy, Size 0.02, Symbol eurusd, Price 1.3682, S/L 1.3542, T/P 1.3688. The second instance of the expert advisor opens a sell order Ticket 13614880, Time 2007.08.27 00:00, Type sell, Size 0.02, Symbol eurusd, Price 1.3682, S/L 1.3822, T/P 1.3676. Twenty minutes later Price drops and the sell order reaches its T/P (sell order is closed). The first instance correctly opens a second buy order Ticket 13614885, Time 2007.08.27 00:20, Type buy, Size 0.02, Symbol eurusd, Price 1.3679, S/L 1.3542, T/P 1.3688.

So now we have two open buy orders from the first instance of the program. The second instance of the program does not have any orders open. Then three and half hours later 04:00 the second instance of the program receives a signal to open a new order. The order is a buy order and is opened correctly Ticket 13614893, Time 2007.08.27 04:00, Type buy, Size 0.02, Symbol eurusd, Price 1.3653, S/L 1.3542, T/P 1.3659. Forty minutes later price moves and signals the second instance of the program to open a second order. The second instance opens the order but does it incorrectly Ticket 13614897, Time 2007.08.27 04:40, Type buy, Size 0.02, Symbol eurusd, Price 1.3650, S/L 1.3542, T/P 1.3688. The S/L and T/P should be exactly the same as the first buy order S/L 1.3542, T/P 1.3659 of the second instance. Instead the S/L and T/P are identical to the buy order of the first instance of the EA.

Here is an example of the code used to open a first buy/sell order:

//open first buy order
Ticket=OrderSend(Symbol(),OP_BUY, Lots,Ask,Slippage,Ask-(StopLoss*Point),Ask+(TakeProfit*Point),NULL,MAGIC,0,Green);
             
//open first sell order               
Ticket=OrderSend(Symbol(),OP_SELL, Lots,Bid,Slippage,Bid+(StopLoss*Point),Bid-(TakeProfit*Point),NULL,MAGIC,0,Green);





Here is an example of the code used to open a second buy/sell order:

//open second buy order
if(OrderSelect(Ticket,SELECT_BY_TICKET)==true)
{
	Ticket2=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,OrderStopLoss(),OrderTakeProfit(),NULL,MAGIC,0,Green);
}


//open second sell order
if(OrderSelect(Ticket,SELECT_BY_TICKET)==true)
{
	Ticket2=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,OrderStopLoss(),OrderTakeProfit(),NULL,MAGIC,0,Green);
}




I'm not sure exactly how Metatrader uses its variables in memory but, it seems as though both instances of the EA are accessing the same Ticket variable in memory. Am I right or is this a problem with the broker processing the order correctly.