Can i get a poll of the best way to submit orders - secifically protective and reverse orders eg long 3 exit and reverse is therefore 6 contracts. I had a situation today where my reverse order just got missed and i ended up with a naked position. i happened to have a sim account running with exactly the same code and everything worked. So it’s an order submission/timing issue i am guessing? Again any insight would be helpful.
|
SimAccount1 (MES) |
Live (MES) |
| Entry |
MEL fills 3 @ 7792.25 |
MEL fills 3 @ 7792.75 |
| Reverse |
ORREV sell-short 6 @ 7790.50, Oco='' → filled |
never submitted |
| Targets |
MT2 / MT3 placed, then MSL2 brackets |
none |
| Outcome |
flipped short 3, scaled out 7787 / 7785.75 |
sat long, flattened by a nameless market Sell 3 @ 7786 |
The key signal is never submitted. That is earlier than broker latency. First prove whether NinjaTrader ignored the request.
Turn on TraceOrders and log these from OnBarUpdate, OnOrderUpdate, and OnExecutionUpdate: signal name, returned order ID, state, error/native error, filled/remaining quantity, and position before/after. With the Managed approach, internal order-handling rules can ignore a request; TraceOrders should state why.
I would implement the reversal as a small state machine:
- Set
reversalRequested and block every other entry path.
- Track the current protective orders explicitly.
- Submit the reversal only after the expected prior order state is confirmed by a callback.
- Create the new protection from actual reversal fills in
OnExecutionUpdate, not from the assumed six-contract request.
Also search for any fallback branch that can submit an unnamed flatten order or clear reversalRequested. The nameless Sell 3 in the live run suggests that cleanup logic fired after the intended ORREV path did not submit.
A good replay acceptance case is: long 3 becomes short 3, exactly one protective bracket exists for the filled short quantity, and no orphan order remains after a delayed callback or reconnect.
Be sure when you are testing this, if you use OnExecutionUpdate or OnOrderUpdate (OnExecutionUpdate is advisable) that you only reference the ARGUMENTS passed into the call, not the strategies properties like Position.Quantity because with the multithreading model of NT you can only rely on the arguments, nothing else. Additionally, don’t assume that because it works on sim or demo or market replay that it works live. The order in which these events arrive is non-deterministic, and the pattern varies from broker to broker.
2 Likes
Thank you for the input. I have now got it where it seems to work - and importantly it fired on the playback of the day i had the issue. Always learning in this game!