Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Adding to DateTime throwing error

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

    Adding to DateTime throwing error

    I am trying to add the timeframe period to a DateTime (see StartTime.AddMinutes(timeframe)) to achieve the outcome in the screenshot below using the following code and I get the following error. " Error on calling 'OnBarUpdate' method on bar 71: You are accessing an index with a value that is invalid since it is out-of-range."

    Oddly the code works on the M1 timeframe, giving the desired result, but does not work on the 5 min onward etc. I am an amateur coder so I can understand if the following code is not the best way of doing the job but any help would be appreciated.

    PHP Code:
    // if barsinprogress == 1 then we are using the series from state.configure. If this isnt true, end the program
    if (BarsInProgress != 1)
    {
        return;
    }
    
    // current DT gives time of the current bar
    DateTime current_dt = Times[1][0];
    
    
    
    for (int i = 0; i < starttimeslist.Count; i++)
    {
    
    
        // convert session time into a full DateTime for further use
        DateTime sess_StartTime = new DateTime(current_dt.Year, current_dt.Month, current_dt.Day, starttimeslist[i].Hour, starttimeslist[i].Minute, starttimeslist[i].Second, DateTimeKind.Local);
        DateTime sess_EndTime = new DateTime(current_dt.Year, current_dt.Month, current_dt.Day, endtimeslist[i].Hour, endtimeslist[i].Minute, endtimeslist[i].Second, DateTimeKind.Local);
    
    
        // if we're in session, do the following, else we are no longer in session and end the function
        if (sess_StartTime < current_dt && current_dt <= sess_EndTime)
        {
            int StartBar = CurrentBar - Bars.GetBar(sess_StartTime.AddMinutes(timeframe));
            double BarOpenPrice = Open[StartBar];
    
            // items for the line # non essential to code
            String tag = GenerateTodayTag(current_dt);
            SimpleFont TextFont = new SimpleFont("Verdana", 9);
    
    
            // the drawing of the lines themselves
    
            Draw.Line(this, " OL" + tag + nameslist[i], false, sess_StartTime.AddMinutes(timeframe), BarOpenPrice, sess_EndTime, BarOpenPrice, Brushes.White, DashStyleHelper.Solid, 1);
            Draw.Text(this, "OL Text" + tag + nameslist[i], false, nameslist[i] + " " + 12, sess_EndTime, BarOpenPrice, 12, Brushes.White, TextFont, TextAlignment.Right, null, null, 100);
        }
    
    
        if (sess_StartTime < current_dt && current_dt <= sess_EndTime)
        {
            return;
        }
    &#8203;​ 
    
    Attached Files
    Last edited by rickesh; 11-29-2023, 01:58 PM.

    #2
    Hello rickesh,

    The error is an invalid index error.

    An index you are using is invalid.

    Below is a link to a forum post on indexing errors.
    Hello, I want to create an indicator that show data in a chart but calculate in other charttime different to the time of the chart where is showed. For example:


    Any index used must be less than the size of the collection. For a series or a drawing object, any barsAgo index must be less than CurrentBar.

    Use prints to find the invalid index.

    Print the value of the index 'i' and print the value of CurrentBar and ensure that the value stored with 'i' is less than CurrentBar.
    Chelsea B.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    587 views
    0 likes
    Last Post Geovanny Suaza  
    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
    0 responses
    341 views
    1 like
    Last Post Geovanny Suaza  
    Started by Mindset, 02-09-2026, 11:44 AM
    0 responses
    103 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
    0 responses
    555 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    552 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X