I'm trying to access previous weekly closes in a strategy I'm testing in market replay, however it keeps giving me the indexing error (cannot access a bar that doesn't exist), which confuses me because I have a couple months of historical data downloaded and loaded on the chart when running this strategy.
I've included a simple strategy below that just prints the close of the prior 4 weeks. It prints the first week in the output just fine but then errors on the second (the indexing error). Any thoughts as to why and how to fix this? I did this on ES 12-18 in late November, with historical data loaded back to the beginning of October (8 weeks). Thanks.
#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.Test_Strategies
{
public class Test_Strat_030719 : Strategy
{
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Enter the description for your new custom Strategy here.";
Name = "Test_Strat_030719";
Calculate = Calculate.OnPriceChange;
EntriesPerDirection = 1;
EntryHandling = EntryHandling.AllEntries;
IsExitOnSessionCloseStrategy = false;
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)
{
AddDataSeries(BarsPeriodType.Week, 1);
}
}
protected override void OnBarUpdate()
{
if (State == State.Historical)
return;
if (BarsInProgress == 0)
{
Print("Closes[1][1] = "+Closes[1][1]);
Print("Closes[1][2] = "+Closes[1][2]);
Print("Closes[1][3] = "+Closes[1][3]);
Print("Closes[1][4] = "+Closes[1][4]);
}
}
}
}

Comment