Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy opening too many positions

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Strategy opening too many positions

    I'm testing a strategy on a simulated account using a Zen-Fire feed and observing strange behavior. I'm using NinjaTrader 7.0.1000.4 (Live).

    Below is my order entry & exit conditions, and " CalculateOnBarClose = true". The conditions are based on the "SampleMultiTimeFrame" strategy included with NinjaTrader.

    Code:
    [FONT="Fixedsys"]if (SMA(BarsArray[1], smaf)[0] > SMA(BarsArray[1], smas)[0] && SMA(BarsArray[2], smaf)[0] > SMA(BarsArray[2], smas)[0])
    	{
    		if (CrossAbove(SMA(smaf), SMA(smas), 1))
    		EnterLong(DefaultQuantity, "SMA");
    	}
    
    if (SMA(BarsArray[1], smaf)[0] < SMA(BarsArray[1], smas)[0] && SMA(BarsArray[2], smaf)[0] < SMA(BarsArray[2], smas)[0])
    	{
    		if (CrossBelow(SMA(smaf), SMA(smas), 1))
    		EnterShort(DefaultQuantity, "SMA");
    	}
    			
    if (CrossBelow(SMA(BarsArray[2], smaf), SMA(BarsArray[2], smas), 1))
    	ExitLong("", "SMA");
    
    if (CrossAbove(SMA(BarsArray[2], smaf), SMA(BarsArray[2], smas), 1))
    	ExitShort("", "SMA");[/FONT]
    My intention is to be only 1 contract LONG or 1 contract SHORT at any time. What I experienced is illustrated in the attached chart image. At several points I have 2 contracts open when I only want to have one.

    Can you tell me what I am doing wrong? What is even more puzzling is this behavior doesn't occur in backtesting, only when running live.

    Thanks in advance for your help!
    Attached Files

    #2
    Hello,

    THanks for the note.

    The OnBarUpdate is run for each series in a NinjaScript strategy. Therefor you need to filter this out. I dont see any if (BarsInProgress == 0) for example checks in your code.

    Please see this sample on how to do this.



    Specifically the Entering Exiting positions sections.

    Let me know if I can be of further assistance.
    BrettNinjaTrader Product Management

    Comment


      #3
      Hi Brett,

      I do have the following code immediately before the first condition:
      Code:
      if (BarsInProgress != 0)
      				return;
      			
      			if (Historical) 
      			{
      			return;
      			}
      
      // Condition 1...

      Comment


        #4
        Hello,

        On your chart, I see the SMA buy order EnterLong(), I see the Sell order which is your exitLong(). What I dont see is where is this Close Position signal coming from?

        Do you have any further code your note showing yet? Anything else that might explain this closing order?

        Other then this I would simply need to stest it on my side. Please send the code into support at ninjatrader dot com ATTN: Brett and reference this forum post. As I do not see anything off that would cause this.
        BrettNinjaTrader Product Management

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        672 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        379 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        111 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        575 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        582 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X