Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Error using strategy builder

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

    Error using strategy builder

    Hi!

    Just tried building my first strategy. The startegy looks at price change (close vs close[-1]) and uses input var to define an intervall.

    I get this error message when I try to enable it in a chart.

    Strategy 'lctrial': Error on calling 'OnBarUpdate' method on bar 1: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.

    ...but I´m only accessing -1 bars back?

    See code below.

    BarsRequiredToTrade = 20;
    // Disable this property for performance gains in Strategy Analyzer optimizations
    // See the Help Guide for additional information
    IsInstantiatedOnEachOptimizationIteration = true;
    Upprpocent = 1.02;
    Maxprocent = 1.13;
    Stddev = 1;
    Closeniva = 0.5;
    Omsattning = 400000;
    }
    else if (State == State.Configure)
    {
    }
    }

    protected override void OnBarUpdate()
    {
    if (CurrentBars[0] < 1)
    return;

    // Set 1
    if ((Close[0] >= (Close[-1] * Upprpocent) )
    && (Close[0] <= (Close[-1] * Maxprocent) ))
    {
    EnterLong(Convert.ToInt32(DefaultQuantity), "");
    }
    // Set 2
    if ((Close[0] <= (Close[-1] * Upprpocent) )
    || (Close[0] >= (Close[-1] * Maxprocent) ))
    {
    ExitLong(Convert.ToInt32(DefaultQuantity), "", "");
    }

    }

    #2
    Hello Nungwe,

    Thanks for your post.

    The bar index of [-1] would actually be pointing to a bar in the future. If you are wanting to compare the current close to the previous close then you would use (from your example): Close[0] >= (Close[1] * Upprpocent

    The bar index [] is a "bars ago" index. Reference: http://ninjatrader.com/support/helpG...-us/?close.htm

    The [0] would be the current bar, [1] is the bar before [2] is 2 bars back from the current bar, [3] is 3 bars back from the current bar, etc.
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      I see....changed it now it works great!! Thanx!

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by DJ888, 04-16-2024, 06:09 PM
      6 responses
      18 views
      0 likes
      Last Post DJ888
      by DJ888
       
      Started by Jon17, Today, 04:33 PM
      0 responses
      1 view
      0 likes
      Last Post Jon17
      by Jon17
       
      Started by Javierw.ok, Today, 04:12 PM
      0 responses
      6 views
      0 likes
      Last Post Javierw.ok  
      Started by timmbbo, Today, 08:59 AM
      2 responses
      10 views
      0 likes
      Last Post bltdavid  
      Started by alifarahani, Today, 09:40 AM
      6 responses
      41 views
      0 likes
      Last Post alifarahani  
      Working...
      X