I came across serious problem working with real accounts (Forex.com).
Suppose i have a signal to open position. In OnBarUpdate handler i check if my position is flat and if it is, I send the order to broker. Since broker will respond me asynchronously, I need to pause my strategy until this order will be proceeded. Ok, no problem: I check in OnBarUpdate whether I have non-filled orders. If yes, then I skip this bar update.
So far so good: I still have flat position and one order being processed. Some milliseconds later broker changes the status of the order to "filled", but position state is still "flat". Then OnBarUpdate event raises and my strategy has:
a) still flat position
b) no orders in processing
and it decides to open position once again. It sends new order. Some milliseconds later OnPositionUpdate event triggers, but it's too late, because the second order is already sent! This situation repeats until OnPositionUpdate triggers before OnBarUpdate. Sometimes I have 4 or even 5 orders in a row for the same signal!
In other words, if i have this sequence of events, i have multiple entries:
- OnOrderUpdate (position is flat, order filled)
- OnBarUpdate(positon is still flat, all orders filled).
- OnPositionUpdate (position is long/short)
How can i distinguish in OnBarUpdate event, that my position will be updated soon from flat state? I thought about setting some flag in OnOrderUpdate handler and then resetting it in OnPositionUpdate handler. But it seems not very reliable
The issue may be replicated only with real accounts. In Sim accounts OnPositionUpdate follows right after OnOrderUpdate. OnBarUpdate never occurs between these events.
Any recommendations?
Comment