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' on using Draw.Ray

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

    Error on calling 'OnBarUpdate' on using Draw.Ray

    Hello,

    I'm super new to NinjaTrader/Script so this may be a dumb question.

    Here's a snippet of my code:

    Code:
    protected override void OnBarUpdate()
    {
    Draw.Ray(this, "tag1", 10, 4808, 0, 4809, Brushes.LimeGreen); 
    }


    I got that line of code from https://ninjatrader.com/support/help...zontalline.htm , just slightly modified it so that it would be visible on my chart (current price is around 4813).

    The problem is that when I add this to my chart (a 5M MES MAR24), it displays nothing. And in the Control Center Log, I get the following error
    Indicator 'SupResIndicator': Error on calling 'OnBarUpdate' method on bar 0: 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.
    Code:
    protected override void OnBarUpdate()
    {
    Draw.HorizontalLine(this, "tag1", 4808, Brushes.Green);
    }


    I have no idea why this isn't working. Hopefully someone can point out what I'm doing wrong...

    Thank you,
    Jamie​​
    Attached Files

    #2
    Hello domiflichi,

    Thank you for your post.

    This is not a dumb question; it is a good question to help you see what your scripts are doing and get a deeper understanding of NinjaScript in general. I see in your Draw.Ray() method, you are seeing the startBarsAgo value to 10. The error is happening because when you apply a script to a chart, it runs historically on all bars of the chart. This means that when it starts on the very first bar, there are not 10 bars on the chart yet to draw a ray starting 10 bars ago and it throws the error. The following page shows how to add a check to make sure there are enough bars on the chart before taking an action, such as checking that there are at least 10 bars on the chart before drawing the ray:


    Please let us know if we may be of further assistance.
    Emily C.NinjaTrader Customer Service

    Comment


      #3
      Thank you Emily, that helped!

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by DJ888, Today, 10:57 PM
      0 responses
      6 views
      0 likes
      Last Post DJ888
      by DJ888
       
      Started by MacDad, 02-25-2024, 11:48 PM
      7 responses
      158 views
      0 likes
      Last Post loganjarosz123  
      Started by Belfortbucks, Today, 09:29 PM
      0 responses
      7 views
      0 likes
      Last Post Belfortbucks  
      Started by zstheorist, Today, 07:52 PM
      0 responses
      7 views
      0 likes
      Last Post zstheorist  
      Started by pmachiraju, 11-01-2023, 04:46 AM
      8 responses
      151 views
      0 likes
      Last Post rehmans
      by rehmans
       
      Working...
      X