Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

onbarupdate issues

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

    onbarupdate issues

    I know this is a common problem and has come up a lot on these forums but I'm
    still not sure what to put for currentbar count.

    The error msg in log is:

    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.


    i have added the daily data series to state.configure

    AddDataSeries(BarsPeriodType.Day, 1);


    and declared 2 doubles which are the previous days high and low:

    double pdh = Highs[1][1];
    double pdl = Lows[1][1];

    if(CurrentBars[0] < 5)
    return;

    '//trigger

    if(Close[0] > pdh)
    { pdh breakout alert}

    for the main data series will be using values under 1 day (usually 1m or 5m). Knowing this how to i need to configure the:

    if(CurrentBars[0] < 5)

    so that this indicator will function as intended. Please assist.
    return;
    Last edited by gordongekko; 12-07-2017, 05:32 PM.

    #2
    try this

    Code:
              
               double pdh ;
                double pdl ;
         OnBarUpdate:   
                if(CurrentBars[0] < 5)
                return;
                pdh = Highs[1][1];
                pdl = Lows[1][1];
                
    '//trigger
    
                if(Close[0] > pdh)
                { pdh breakout alert}

    Comment


      #3
      Hello gordongekko,

      Thanks for your post.

      You are adding a data series however you are not accounting for this in your CurrentBars check. CurrentBars[0] points to the charts bars, CurrentBars[1] points to the added dataseries.

      You are trying to access the previous day high/low with Highs[1][1] and Lows[1][1] after the 5 bars loaded on the chart bars which are 1m or 5m, this is not enough bars for the daily bars to load

      Try changing your CurrentBars check to something like:

      if (CurrentBars[0] < 5 || CurrentBars[1] < 1)
      return;


      The statement would need to be the first one in the OnBarUpdate() as you don't want to process anything below that line until you have the correct number of bars loaded.

      Reference: https://ninjatrader.com/support/help...urrentbars.htm

      Comment


        #4
        Thanks. This seems so obvious now. For some reason I was overlooking the [] which should have clued me into the fact that it was referencing that bar series from the array. I was thinking the 0 was the current bar of the data series and it was loading both.

        Is there a way to have this send out an alert only when the price closes above or below this line and not every bar that is above or below the previous days high without using a timer.

        if(Close[0] > pdh)
        { pdh breakout alert}

        When i use this the alert will be sent out at the close of every bar that is above the previous daily high but I only want the alert to go out on the first bar to close above the previous days high on the main data series. Can cross above/below be used for price like it can for an indicator? What is the best way to accomplish this?
        Last edited by gordongekko; 12-08-2017, 09:21 AM.

        Comment


          #5
          Hello gordongekko,

          Thanks for your reply.

          Yes, you can create a boolean variable that is true or false only and can test for that as well as set to what you need.

          You can use CrossAbove() or CrossBelow() as needed.

          For example:

          if (CrossAbove(Close, pdh, 1) && FirstCrossAbove)
          {
          // pdh breakout alert
          FirstCrossAbove = false; // set bool to false to prevent re-entry
          }


          You would also need to reset the bool to true at some point in time to again enable the condition.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          579 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          334 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          101 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          554 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          551 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X