Thanks for your help.
(Strategy 'DemoTest2': Error on calling 'OnStateChange' method: 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.)
namespace NinjaTrader.NinjaScript.Strategies
{
public class DemoTest2 : Strategy
{
private ZLEMA ZLEMA1;
private EMA EMA1;
private ATR ATR1;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Enter the description for your new custom Strategy here.";
Name = "DemoTest2";
Calculate = Calculate.OnBarClose;
EntriesPerDirection = 1;
EntryHandling = EntryHandling.AllEntries;
IsExitOnSessionCloseStrategy = false;
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.ByStrategyPosition;
BarsRequiredToTrade = 55;
// 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)
{
ZLEMA1 = ZLEMA(Close, 14);
EMA1 = EMA(Close, 50);
ATR1 = ATR(Close, 14);
ZLEMA1.Plots[0].Brush = Brushes.Goldenrod;
ATR1.Plots[0].Brush = Brushes.DarkCyan;
AddChartIndicator(ZLEMA1);
AddChartIndicator(ATR1);
SetProfitTarget(@"GoLong", CalculationMode.Price, (Close[1] + ((ATR1[1] * 1.5) )) );
}
}
protected override void OnBarUpdate()
{
if (BarsInProgress != 0)
return;
if (CurrentBars[0] < 2)
return;
// Set 1
if ((ZLEMA1[0] > ZLEMA1[1])
&& (ZLEMA1[1] < ZLEMA1[2])
&& (EMA1[0] >= EMA1[1]))
{
EnterLong(Convert.ToInt32(DefaultQuantity), @"GoLong");
Draw.ArrowUp(this, @"DemoTest2 Arrow up_1", false, 0, (Low[0] + (-5 * TickSize)) , Brushes.Lime);
}
// Set 2
if ((ZLEMA1[0] < ZLEMA1[1])
&& (ZLEMA1[1] > ZLEMA1[2])
&& (EMA1[0] <= EMA1[1]))
{
EnterShort(Convert.ToInt32(DefaultQuantity), @"GoShort");
Draw.ArrowDown(this, @"DemoTest2 Arrow down_1", false, 0, (High[0] + (5 * TickSize)) , Brushes.Red);
}
}
}
}
Comment