NinjaTrader duplicating orders in strategy

I have coded a strategy and am having trouble with duplicating orders. Here is my code in which I specify an entry and a bracket and what happens in real life.
Here is what happens in real life:

Instead of only submitting my instructed stop order (BOUS), another order is almost simultaneously submitted (Close position), thereby flipping my position instead of getting me flat. It happens within the same second as you can see in the time stamp. How can I fix this?

Hi @losbolis08 , One thing you could try would be to change “Set 6”, “Set 7”, and any other “if blocks” (except the very first one at the very top of the OnBarUpdate() method’s code) from “if blocks” to “else if blocks” (just add the word "else " in front of “if”) so that you remove the possibility of submitting a market order to enter a long position and to exit the same long position on the same tick, price change, or bar close if the conditions of their enclosing “if blocks” are True on the same tick, price change, or bar close. The problem you describe could also be caused by multiple “if” blocks, one after the other, that check for “Position.MarketPosition == MarketPosition.Long”, multiple of which could be True, one after the other, even if a previous “if block” condition set was True and an ExitLong() method call already submitted an exit order, since the Position.MarketPosition isn’t necessarily updated synchronously, i.e. at the moment the ExitLong() method is called. It is good to only attempt to enter or exit the same position (based on entry signal name) once on the same execution of the “OnBarUpdate()” method, i.e. once for each tick, price change, or bar close, and not rely on the value of Position.MarketPosition being correct the moment you submit an order, even if it is a market order.

Addendum: To demonstrate how a NinjaScript strategy’s Position.MarketPosition is not updated synchronously from Long to Flat, the moment the “ExitLong()” method is called to submit a market order to exit a Long Position such that it would be Flat, I went ahead and ran a quick test, which you can do as well in your code.

I added the two Print() statements below, one above and one below an ExitLong() method call in a very simply strategy, as shown below:

Print("Before Exit Long: " + this.Position.MarketPosition.ToString());
ExitLong(“LX”, “LE”);
Print("After Exit Long: " + this.Position.MarketPosition.ToString());

Here is what was printed to the NinjaScript Output window each time the code above was called when the strategy was Long:

Before Exit Long: Long
After Exit Long: Long

I just wanted to demonstrate the danger of using multiple “if blocks” that each call “ExitLong()” if Position.MarketPosition is Long, which is one of the issues I mentioned in my previous post. This is why using multiple “else if blocks” after an initial “if block” is a safer choice that using multiple “if blocks” when using the value of Position.MarketPosition in multiple conditions of multiple conditional logic blocks (by which I mean either “if blocks” or “else if blocks”) that each have EnterLong(), ExitLong(), or similar order submission methods that get executed if their conditions are True. “else if” blocks are nice because only one of the conditional logic blocks in an if-else if-else if-else if-else block will be entered, even if more than one of the condition sets of the “if block” or “else if” blocks are True, so you won’t need to worry about submitting two market orders to exit a position on the same tick, price change, or bar close due to the asynchronous behavior of Position.MarketPosition.

Thank you so much I’ll be testing these suggestions!

1 Like