Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

AddChartIndicator() requirement of accessing indicator in OnBarUpdate()?

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

    AddChartIndicator() requirement of accessing indicator in OnBarUpdate()?

    Hello,

    At the very bottom of the "AddChartIndicator()" method documentation at https://ninjatrader.com/support/help...tindicator.htm it states:

    Tip: If you are adding an indicator which is dependent on the correct State of the indicator, you will need to ensure that you are also calling the indicator from the strategy in OnBarUpdate(), otherwise your indicator will only process in State.RealTime for performance optimizations.
    Code:
    protected override void OnStateChange()
    {
      if (State == State.DataLoaded)
      {
        // Charts a 20 period simple moving average to the chart
        AddChartIndicator(SMA(20));
      }
    }
     
    protected override void OnBarUpdate()
    {   
      // call SMA() historically to ensure the indicator processes its historical states as well
      double sma = SMA(20)[0];
    }
    Questions:

    Question #1: Is the problem mentioned above (that is, "otherwise your indicator will only process in State.RealTime for performance optimizations") merely a visual problem, or could it cause the indicator values to be wrong in the code of a Strategy? Is this a dangerous problem for NinjaScript programs that place trades?

    Question #2: To avoid the problem mentioned above, does the indicator have to be used in every single OnBarUpdate() call, even the first one (where CurrentBar == 0), or does it only need to be called once in the lifetime of the strategy? If it only needs to be called once, does it matter on which bar, or must it be the first one? In the example from the documentation above it is called on every bar update, even though an indicator might not be valid until the 20th bar.

    Thank you in advance!

    EquityTrader

    #2
    Hello Equity Trader,

    Yes, this could affect your values, however in your posted example you resolve this by calling the indicator in OnBarUpdate.

    You would want to call the indicator on each bar for it to update historically.
    I have submitted a feature request to change the example in the AddChartIndicator section of the helpguide to better explain this point as well as a suggestion to use an example other than then the SMA to demonstrate this.

    I have attached a strategy and indicator which might better explain this point. Apply the strategy with the output window open, then comment out line 68 of the strategy and rerun it with the output window open. I have provided a screen shot showing you the difference in results.

    Please let us know if you need further assistance.
    Attached Files
    Alan P.NinjaTrader Customer Service

    Comment


      #3
      Hello NinjaTrader_AlanP,

      Thank you very much for the helpful explanation and the code, and the time you took to explain this. I downloaded and tried it, and now have a real example of this potential source of bugs in every NinjaScript strategy that uses an indicator.

      This problem worries me.

      NinjaScript programmers literally have to look into the internals of the indicators they use, and if the indicator depends on its state, the NinjaScript programmer has to add a line like "double sma = SMA(20)[0];" on every bar update. If I understand correctly, its not enough to use the indicator in most bar updates or some bar updates, which means that an indicator's values could be incorrect if an indicator with this problem is used anywhere that execution might not reach on every bar update, such as:
      1. If it is used in an inner "if" condition that is only hit when the outer "if" condition is true).
      2. If it is used after any "return" statement, including one that checks for a sufficient number of bars to begin processing: (if (CurrentBar < BarsRequiredToTrade) return; )


      Thank you for submitting the feature request to change the example in the AddChartIndicator section of the helpguide to better explain this point as well as a suggestion to use an example other than then the SMA to demonstrate this.

      Is there any possibility that the NinjaTrader product team could fix this problem internally, so that NinjaScript programmers such as myself don't need to constantly worry about whether or not we need to write a line like "double sma = SMA(20)[0];" that does nothing but ensure that an indicator that may depend on its state doesn't return bad values?

      I sincerely ask this out of concern and not as an insult.

      Thank you again for your time and help,

      EquityTrader

      Comment


        #4
        Hello EquityTrader,

        I will pass your suggestion on to development in a feature request.

        Please let us know if you need further assistance.
        Alan P.NinjaTrader Customer Service

        Comment


          #5
          Hello NinjaTrader_AlanP,

          Thank you very much for passing my suggestion on to development in a feature request.

          Also, I will keep an eye out for when the page at https://ninjatrader.com/support/help...tindicator.htm gets updated with a more detailed explanation of this issue.

          Hopefully it just gets fixed behind the scenes however.

          Thank you again for considering my input. Ninjatrader 7 was a big improvement on NinjaTrader 6.5, and NinjaTrader 8 is another big improvement on NinjaTrader 7. The more sources of bugs, such as the one I've described above, that your product team can remove, the fewer people that will get stuck on a NinjaScript problem and give up on the NinjaTrader platform altogether. NinjaScript is already easy compared to many other trading platforms' languages, and the easier it becomes, the more people will choose the NinjaTrader platform and stick with it for the long-term.

          Best regards,

          EquityTrader

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by condorhero, Today, 09:28 PM
          0 responses
          5 views
          0 likes
          Last Post condorhero  
          Started by MaraAnkh, Today, 08:44 PM
          0 responses
          6 views
          0 likes
          Last Post MaraAnkh  
          Started by wooofad, Today, 05:38 PM
          0 responses
          7 views
          0 likes
          Last Post wooofad
          by wooofad
           
          Started by caacapital, Today, 10:10 AM
          2 responses
          18 views
          0 likes
          Last Post caacapital  
          Started by DiverG20, Today, 10:23 AM
          0 responses
          23 views
          0 likes
          Last Post DiverG20  
          Working...
          X