Any help/advice on the following would be greatly appreciated.
I am setting my profit target limit order under certain conditions on a 1min basis.
My strategy is running on a tick-by-tick basis ("CalculateOnBarClose = false;") and simulating bar closes by using "if (FirstTickOfBar ) {..." .
This is working as it should in realtime, and on market replay. The problem very occasionally arises when I reload the strategy and the (Historical) reload of the data causes the profit target to be hit due to the 1min bar data (OHLC) being loaded for a particular bar *before* the new profit target for that minute can be set.
An example:
At 09:14:00 my profit target for my long position is 134.79. (The high so far on the day was 134.78).
At 09:15:00 - when running the strategy in realtime - the new target gets moved by the code in the OnBarUpdate() method to 134.89 and the target is not hit. I am still long. (The price goes to 134.79 at 09:15:26, ie 26 seconds after the target is moved higher when running in realtime).
If, however, I reload the strategy at any point after 09h15 then at the open of the 09h15 bar (before the code has a chance to move the profit target higher) the system checks the OHLC 1min bar data, which has a 1min high of 134.80, and declares that the profit target order is filled at 09:15:00 at 134.79.
Of course in realtime that high is not really hit until 09:15:26, which allows the code to move the profit target higher to the new target price which it does when running in realtime at 09:15:00.
I understand that historically the bar data is loaded once for each bar and that therefore the system 'sees' the OHLC close data for the 09h15 bar as a single event and so fills my limit order at 134.79.
My question is therefore as follows:
Is there anyway when loading *(Historical)* data to prevent the order processing from occurring *before* my code updates the positions/orders via the OnBarUpdate method?
Or to put it another way: is it possible to *prioritise* the code within my OnBarUpdate() method over the *historical* order processing, so that order fills on a historical re-load of data do not occur until after the OnBarUpdate() method has run? (This would effectively be like saying to the system to only fill orders at the *end* of each 1min bar when loading historical data, not at the beginning of each bar).
Many thanks.
Comment