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

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 Klaus Hengher, Yesterday, 03:13 AM
        2 responses
        15 views
        0 likes
        Last Post Klaus Hengher  
        Started by Sebastian - TwinPeaks, Yesterday, 01:31 PM
        2 responses
        13 views
        0 likes
        Last Post Sebastian - TwinPeaks  
        Started by wbennettjr, 07-15-2017, 05:07 PM
        16 responses
        2,528 views
        1 like
        Last Post eladlevi  
        Started by Human#102, Yesterday, 09:54 AM
        2 responses
        8 views
        0 likes
        Last Post Human#102  
        Started by Patlpp, 08-16-2021, 03:10 PM
        10 responses
        498 views
        0 likes
        Last Post Joerg
        by Joerg
         
        Working...
        X