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

Strategy Indicator Calculate Behavior

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

    Strategy Indicator Calculate Behavior

    I need my strategy to Calculate on price change but I need my indicator to update on bar closed. Can you clarify the calculation behavior of indicators in these use cases:

    1. Indicator is a class variable. Does it inherit the strategy calculation mode? If the indicator calculates on bar closed and strategy is on price changed will the indicator be updated "slower" than the strategy?

    2 Indicator is a strategy indicator. If the indicator is added via AddChartIndicator() does it inherit the strategy calculation mode?

    3. This one is a bit confusing. If I just call the indicator directly (not as a class variable or chart indicator) will its values be "out of date" until it gets called? How is it being calculated internally if it needs to hold some accumulation state in between calls?

    #2
    Hello jflaggs,

    Thank you for your post.

    Yes, the strategy will dictate the indicator calculate setting. You should use the strategy with Calculate.OnEachTick and put the logic in a condition with IsFirstTickOfBar.

    Please see the Help Guide link below which has a code snippet demonstrating:
    https://ninjatrader.com/support/help...ttickofbar.htm

    If you call the indicator directly, the value of the indicator is updated whenever you call the indicator (so on every pass of OnBarUpdate()).

    Please let me know if you have any other questions.
    Gaby V.NinjaTrader Customer Service

    Comment


      #3
      Sorry, that's not completely clear to me. Let me rephrase with an example. Suppose I have an indicator named Counter that just increments a class variable _counter++ on OnBarUpdate(). I'm looking for a way to have my strategy calculate on each tick but I do not want the internal "_counter" variable of the Counter indicator to be incremented automatically on each tick. In my strategy logic, I want to have control over the internal state of the Counter indicator whether that be using FirstTickOfBar or by invoking the Counter method directly in my strategy logic. But I *do not* want the Counter indicator to update at any time between calls from the strategy. I'm just trying to understand what is done automatically and what I have control over.

      By the way, the Calculate property of indicators are exposed with public get/set access which makes it seem like you have control over the calculation mode of indicators at the strategy level. Are you saying that the following code will not work?

      Code:
      private Counter _indicator;
      ...
      if (State == State.Configure)
      {
        // Set strategy to calculate each tick
        Calculate = CalculationMode.OnEachTick;
      
        // Set indicator to calculate on bar closed
        _indicator = Counter()
        _indicator.Calculate = CalculationMode.OnBarClosed;
      }

      Comment


        #4
        Hello,

        No, you cannot set the indicator calculate setting that way.

        From the Help Guide:

        "Embedded scripts within a calling parent script should not use a different Calculate property since it is already utilizing the Calculate property of the parent script (i.e. the strategy your indicator is called from)."

        "Since the parent NinjaScript therefore governs this setting, it should be set to the highest needed setting satisfying all its childs."

        Calculate - https://ninjatrader.com/support/help.../calculate.htm

        You can still separate the logic to either calculate OnBarClosed or OnEachTick. Please see the sample script linked below:

        https://forum.ninjatrader.com/forum/...tick#post19874

        Please let me know if you have any other questions.
        Gaby V.NinjaTrader Customer Service

        Comment


          #5
          Thank you. Then it seems as though wrapping the indicator logic in a IsFirstTickOfBar block is equivalent to OnBarClosed (maybe shifted by 1 tick)? Am I understanding that correctly? That is, if I use my Counter indicator example, the count will be the number of bars, rather than the number of ticks processed.

          Comment


            #6
            Hello,

            Yes, that is correct. You can do it that way.

            Please let us know if you have any other questions.
            Gaby V.NinjaTrader Customer Service

            Comment


              #7

              Code:
              private Counter _indicator;
              ...
              if (State == State.Configure)
              {
              // Set strategy to calculate each tick
              Calculate = CalculationMode.OnEachTick;
              
              // Set indicator to calculate on bar closed
              _indicator = Counter()
              _indicator.Calculate = CalculationMode.OnBarClosed;
              }
              [/QUOTE]

              Can you revisit this, is it missing a semicolon at the end of the _indicator = Counter() line?

              Comment


                #8
                This was meant to be pseudocode.

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by fx.practic, 10-15-2013, 12:53 AM
                5 responses
                5,403 views
                0 likes
                Last Post Bidder
                by Bidder
                 
                Started by Shai Samuel, 07-02-2022, 02:46 PM
                4 responses
                94 views
                0 likes
                Last Post Bidder
                by Bidder
                 
                Started by DJ888, Yesterday, 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, Yesterday, 09:29 PM
                0 responses
                8 views
                0 likes
                Last Post Belfortbucks  
                Working...
                X