Announcement

Collapse
No announcement yet.

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.

    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.

        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.

            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 NullPointStrategies, Yesterday, 05:17 AM
                0 responses
                54 views
                0 likes
                Last Post NullPointStrategies  
                Started by argusthome, 03-08-2026, 10:06 AM
                0 responses
                130 views
                0 likes
                Last Post argusthome  
                Started by NabilKhattabi, 03-06-2026, 11:18 AM
                0 responses
                70 views
                0 likes
                Last Post NabilKhattabi  
                Started by Deep42, 03-06-2026, 12:28 AM
                0 responses
                44 views
                0 likes
                Last Post Deep42
                by Deep42
                 
                Started by TheRealMorford, 03-05-2026, 06:15 PM
                0 responses
                49 views
                0 likes
                Last Post TheRealMorford  
                Working...
                X