Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Index outside of bounds...

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

    Index outside of bounds...

    I have a strategy "Wtrader2" which uses 2 indicators ("MACDfast" & "MACDslow") when I run the script I get an error in the log:
    "Error on calling 'OnBarUpdate' method for strategy Wtrader2: Index was outside the bounds of the array"
    I have narrowed it down to a specific area of the script, bhowever I am unable to find the problem. I hope someone can help...

    Here is the script in full:

    Code:
     
    #region Variables
    // Wizard generated variables
    // User defined variables (add any user defined variables below)
    private bool DLYHF = false;
    private bool DLYLF = false;
    private bool Retrace61F = false;
    private bool Retrace78F = false;
    private bool Retrace127F = false;
    private bool RetraceF = false;
    private bool RetraceClosedF = false;
    private double Retrace61P;
    private double Retrace78P;
    private double Retrace127P;
    private double Retrace=0;
    private double DLYH=0;
    double DLYL=0;
    double DLYO=0;
    double StudyHigh = 0;
    double StudyLow = 0;
    double StudyOpen = 0;
    double StudyClose = 0;
    int Scale=0;
    private int fast = 89; // Default setting for Fastfast
    private int slow = 36; // Default setting for Fastslow
    private int smooth = 12; // Default setting for Fastsmooth
    private int fastseries = 0; // Default setting for SlowSeries
    private int slowseries = 0; // Default setting for SlowSeries
    private double macd;
    private double macdAvg;
    #endregion
    /// <summary>
    /// This method is used to configure the strategy and is called once before any strategy method is called.
    /// </summary>
    protected override void Initialize()
    {
    CalculateOnBarClose = false;
    int ID = BarsPeriod.Value;
    Add(PeriodType.Minute,ID*2);//Scale 1
    Add(PeriodType.Minute,ID*4);//Scale 2
    Add(PeriodType.Minute,ID*8);//Scale 3
    Add(PeriodType.Minute,ID*16);//Scale 4
    Add(PeriodType.Minute,ID*32);//Scale 5
    Add(PeriodType.Minute,ID*64);//Scale 6
    Add(PeriodType.Minute,ID*128);//Scale 7
    Add(PeriodType.Minute,ID*256);//Scale 8
    Add(PeriodType.Minute,ID*512);//Scale 9
    Add(PeriodType.Minute,ID*1025);//Scale 10
    Add(PeriodType.Minute,ID*4096);//Scale 11
    Add(PeriodType.Minute,ID*8192);//Scale 12
    Add(PeriodType.Minute, 1440);//Day
    Add(PeriodType.Minute, 7200);//Week
    Add(PeriodType.Month, 1); 
    Add(PeriodType.Year, 1);
    Add(MACDCrossFast(fast,slow,smooth));
    Add(MACDCrossSlow(fast,slow,smooth));
    Scale=1;
    }
     
    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    if(CurrentBar < 1)return;
    DLYH=Highs[13][0];
    DLYL=Lows[13][0];
    DLYO=Opens[13][0];
    DrawHorizontalLine("DailyHighLine",DLYH,Color.Red);
    DrawText("DailyHighText","Daily High "+DLYH.ToString("N2"),5,DLYH,Color.Black);
    DrawHorizontalLine("DailyLowLine",DLYL,Color.Green);
    DrawText("DailyLowText","Daily Low "+DLYL.ToString("N2"),5,DLYL,Color.Black);
    DrawHorizontalLine("DailyOpenLine",DLYO,Color.Blue);
    DrawText("DailyOpenText","Daily Open "+DLYO.ToString("N2"),5,DLYO,Color.Black);
    if (Close[0]>=DLYH)
    if (BarsInProgress == Scale)
    {
    if(DLYHF == false)
    {
    DLYHF = true;
    DLYLF = false;
    }
     
    if (Close[0]<=DLYL)
    {
    if(DLYLF == false)
    {
    DLYLF = true;
    DLYHF = false;
    }
    }
    if (DLYHF == true)
    {
    if (FirstTickOfBar)
    {
    StudyHigh = Highs[Scale][0];
    StudyLow = Lows[Scale][0];
    StudyOpen = Opens[Scale][0];
    StudyClose = Closes[Scale][0];
    DrawRectangle("Study",1,StudyLow,0,StudyHigh,Color.Blue,Color.Blue,5);
    Scale = Scale+1;
    if (Scale > 12)
    {
    Scale = 0;
    DLYHF = false;
    Print("DLYHF= "+DLYHF);
    RemoveDrawObject("RetraceText");
    RemoveDrawObject("RetraceIndicator");
    }
    }
    if(Close[0]>Low[1])
    {
    Retrace = (Close[0]-Low[1])/((High[1]-Low[1])/100);
    DrawDiamond("RetraceIndicator",0,Close[0],Color.Blue);
    DrawText("RetraceText",Retrace.ToString("N0")+"%",1,Close[0],Color.Black);
    if (Retrace >61 && Retrace <78 && Retrace61F == false)
    {
    RetraceF = true;
    Retrace61F = true;
    Retrace78F = false;
    Retrace127F = false;
    }
    if (Retrace >78 && Retrace <127 && Retrace78F == false)
    {
    Retrace61F = false;
    Retrace78F = true;
    Retrace127F = false;
    }
    if (Retrace >78 && Retrace <127 && Retrace78F == false)
    {
    Retrace61F = false;
    Retrace78F = false;
    Retrace127F = true;
    }
    }
    }
    if (DLYLF == true)
    {
    if (FirstTickOfBar)
    {
    StudyHigh = Highs[Scale][1];
    StudyLow = Lows[Scale][1];
    StudyOpen = Opens[Scale][1];
    StudyClose = Closes[Scale][1];
    Scale = Scale+1;
    if (Scale > 12)
    {
    Scale = 0;
    DLYLF = false;
    Print("DLYLF= "+DLYHF);
    RemoveDrawObject("RetraceText");
    RemoveDrawObject("RetraceIndicator");
    }
    }
    if(Close[0]<High[1])
    {
    Retrace = (Close[0]-Low[1])/((High[1]-Low[1])/100);
    DrawDiamond("RetraceIndicator",0,Close[0],Color.Blue);
    DrawText("RetraceText",Retrace.ToString("N0")+"%",1,Close[0],Color.Black);
    }
    }
    }
    [COLOR=red]if (BarsInProgress == fastseries)[/COLOR]
    [COLOR=red]{[/COLOR]
    [COLOR=red]if(MACDCrossFast(fast,slow,smooth).CurrentBar < 1)return;[/COLOR]
    [COLOR=red]MACDCrossFast(fast,slow,smooth).fastEma.Set((2.0 / (1 + MACDCrossFast(fast,slow,smooth).Fast)) * Input[0] + (1 - (2.0 / (1 + MACDCrossFast(fast,slow,smooth).Fast))) * MACDCrossFast(fast,slow,smooth).fastEma[1]);[/COLOR]
    [COLOR=red]MACDCrossFast(fast,slow,smooth).slowEma.Set((2.0 / (1 + MACDCrossFast(fast,slow,smooth).Slow)) * Input[0] + (1 - (2.0 / (1 + MACDCrossFast(fast,slow,smooth).Slow))) * MACDCrossFast(fast,slow,smooth).slowEma[1]);[/COLOR]
    [COLOR=red]macd = MACDCrossFast(fast,slow,smooth).fastEma[0] - MACDCrossFast(fast,slow,smooth).slowEma[0];[/COLOR]
    [COLOR=red]macdAvg = (2.0 / (1 + MACDCrossFast(fast,slow,smooth).Smooth)) * macd + (1 - (2.0 / (1 + MACDCrossFast(fast,slow,smooth).Smooth))) * MACDCrossFast(fast,slow,smooth).Avg[1];[/COLOR]
     
    [COLOR=red]MACDCrossFast(fast,slow,smooth).Value.Set(macd);[/COLOR]
    [COLOR=red]MACDCrossFast(fast,slow,smooth).Avg.Set(macdAvg);[/COLOR]
    [COLOR=red]MACDCrossFast(fast,slow,smooth).Diff.Set(macd - macdAvg);[/COLOR]
    [COLOR=red]}[/COLOR]
    [COLOR=red]if (BarsInProgress == slowseries)[/COLOR]
    [COLOR=red]{[/COLOR]
    [COLOR=red]Print(slowseries);[/COLOR]
    [COLOR=red]if(MACDCrossSlow(fast,slow,smooth).CurrentBar < 1)return;[/COLOR]
    [COLOR=red]MACDCrossSlow(fast,slow,smooth).fastEma.Set((2.0 / (1 + MACDCrossSlow(fast,slow,smooth).Fast)) * Input[0] + (1 - (2.0 / (1 + MACDCrossSlow(fast,slow,smooth).Fast))) * MACDCrossSlow(fast,slow,smooth).fastEma[1]);[/COLOR]
    [COLOR=red]MACDCrossSlow(fast,slow,smooth).slowEma.Set((2.0 / (1 + MACDCrossSlow(fast,slow,smooth).Slow)) * Input[0] + (1 - (2.0 / (1 + MACDCrossSlow(fast,slow,smooth).Slow))) * MACDCrossSlow(fast,slow,smooth).slowEma[1]);[/COLOR]
    [COLOR=red]macd = MACDCrossSlow(fast,slow,smooth).fastEma[0] - MACDCrossSlow(fast,slow,smooth).slowEma[0];[/COLOR]
    [COLOR=red]macdAvg = (2.0 / (1 + MACDCrossSlow(fast,slow,smooth).Smooth)) * macd + (1 - (2.0 / (1 + MACDCrossSlow(fast,slow,smooth).Smooth))) * MACDCrossSlow(fast,slow,smooth).Avg[1];[/COLOR]
     
    [COLOR=red]MACDCrossSlow(fast,slow,smooth).Value.Set(macd);[/COLOR]
    [COLOR=red]MACDCrossSlow(fast,slow,smooth).Avg.Set(macdAvg);[/COLOR]
    [COLOR=red]MACDCrossSlow(fast,slow,smooth).Diff.Set(macd - macdAvg);[/COLOR]
    [COLOR=red]}[/COLOR]
    [COLOR=red]}[/COLOR]
    }
    }
    The problem area is highlighted in Red, this is actually 2 areas which are nearly identical... one is for 'MACDfast' and the other for 'MACDslow', scripts are below:

    MACDfast & MACDslow are nearly identical, but have different names so that each can be called by strategy and referenced.

    Code:
     
    #region Variables
    public int fast = 12;
    public int slow = 26;
    public int smooth = 9;
    public int fastseries = 0;
    public DataSeries fastEma;
    public DataSeries slowEma;
     
    #endregion
    /// <summary>
    /// This method is used to configure the indicator and is called once before any bar data is loaded.
    /// </summary>
    protected override void Initialize()
    {
    Add(new Plot(Color.Green, "Macd"));
    Add(new Plot(Color.Black, "Avg"));
    Add(new Line(Color.DarkGray, 0, "ZeroLine"));
    fastEma = new DataSeries(this);
    slowEma = new DataSeries(this);
    }
    /// <summary>
    /// Calculates the indicator value(s) at the current index.
    /// </summary>
    protected override void OnBarUpdate()
    {
    }
    #region Properties
    /// <summary>
    /// </summary>
    [Browsable(false)]
    [XmlIgnore()]
    public DataSeries Avg
    {
    get { return Values[1]; }
    }
    /// <summary>
    /// </summary>
    [Browsable(false)]
    [XmlIgnore()]
    public DataSeries Default
    {
    get { return Values[0]; }
    }
     
    /// <summary>
    /// </summary>
    [Browsable(false)]
    [XmlIgnore()]
    public DataSeries Diff
    {
    get { return Values[2]; }
    }
    /// <summary>
    /// </summary>
    [Description("Number of bars for fast EMA")]
    [Category("Parameters")]
    public int Fast
    {
    get { return fast; }
    set { fast = Math.Max(1, value); }
    }
    /// <summary>
    /// </summary>
    [Description("Number of bars for slow EMA")]
    [Category("Parameters")]
    public int Slow
    {
    get { return slow; }
    set { slow = Math.Max(1, value); }
    }
    /// <summary>
    /// </summary>
    [Description("Number of bars for smoothing")]
    [Category("Parameters")]
    public int Smooth
    {
    get { return smooth; }
    set { smooth = Math.Max(1, value); }
    }
    #endregion
    }
    }

    #2
    Hi dhunnewell,

    I see your exceptions for

    if (CurrentBar < 1)
    return;

    but that you reference bars further back (as in your fast period)

    Please review the following post....


    Try something like

    if (CurrentBar < 89)
    return;
    TimNinjaTrader Customer Service

    Comment


      #3
      Hi,

      I get the same error and I am feeding a 7 period zma into a 7 period zma, even if i set to 200 then I still get the error. I am creating the indicator so I can plot different colors for the rising and falling. Any thoughts? Thanks.

      protected override void OnBarUpdate()

      {

      if ( CurrentBar < 200 )
      return;

      {

      EMA ema1 = EMA(Input, Period1);
      double difference = ema1[0] - EMA(ema1, Period1)[0];
      ZLEMA1.Set(ema1[0] + difference);

      EMA ema2 = EMA(ZLEMA1, Period2);
      double difference2 = ema2[0] - EMA(ema2, Period2)[0];
      ZMAHolder.Set(ema2[0] + difference2);

      Print(ema1[0] + difference);
      Print(ema2[0] + difference2);

      if ( ZMAHolder[1] < ZMAHolder[0])
      {
      ZMARising[1] = ZMAHolder[1];
      ZMARising.Set(ZMAHolder[0]);
      }
      else
      {
      ZMAFalling[1] = ZMAHolder[1];
      ZMAFalling.Set(ZMAHolder[0]);
      }

      }
      }
      Attached Files

      Comment


        #4
        Hi vegasfoster,

        A few things...

        1. You will need to set up ZLEMA1 dataseries as you did ZMAHolder.
        2. You appear to be setting ZMARising to Values[2];, yet the plot is at Values[0]
        3. You try to access ZMARising[1], before setting it one line later...ZMARising.Set(ZMAHolder[0]);
        TimNinjaTrader Customer Service

        Comment


          #5
          Got it sorted, thanks a lot.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          633 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          364 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          105 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          567 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          568 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X