Within a loop, I must use the variable Close[0] and at the same time, assigning a certain value to a List array variable. Despite all is well declared previously, and it compiles flawlessly, when it comes to act, a runtime error appears:
Error on calling 'OnBarUpdate' method for strategy 'QWS/d86de07fec8c46148c329c5d2a13fd08': You are accessing an index with a value that is invalid since its out of range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.
Obviously is cause I'm trying to use Close[0] object with a List array variable at the same time. How to declare properly variables, procedures...etc in order to avoid "interference" and being able to run the script?
Here is the structure of the script:
namespace NinjaTrader.Strategy { /// <summary> /// Q /// </summary> using System.Collections.Generic; [Description("Q")] public class QWS : Strategy { #region Variables // all variables here #endregion List<int> cable = new List<int>(); protected override void Initialize() { SetStopLoss("", CalculationMode.Ticks, pnllimit, false); CalculateOnBarClose = true; BarsRequired=31; DisconnectDelaySeconds = 130; MaxRestartMinutes = 30; MaxRestartAttempts = 120; ConnectionLossHandling = ConnectionLossHandling.KeepRunning ; RestartDelaySeconds = 2; } protected override void OnPositionUpdate(IPosition position) { // do my stuff } protected override void OnBarUpdate() { if ( Bars.BarsSinceSession<= BarsRequired ) { return; } for ( int d = 1; d <= t; d++ ) { // do calculations using Close[0] and assign values to List Array } if( A) { EnterLong(qty); } if( B) { EnterShort(qty); } } protected override void OnTermination() { // Do final }
Comment