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

BarsRequiredToPlot bug when an Indicator is accessed from a Strategy

Collapse
X
 
  • 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:
    Code:
            protected override void OnBarUpdate()
            {
                if (BarsInProgress != 0)
                    return;
    
                if (CurrentBar == 0)
                {
                    BarsRequiredToPlot = 435;
                    return;
                }
    
                ... Other processing
    
            }
    Even though that works, something is obviously wrong.

    #2
    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

    Comment


      #3
      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.

      Comment


        #4
        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.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by terofs, Yesterday, 04:18 PM
        1 response
        21 views
        0 likes
        Last Post terofs
        by terofs
         
        Started by CommonWhale, Today, 09:55 AM
        1 response
        3 views
        0 likes
        Last Post NinjaTrader_Erick  
        Started by Gerik, Today, 09:40 AM
        2 responses
        7 views
        0 likes
        Last Post Gerik
        by Gerik
         
        Started by RookieTrader, Today, 09:37 AM
        2 responses
        12 views
        0 likes
        Last Post RookieTrader  
        Started by alifarahani, Today, 09:40 AM
        1 response
        7 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Working...
        X