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

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 jaybedreamin, Today, 05:56 PM
          0 responses
          7 views
          0 likes
          Last Post jaybedreamin  
          Started by DJ888, 04-16-2024, 06:09 PM
          6 responses
          18 views
          0 likes
          Last Post DJ888
          by DJ888
           
          Started by Jon17, Today, 04:33 PM
          0 responses
          4 views
          0 likes
          Last Post Jon17
          by Jon17
           
          Started by Javierw.ok, Today, 04:12 PM
          0 responses
          12 views
          0 likes
          Last Post Javierw.ok  
          Started by timmbbo, Today, 08:59 AM
          2 responses
          13 views
          0 likes
          Last Post bltdavid  
          Working...
          X