Managing Event Handlers

 

Hello all,

I'm currently encountering the following problem:
 - BAR 1 : I have an open position and 2 pending orders (Profit Target and Stop Loss) not attached to the order (not within the order, i.e as separate orders).
 - BAR 2 : On opening of the bar, Stop Loss is touched and position is closed, however OnTradeTransaction() is not called ! The OnTick() event handler is first called (as there is a new quote), then the OnTradeTransaction() event handler is called. However my order management system is only updated through OnTradeTransaction() because it needs to know what has been executed and what hasn't. In this example, if OnTradeTransaction() had been called first, I would have known with MqlTradeTransaction.type what had happened and updated my strategy, knowing that the order had been filled, and the position closed.

To make it short, is there any way to prioritize the OnTradeTransaction() event on the OnTick() event.

Thanks in advance,

Sk

 
Skullnick:

I'm currently encountering the following problem:
 - BAR 1 : I have an open position and 2 pending orders (Profit Target and Stop Loss) not attached to the order (not within the order, i.e as separate orders).
 - BAR 2 : On opening of the bar, Stop Loss is touched and position is closed, however OnTradeTransaction() is not called ! The OnTick() event handler is first called (as there is a new quote), then the OnTradeTransaction() event handler is called. However my order management system is only updated through OnTradeTransaction() because it needs to know what has been executed and what hasn't. In this example, if OnTradeTransaction() had been called first, I would have known with MqlTradeTransaction.type what had happened and updated my strategy, knowing that the order had been filled, and the position closed.

To make it short, is there any way to prioritize the OnTradeTransaction() event on the OnTick() event.

No. The events are synchronous, you can't change their order.

Your possible solution could be not using OnTick at all. Instead, call SymbolInfoTick from OnTradeTrsaction to get latest known tick data.

 
Thanks for your answer. But how would I update my system with your solution ? OnTradeTransaction is only called when there are trades detected. I need to update my system on each tick/bar to see if new valid conditions for trading arise. The problem is that I need to update it after having processed trades, not before.
Here I have a limit order that has been executed on the open of a new tick/bar, and the OnTick() event is first called, then the OnTradeTransaction(). Thus my system is unaware that a trade has been executed and proceeds to finding new conditions to open/close orders.
 
You can implement a trigger - a special variable storing last known state of ticks and accessible for postponded processing. When an event OnTick occurs, check conditions in the stored state, then upade the variable. In OnTradeTransaction (if necessary) use the variable instead of direct tick data. Your system will be always at least 1 tick behind the real-time data, but this is the only workaround I can think of right now.