I have observed that since I am using OnBarUpdate to place the orders, if the market moves quickly, I can see a dozen Entry and Exit calls. This 95% of the time works ok and only one Entry or Exit is actually placed.
Two things I have observed that are frustrating though...
1.) Once in a while (I woke up this morning to the issue) on Exit, the Exit will have placed 2 orders and my strategy will be out of whack... Meaning, I'll be in a trade the opposite direction and it won't have been because my strategy called an Enter in that direction. How do I prevent this from happening? This leads me to my second observation...
2.) The ExitLong/Short() calls do not call OnExecutionUpdate or OnOrderUpdate() functions so I cannot use those functions to set a variable to avoid placing multiple calls to ExitLong/Short().
How do I properly avoid calling ExitLong/Short() in very very quick succession? Is there something my OnBarUpdate() should be calling to properly sync up the Position.MarketPosition?
I'm using this code to check for Long and Short before exiting the trade. And I don't call a new Entry until the next iteration of OnBarUpdate. i.e. I don't call Exit and Entry in the same call to OnBarUpdate.
if (Position.MarketPosition == MarketPosition.Short) {
And here's an example from logging that shows how many times it's calling the Exit function because of OnBarUpdate() getting called so many times so quickly.
1/30/2020 2:00:28 AM - [ SilentThunder-ZBEG6N1K ] - didDotForm(): FALSE -- ToTime(dt): 224901 ToTime(Time[0]): 20028 ToTime(Time[1]): 20027 1/30/2020 2:00:29 AM - [ SilentThunder-ZBEG6N1K ] - OnBarUpdate(): Closing SHORT position at 28513 because price is at or below Take Profit at 28513 1/30/2020 2:00:29 AM - [ SilentThunder-ZBEG6N1K ] - OnBarUpdate(): Closing SHORT position at 28513 because price is at or below Take Profit at 28513 1/30/2020 2:00:29 AM - [ SilentThunder-ZBEG6N1K ] - OnBarUpdate(): Closing SHORT position at 28513 because price is at or below Take Profit at 28513 1/30/2020 2:00:29 AM - [ SilentThunder-ZBEG6N1K ] - TakeScreenShotMethod(): Attempting to save screenshot with message (Exiting Short Trade (Realized Daily PnL: $355.00)) to: Screenshot-20200130-020029.jpg 1/30/2020 2:00:29 AM - [ SilentThunder-ZBEG6N1K ] - OnBarUpdate(): Closing SHORT position at 28512 because price is at or below Take Profit at 28513 1/30/2020 2:00:29 AM - [ SilentThunder-ZBEG6N1K ] - OnBarUpdate(): Closing SHORT position at 28512 because price is at or below Take Profit at 28513 1/30/2020 2:00:29 AM - [ SilentThunder-ZBEG6N1K ] - OnBarUpdate(): Closing SHORT position at 28511 because price is at or below Take Profit at 28513 1/30/2020 2:00:29 AM - [ SilentThunder-ZBEG6N1K ] - OnBarUpdate(): Closing SHORT position at 28511 because price is at or below Take Profit at 28513 1/30/2020 2:00:29 AM - [ SilentThunder-ZBEG6N1K ] - OnBarUpdate(): Closing SHORT position at 28511 because price is at or below Take Profit at 28513 1/30/2020 2:00:29 AM - [ SilentThunder-ZBEG6N1K ] - OnBarUpdate(): Closing SHORT position at 28511 because price is at or below Take Profit at 28513 1/30/2020 2:00:29 AM - [ SilentThunder-ZBEG6N1K ] - OnBarUpdate(): Closing SHORT position at 28511 because price is at or below Take Profit at 28513
In summary (TL/DR), is there something other than Position.MarketPosition I should be checking to ensure the order placement is synced between all current and future instances of OnBarUpdate() so that my strategy is not calling the Entry/Exit functions potentially dozens of times?
Thanks,
Timothy

Comment