Take profit and Stop loss are getting triggered by other orders.

 

I am making an Expert Advisor and I've run into an issue. Some of the orders that are placed are immediately canceled by other orders stop loss and take profits.

For example In my journal it says that order #33's take profit was triggered by order #150's price:


2022.07.22 19:57:08.340 Core 01 2022.01.03 16:48:24   take profit triggered #33 sell 0.01 EURUSD 1.13259 sl: 1.13292 tp: 1.13204 [#150 buy 0.01 EURUSD at 1.13204]

2022.07.22 19:57:08.340 Core 01 2022.01.03 16:48:24   deal #150 buy 0.01 EURUSD at 1.13204 done (based on order #150)

2022.07.22 19:57:08.340 Core 01 2022.01.03 16:48:24   deal performed [#150 buy 0.01 EURUSD at 1.13204]

2022.07.22 19:57:08.340 Core 01 2022.01.03 16:48:24   order performed buy 0.01 at 1.13204 [#150 buy 0.01 EURUSD at 1.13204]

2022.07.22 19:57:08.340 Core 01 2022.01.03 16:48:25   take profit triggered #63 sell 0.01 EURUSD 1.13259 sl: 1.13281 tp: 1.13203 [#151 buy 0.01 EURUSD at 1.13203]

2022.07.22 19:57:08.340 Core 01 2022.01.03 16:48:25   deal #151 buy 0.01 EURUSD at 1.13203 done (based on order #151)

2022.07.22 19:57:08.340 Core 01 2022.01.03 16:48:25   deal performed [#151 buy 0.01 EURUSD at 1.13203]

2022.07.22 19:57:08.340 Core 01 2022.01.03 16:48:25   order performed buy 0.01 at 1.13203 [#151 buy 0.01 EURUSD at 1.13203]


Here is the code for my orders:

Buy

 		  requestz[z].action = TRADE_ACTION_DEAL;
                  requestz[z].symbol = Symbol();
                  requestz[z].volume = tradeAmount;
                  requestz[z].type = ORDER_TYPE_BUY;
                  requestz[z].price = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
                  requestz[z].deviation=1;
                  requestz[z].magic = z;
                  requestz[z].expiration = TimeCurrent() + (datetime)GetAverageReqired(z,15);
                  requestz[z].sl = SymbolInfoDouble(_Symbol,SYMBOL_BID)-GetAverageReqired(z,13)*Point();
                  requestz[z].tp = SymbolInfoDouble(_Symbol,SYMBOL_ASK)+GetAverageReqired(z,12)*Point();
                  ticke=OrderSend(requestz[z],resultz[z]);
                  if(ticke>0)
                    {
                     SymbolInfoTick(_Symbol,last_tick);
                     Ask=last_tick.ask;
                     Bid=last_tick.bid;
                     wait[z] += GetAverageReqired(z,14);
                    }

Sell

                  requestz[z].action = TRADE_ACTION_DEAL;
                  requestz[z].symbol = Symbol();
                  requestz[z].volume = tradeAmount;
                  requestz[z].type = ORDER_TYPE_SELL;
                  requestz[z].price = SymbolInfoDouble(Symbol(),SYMBOL_BID);
                  requestz[z].deviation=1;
                  requestz[z].magic = z;
                  requestz[z].expiration = TimeCurrent() + (datetime)GetAverageReqired(z,15);
                  requestz[z].sl = SymbolInfoDouble(_Symbol,SYMBOL_ASK)+GetAverageReqired(z,13)*Point();
                  requestz[z].tp = SymbolInfoDouble(_Symbol,SYMBOL_BID)-GetAverageReqired(z,12)*Point();
                  ticke=OrderSend(requestz[z],resultz[z]);
                  if(ticke>0)
                    {
                     SymbolInfoTick(_Symbol,last_tick);
                     Ask=last_tick.ask;
                     Bid=last_tick.bid;
                     wait[z] += GetAverageReqired(z,14);
                    }


I am probably mixing up Orders, Deals, and Positions and I might be interpreting the journal logs wrong, but if anyone could help me figure out what is happening I would appreciate it. 
 

I am guessing here, but looking at the first 4 lines of the journal, they all occur at the same time  (16:48:24)

  1. Order #33 appears to hit the tp: 1.13204 so it sells
  2. At the same time order #150 buys at at 1.13204

So it looks like these could be independent transactions, with the broker just matching buy and sell orders of the same volume - just so happens you are on both sides of the deal but at least they are kind enough to inform you. 

The second set of journal lines seem to exhibit the same pattern - perhaps experimenting with differing order volumes, prices for buy/sell could rule this out and reveal if this is so, but I have not seen this in my journal logs.

Maybe keeping the "Depth of Market" window open could help you explore this pattern too.

https://www.metatrader5.com/en/terminal/help/trading/depth_of_market

Depth of Market - Trading Operations - MetaTrader 5 Help
Depth of Market - Trading Operations - MetaTrader 5 Help
  • www.metatrader5.com
The Depth of Market (DOM) displays bids and asks for a particular instrument at the currently best prices (closest to the market). The Dept of...