• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.


No announcement yet.

Partner 728x90


BarsRequiredToPlot bug when an Indicator is accessed from a Strategy

  • Filter
  • Time
  • Show
Clear All
new posts

    BarsRequiredToPlot bug when an Indicator is accessed from a Strategy

    This is posted under Strategy Development, because the error I am about to explain only occurs when an Indicator is accessed from a Strategy.

    I have developed an unusual proprietary Indicator that works well and I am currently coding a Strategy to trade automatically based on its indications. Despite not having an error message during extensive testing of the finished Indicator attached directly to a chart, when accessed from a Strategy there was a startup error message complaining that the code was trying to reference 5 bars back with only 4 bars loaded.

    The indicator only accesses 5 bars back, but needs to analyze patterns over a several hundred historic bars to be able to make reliable market indications. The number of historic bars needed depends complexly on various user settings, so I initially calculated and set a suitable BarsRequiredToPlot value in State.Configure rather than making the value a user setting in State.SetDefaults.

    I thought calculating and setting the value that way might have something to do with the error when the Indicator was accessed from a Strategy, so I commented-out the setting in State.Configure, made BarsRequiredToPlot a user-setting in State.SetDefaults, and passed a suitable value from the Strategy. That didn't change anything. I still had the error message.

    Then I put a Print statement in the indicator to print the BarsRequiredToPlot value and learned that when the Indicator was accessed from a Strategy the BarsRequiredToPlot value was always 20, regardless of the value passed from the Strategy or even hard-coded locally in the Indicator! However, when the Indicator was attached directly to a chart the printed BarsRequiredToPlot value was always whatever value I set and there was no error message.

    Another strange fact is that when the Indicator is attached directly to a chart it runs without an error message with any BarsRequiredToPlot value greater than 4, but when accessed from a Strategy there is the error message even though the printed BarsRequiredToPlot value is 20 ???

    I found experimentally that I could avoid the error and make the indicator work normally when accessed from a Strategy by setting a suitable BarsRequiredToPlot value in OnBarUpdate(), instead of in State.SetDefaults, like this:
            protected override void OnBarUpdate()
                if (BarsInProgress != 0)
                if (CurrentBar == 0)
                    BarsRequiredToPlot = 435;
                ... Other processing
    Even though that works, something is obviously wrong.

    Hello caveat_lector,

    Thanks for your post.

    Strategies have the ability to plot as well, and when an indicator is added, the indicator will inherit the BarsRequiredToPlot property from the strategy. Setting this in the indicator's OnBarUpdate method or in the indicator's OnStateChange method on State.DataLoaded can have the indicator force its own BarsRequiredToPlot value.

    I have attached a test strategy and indicator that can illustrate.

    Please let us know if there is anything else we can do to help.
    Attached Files
    JimNinjaTrader Customer Service


      Thanks, I will try that.

      I was mislead by the methods and properties listed under NinjaScript > Language Reference > Strategy in the Help which include BarsRequiredToTrade, but not BarsRequiredToPlot. However, with more careful reading I see that the statement at the top is "The methods and properties covered in this section are unique to custom strategy development" which doesn't exclude the methods and properties of indicators. Mention there that strategies additionally support Indicator methods and properties would help avoid misinterpretation.


        Yes, setting BarsRequiredToPlot in my indicator's OnStateChange method on State.DataLoaded causes it to run correctly without error either attached to a chart or accessed from a Strategy.


        Latest Posts


        Topics Statistics Last Post
        Started by White Fork, Today, 04:56 PM
        0 responses
        Last Post White Fork  
        Started by mkoning, Today, 04:20 PM
        0 responses
        1 view
        Last Post mkoning
        by mkoning
        Started by itrader46, Today, 02:59 PM
        3 responses
        Last Post NinjaTrader_Jesse  
        Started by madams212121, Today, 12:10 PM
        13 responses
        Last Post NinjaTrader_Jim  
        Started by jvanelli, Today, 12:19 PM
        1 response
        Last Post NinjaTrader_BrendanC