Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Clarification on Indicator cache and efficiency

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

    Clarification on Indicator cache and efficiency

    Please find attached the Strategy TestStrategy with Indicators TestIndHost, & TestIndChild.

    My question is whether the approach used in TestIndChild (i.e. “ShortForm” operation) is in fact less efficient.

    TestIndChild includes logic whereby depending where the Indicator is called from it calculates differently. Where TestIndChild is called from a Chart or TestStrategy, it will calculate “ShortForm” and “LongForm” components. Where TestIndChild is called from any other Indicator (e.g. TestIndHost), it will calculate “ShortForm” only (i.e. it will not calculate “LongForm”).

    So, my question revolves around the behaviour where the cache is created in the NinjaScript auto-generated code under NinjaScript.Indicators and NinjaScript.Strategies. As I understand it, where TestStrategy is enabled, cacheTestIndChild would contain two elements, one where IsCalledFrom = “TestStrategy”, and one where IsCalledFrom = “TestIndHost”. This would result in both elements processing OnBarUpdate and thus being less efficient relative to the Indicator with “IsCalledFrom” and “ShortForm” logic removed. That is, where the argument and "ShortForm" logic is removed, cacheTestIndChild would only have one element processing OnBarUpdate.

    Please confirm/correct my understanding.
    Attached Files

    #2
    Hello Shansen,

    Thank you for your reply.

    It sounds like you're referring to assigning the indicator to a variable vs. calling it explicitly each time you want to reference it, and really, it doesn't change anything. NinjaTrader automatically caches indicators that use the same parameters in the method calls.

    However, for convenience it's nice to refer to the variable instead of calling the indicator method over and over.

    Further, the generated code at the bottom is also not involved with how NinjaTrader internally caches indicators in memory. That generated code at the bottom is for making overload parameters for indicators being called.

    Please let us know if we may be of further assistance to you.

    Comment


      #3
      Kate,

      Thanks for your reply.

      My question is not around assigning an indicator to a variable, for example in TestStrategy (below).
      Code:
      else if (State == State.Historical)
      {
       /* https://ninjatrader.com/support/helpGuides/nt8/en-us/ninjascript_best_practices.htm
       * To improve performance, save instances Indicators during State.Historical. */
       _testIndHost = TestIndHost(isCalledFrom: Name, period: Period);
       _testIndChild = TestIndChild(isCalledFrom: Name, period: Period);
      }
      My question is around the auto-generated code in TestIndChild. Specifically, TestIndChild[] cacheTestIndChild (below).
      If my understanding is correct, by adding the parameter "IsCalledFrom" to TestIndChild, when TestStrategy is enabled cacheTestIndChild will have two elements:
      1. an instance of TestIndChild(isCalledFrom: "TestStrategy", period: 8)
      2. an instance of TestIndChild(isCalledFrom: "TestIndHost", period: 8)

      Where the "ShortForm" logic and "IsCalledFrom" parameter is removed, cacheTestIndChild will have one element:
      1. an instance of TestIndChild(period: 8)

      If the above is correct, while the "ShortForm" logic was an attempt to improve performance. It would in fact reduces performance, by increasing the number of instances of TestIndChild.
      Code:
      namespace NinjaTrader.NinjaScript.Indicators
      {
       public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
       {
        private TestIndChild[] cacheTestIndChild;
        public TestIndChild TestIndChild(string isCalledFrom, int period)
        {
         return TestIndChild(Input, isCalledFrom, period);
        }
      
        public TestIndChild TestIndChild(ISeries<double> input, string isCalledFrom, int period)
        {
         if (cacheTestIndChild != null)
          for (int idx = 0; idx < cacheTestIndChild.Length; idx++)
           if (cacheTestIndChild[idx] != null && cacheTestIndChild[idx].IsCalledFrom == isCalledFrom && cacheTestIndChild[idx].Period == period && cacheTestIndChild[idx].EqualsInput(input))
            return cacheTestIndChild[idx];
         return CacheIndicator<TestIndChild>(new TestIndChild(){ IsCalledFrom = isCalledFrom, Period = period }, input, ref cacheTestIndChild);
        }
       }
      }

      Comment


        #4
        Hello Shansen,

        Thank you for your reply.

        Indicator caching ONLY occurs when an indicator within the same host script/window is recalled with the same EXACT parameters and input. (i.e. when a previously called indicator is called a second time with new parameters, a second instance will be created / cached)

        The full indicator is cached (everything in the class) not just plots.

        If you're calling the indicator essentially twice, once directly from the strategy and once from the host indicator called from the strategy, with different parameters, yes, a second instance would be created and cached.

        Please let us know if we may be of further assistance to you.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        628 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        359 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        105 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        562 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        568 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X