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 "On BarUpdate" Indicator

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

    Error on calling "On BarUpdate" Indicator

    HI,

    I'm trying to write a simple indicator, that compares the current bar high value with the previous bar high value as follows:
    if (High[0]<High[1])
    {
    res=100;
    }

    However, I'm getting the run time error: Error on Calling OnBarUpdate....on bar 0. You are accessing an index with a value that is invalide since its out of range.
    1. Can someone explain why I get that error message?
    2. Is there a way to know how many bars are loaded into the cart (i.e. In case I need to look back to find whether a condition existed in the past).

    Thank you.



    #2
    Hello,

    it sounds like you're already on the right track for identifying the error.

    You will want to add a CurrentBar check at the start of your OnBarUpdate() to ensure enough bars have been loaded.



    Let me know if I can be of further assistance.
    LanceNinjaTrader Customer Service

    Comment


      #3
      Thank you for the quick response.
      As you suggested, I checked the value of CurrentBar and is really = 0.
      However, I don't really understand why.
      My Chart contain way more the 0 bars.
      Any idea?


      Adding later....:
      Ok...I think I understand now.
      I need to add the condition:
      if (CurrentBar<2)
      return....
      Last edited by rmaron; 06-12-2013, 04:29 PM.

      Comment


        #4
        This is because when you add an indicator to a chart it will begin calculating from bar 0 and load each bar in 1 at a time until it reaches the current bar.

        Adding the return is beneficial in many scripts that require more data to be loaded before calculating.
        LanceNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by gentlebenthebear, Today, 01:30 AM
        2 responses
        13 views
        0 likes
        Last Post gentlebenthebear  
        Started by Kaledus, Today, 01:29 PM
        2 responses
        7 views
        0 likes
        Last Post Kaledus
        by Kaledus
         
        Started by frankthearm, Yesterday, 09:08 AM
        13 responses
        45 views
        0 likes
        Last Post frankthearm  
        Started by PaulMohn, Today, 12:36 PM
        2 responses
        16 views
        0 likes
        Last Post PaulMohn  
        Started by Conceptzx, 10-11-2022, 06:38 AM
        2 responses
        55 views
        0 likes
        Last Post PhillT
        by PhillT
         
        Working...
        X