Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Error on calling 'OnBarUpdate' method on bar -1

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

    Error on calling 'OnBarUpdate' method on bar -1

    Hi,

    I'm writing a very small and easy indicator and here is the code for OnBarUpdate

    Code:
    protected override void OnBarUpdate()
    { 
    RS[0] = (Closes[0][0] / Closes[1][0]) * 100;
    if (CurrentBar > 50)
    RS_MA[0] = SMA(RS, 50)[0];
    }
    I cannot understand how it is possible but I'm getting following error:
    Error on calling 'OnBarUpdate' method on bar -1

    I cannot even catch the Error by Visual Studio,
    How I can find were this is happening ?

    Thanks,
    Nima

    #2
    Originally posted by njafarynia View Post
    Hi,

    I'm writing a very small and easy indicator and here is the code for OnBarUpdate

    Code:
    protected override void OnBarUpdate()
    {
    RS[0] = ([COLOR=#e74c3c]Closes[0][0[/COLOR]] / [COLOR=#e74c3c]Closes[1][0][/COLOR]) * 100;
    if (CurrentBar > 50)
    RS_MA[0] = SMA(RS, 50)[0];
    }
    I cannot understand how it is possible but I'm getting following error:
    Error on calling 'OnBarUpdate' method on bar -1

    I cannot even catch the Error by Visual Studio,
    How I can find were this is happening ?
    Why are you using 'Closes[0][0]' and 'Closes[1][0]'?

    [I mean, these are legitimate things to use, but using
    'Closes[1][0]' implies you have added a secondary
    series -- and I doubt you're doing that.]

    Perhaps you probably meant to use 'Closes[0][1]',
    which is a much different thing that 'Closes[1][0]'.

    In fact,
    Instead of Closes[0][0] use Close[0].
    Instead of Closes[0][1] use Close[1],

    Comment


      #3
      Originally posted by njafarynia View Post
      I cannot understand how it is possible but I'm getting following error:
      Error on calling 'OnBarUpdate' method on bar -1


      When you use Closes[1][n], NinjaTrader assumes a bar series
      has been created to support use of 'Closes[1]' -- note that the
      plural forms of Open, High, Low, Close, etc, all take an index
      which directly corresponds to the order of the bar series added
      via the calls to AddDataSeries.

      By definition, Closes[1][n] accesses the 1st bar series created
      by the 1st call to AddDataSeries.

      By definition, Closes[2][n] accesses the 2nd bars series created
      by the 2nd call to AddDataSeries ... and so on.

      Note that Closes[0][n], by definition, is always accessing the
      primary bar series, which is always available.

      -=o=-

      So, why the error message?
      Probably because 1) either Closes[1] doesn't exist (because
      you've not setup a secondary bar series via AddDataSeries)
      and/or 2) there are no bars available on that series yet.

      For #2, this could happen if your primary data series is smaller
      time frame than the secondary data series. Consider that 59
      1-minute bars will arrive before the first 1-hour bar is even
      available. Until that first 1-hour bar is available, CurrentBars[1]
      will be -1.

      If you really are using a secondary data series, then you need
      some guard code, something like this,

      Code:
      protected override void OnBarUpdate()
      {
          if (BarsInProgress != 0 || CurrentBar < 50 || CurrentBars[1] < 0)
             return;
          RS[0] = (Closes[0][0] / Closes[1][0]) * 100;
          RS_MA[0] = SMA(RS, 50)[0];
      }
      Make sense?
      Last edited by bltdavid; 02-12-2022, 03:54 PM.

      Comment


        #4
        My Friend, you are the # 1
        I've followed your suggestion code and now it is good:

        Code:
        protected override void OnBarUpdate()
        {[INDENT]if (BarsInProgress != 0 || CurrentBar < 50 || CurrentBars[1] < 0)[/INDENT][INDENT=2]return;[/INDENT][INDENT]RS[0] = (Closes[0][0] / Closes[1][0]) * 100;
        RS_MA[0] = SMA(RS, 50)[0];[/INDENT]
          }

        In my original message I did not mention that I've added another data series into my code.
        Code:
        AddDataSeries("SPY", new BarsPeriod { BarsPeriodType = BarsPeriod.BarsPeriodType, Value = BarsPeriod.BaseBarsPeriodValue });
        Thanks for your help.
        Nima

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        608 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        355 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
        560 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        561 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X