"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");
}
}
}
}
Comment