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 - OnBarUpdate - can't work out what's wrong

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

    Error - OnBarUpdate - can't work out what's wrong

    Hi, I'm just getting started with Ninjatrader. I've created a simple strategy using the strategy builder, of a 7 and 12 bar weighted moving average, so it buys long when the 7-bar crosses over the 12-bar, and exits 5 bars later. Or at least, that's what it's supposed to do. However, when I try to run the strategy analyser, I get the dreaded,

    "Error on calling 'OnBarUpdate: method on bar 21: Index was outside the bounds of the array'.

    I've tried using Visual Studio to debug it, but a lot of the variables I would typically want to see don't seem to be available.

    Here is the complete code of the strategy. i haven't altered it since I unlocked the code, which I only did because it wasn't working originally for the same reason. I should add that when I use the standard SampleMAcrossover strategy it runs fine, which indicates the data is not likely to be causing the problem. Can anyone see anything obvious I've missed? I should add that I have looked at several other 'OnBarUpdate' posts and I haven't found an explanation that is meaningful to me. I used to be a professional programmer, mainly on IBM AS/400, so am comfortable with extremely technical explanations!

    #region Using declarations
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Gui;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Gui.SuperDom;
    using NinjaTrader.Gui.Tools;
    using NinjaTrader.Data;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Core.FloatingPoint;
    using NinjaTrader.NinjaScript.Indicators;
    using NinjaTrader.NinjaScript.DrawingTools;
    #endregion

    //This namespace holds Strategies in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class WMA712dayX : Strategy
    {
    private WMA WMA1;
    private WMA WMA2;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"WMA 7 and 12 bar crossover";
    Name = "WMA712dayX";
    Calculate = Calculate.OnBarClose;
    EntriesPerDirection = 1;
    EntryHandling = EntryHandling.UniqueEntries;
    IsExitOnSessionCloseStrategy = true;
    ExitOnSessionCloseSeconds = 30;
    IsFillLimitOnTouch = false;
    MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
    OrderFillResolution = OrderFillResolution.Standard;
    Slippage = 0;
    StartBehavior = StartBehavior.WaitUntilFlat;
    TimeInForce = TimeInForce.Gtc;
    TraceOrders = false;
    RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
    StopTargetHandling = StopTargetHandling.PerEntryExecution;
    BarsRequiredToTrade = 20;
    // Disable this property for performance gains in Strategy Analyzer optimizations
    // See the Help Guide for additional information
    IsInstantiatedOnEachOptimizationIteration = true;
    }
    else if (State == State.Configure)
    {
    }
    else if (State == State.DataLoaded)
    {
    WMA1 = WMA(Close, 7);
    WMA1.Plots[0].Brush = Brushes.Maroon;
    AddChartIndicator(WMA1);
    WMA2 = WMA(Close, 12);
    WMA2.Plots[0].Brush = Brushes.Lime;
    AddChartIndicator(WMA2);
    }
    }

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

    // Set 1
    if (CrossAbove(WMA1, WMA2, 1))
    {
    EnterLong(Convert.ToInt32(DefaultQuantity), @"EL");
    }

    // Set 2
    if (BarsSinceEntryExecution(5, "", 0) == 5)
    {
    ExitLong(Convert.ToInt32(DefaultQuantity), @"ELX", @"EL");
    }

    }
    }
    }
    Last edited by herseem; 10-25-2017, 05:01 AM.

    #2
    Hello herseem,

    Thanks for your post.

    The cause of the error would be this line: (BarsSinceEntryExecution(5, "", 0) == 5). The 5 inside the inner parenthesis means you are looking for the 5th added data series which would apply to a multi series or time frame type strategy. Please change that to 0. Also as you have named your entry "EL" as the signal name, you will need to add the signal name as well, for example: (BarsSinceEntryExecution(0, @"EL", 0) == 5)
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Paul,

      That - and eventually remembering that my amended code needed to be recompiled - did the trick. Thank you very much!

      Mike

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by andrewtrades, Today, 04:57 PM
      1 response
      8 views
      0 likes
      Last Post NinjaTrader_Manfred  
      Started by chbruno, Today, 04:10 PM
      0 responses
      6 views
      0 likes
      Last Post chbruno
      by chbruno
       
      Started by josh18955, 03-25-2023, 11:16 AM
      6 responses
      436 views
      0 likes
      Last Post Delerium  
      Started by FAQtrader, Today, 03:35 PM
      0 responses
      7 views
      0 likes
      Last Post FAQtrader  
      Started by rocketman7, Today, 09:41 AM
      5 responses
      19 views
      0 likes
      Last Post NinjaTrader_Jesse  
      Working...
      X