I'm developing an unmanaged order strategy. I'm currently putting it through stress testing to identify issues that still need to be resolved or addressed. I've come across an issue where there is an open real order either after I close my strategy or seemingly not controlled by the current instance of the strategy, i.e. lingering from the previous session. I haven't figured out yet what might be causing this, e.g. overfill, order was live when strategy terminated for an error (e.g. out of range index, etc. still cleaning up some of these). My question is what is a robust way to handle this? Should I for instance check Position.MarketPosition during OnStateChange State == State.Terminated and call ExitLong or ExitShort if position is not flat, or is it too late by then?
Related to this, what is the most accurate way to tell the realtime market position from within the strategy? I've been logging Position.MarketPosition during OnBarUpdate and it didn't show that this order was still open from before the new invocation of the strategy, i.e. it was reporting Flat. Should I use PositionAccout.MarketPosition instead? What is the difference between the two and when should I use one vs. the other?
Thanks.
Comment