Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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 0starttimeslist.Counti++)
    {


        
    // convert session time into a full DateTime for further use
        
    DateTime sess_StartTime = new DateTime(current_dt.Yearcurrent_dt.Monthcurrent_dt.Daystarttimeslist[i].Hourstarttimeslist[i].Minutestarttimeslist[i].SecondDateTimeKind.Local);
        
    DateTime sess_EndTime = new DateTime(current_dt.Yearcurrent_dt.Monthcurrent_dt.Dayendtimeslist[i].Hourendtimeslist[i].Minuteendtimeslist[i].SecondDateTimeKind.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], falsesess_StartTime.AddMinutes(timeframe), BarOpenPricesess_EndTimeBarOpenPriceBrushes.WhiteDashStyleHelper.Solid1);
            
    Draw.Text(this"OL Text" tag nameslist[i], falsenameslist[i] + " " 12sess_EndTimeBarOpenPrice12Brushes.WhiteTextFontTextAlignment.Rightnullnull100);
        }


        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 fx.practic, 10-15-2013, 12:53 AM
    5 responses
    5,404 views
    0 likes
    Last Post Bidder
    by Bidder
     
    Started by Shai Samuel, 07-02-2022, 02:46 PM
    4 responses
    95 views
    0 likes
    Last Post Bidder
    by Bidder
     
    Started by DJ888, Yesterday, 10:57 PM
    0 responses
    7 views
    0 likes
    Last Post DJ888
    by DJ888
     
    Started by MacDad, 02-25-2024, 11:48 PM
    7 responses
    159 views
    0 likes
    Last Post loganjarosz123  
    Started by Belfortbucks, Yesterday, 09:29 PM
    0 responses
    8 views
    0 likes
    Last Post Belfortbucks  
    Working...
    X