[EDIT: Partial Fills don't seem to be relevant. I'm seeing the behavior with no Partial Fills as well]
Context: I'm trying to understand why my working orders are getting cancelled. Based on my investigation, this is NinjaTrader deciding to cancel the orders, not anything I'm doing in the strategy, I think this has to do with some knowledge I'm missing on how NT is handling the orders behind the scenes.
Some Pseudo code:
- in OnBarUpdate, I enter a long for 10 contracts
- in OnExecutionUpdate, I create my StopLoss and 2 TakeProfit targets. The first TP has 4 orders, the second TP has 6 order.
- time goes by
- I'm lucky! my first TakeProfit is hit as a partial fill
- in OnOrderUpdate, I move my StopLoss using ChangeOrder()
- in the Logs, I see Cancel requests for my StopLoss and for my other TakeProfit
- in the Logs, I see the order be cancelled
- I'm left with 6 working orders and no TP and no SL (so.. very very bad).
Attached is a screenshot of the Logs with some compelling artwork.
More in depth:
- I do in fact have code paths that can cancel orders. This is where I focused most of my time.
- in OnOrderUpdate, if StopLoss is hit, I cancel all the order and clean up
- in OnOrderUpdate, if all of the TakeProfits are "Filled" and I have 0 filled orders, I cancel all orders and clean up
- I've added logging to both these scenarios
- I've also added logging to the cancel methods themselves.
- When the scenario happens (partial fill -> cancel all orders), I no trace of my cancel methods in the logs.
- As part of my logs, I'm also dump the state of all my orders (main entry, take profits and stoploss). They are look normal before the cancel.
- I've also added debug level logs that log out every single OrderUpdate and ExecutionUpdate, and I just added every PositionUpdate.
- For my managed approach, I use:
-- EnterLong / EntryShort to start my order
-- ExitLongLimit / ExitShortLimit for my TakeProfits
-- ExitLongStopMarket / ExitShortStopMarket for my StopLoss
- This all works fine in the Strategy Analyzer.
- I've been testing this on Sim accounts (as you can see in the screenshot above).
What I've done so far:
- I worked on a lot on my OnOrderUpdate logic to make sure I wasn't calling the cancels on accident.
- My theory yesterday had to do with OCO order. This led me down 2 paths:
--> I did have signalnames on my stoploss and TakeProfits. I removed those
--> I switched to unmanaged
- Both the above approaches led to the same result. Canceled orders on partial fills.'
[EDIT: Not true. As it turned out, switching to unmanaged does in fact fix the behavior]
- Added lots of logging..
So that's where I'm at right now. From what I can tell, NT makes a decision to cancel my orders. I don't understand why it would do that, and if there's something I can do to stop it.
Cheers!
Stephen

Comment