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

Caching Indicator instances for use in other Indicators

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

    Caching Indicator instances for use in other Indicators

    (Using NinjaTrader 7.0.0.22) My understanding is that if you have two indicators that both use a 3rd indicator with the same parameters, it is supposed to use the same instance of the indicator for efficiency. The generated code seems to try to do that. However... I just tried it and it didn't seem to work.

    Create IndicatorShared with this code in it:

    Code:
    		public System.Guid guid = System.Guid.NewGuid();
    Now create Indicator1 that uses IndicatorShared with something like:

    Code:
            IndicatorShared ind;
    
            protected override void OnBarUpdate()
            {
    			if (ind == null) {
    				ind = IndicatorShared();
    				ind.Update();
    				Print("Shared guid: "+ind.guid.ToString());
    			}
    	}
    Now create Indicator2 that uses IndicatorShared and has the same code above.

    Put Indicator1 and Indicator2 on a chart.

    Output:

    Shared guid: c685b595-f614-4685-9515-2ce0e502dea6
    Shared guid: 6131b9cd-73a9-4cf5-acbe-802ad6db4bc5

    It appears they are using two different instances. Why?

    #2
    Correct indicator instances are cached if they use the same parameters, however I'm not sure if your test code is valid to prove / disprove it - isn't this just a unqiue id generated with each call?
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Bertrand View Post
      Correct indicator instances are cached if they use the same parameters, however I'm not sure if your test code is valid to prove / disprove it - isn't this just a unqiue id generated with each call?
      No, the unique guid is created each time a new indicator is created. The output only shows up twice--one for each indicator created because of the two indicators added to the chart.

      The parameter list in this test is the same (none) so it should be cached, but it apparently is not.

      Comment


        #4
        taotree, we checked into and are not sure what you're trying to achieve with your custom code - the following snippet could be used to check instances working -

        protected override void OnBarUpdate()
        {
        if (mySMA == null)
        mySMA = SMA(14);
        else if (mySMA != SMA(14))
        Print("fresh instance");
        }


        In my testing here I never saw a print thus the indicator instances using same parameters are cached as expected by NT.
        BertrandNinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_Bertrand View Post
          taotree, we checked into and are not sure what you're trying to achieve with your custom code - the following snippet could be used to check instances working -

          protected override void OnBarUpdate()
          {
          if (mySMA == null)
          mySMA = SMA(14);
          else if (mySMA != SMA(14))
          Print("fresh instance");
          }


          In my testing here I never saw a print thus the indicator instances using same parameters are cached as expected by NT.
          You're doing this from the same Indicator. Try to access a third indicator from 2 other different indicators--that's what I'm testing. The reason I did the guid thing was just a way to differentiate between instances. In your code, you have both references right next to each other so can compare using ==. But to test with references from 2 different indicators, there is not a single place that has both references to do that == comparison. So... I have to put in some unique identifier with each instance of the shared indicator so I can compare them.

          If you give some thought to how you would do the test from two other indicators, I think you'll come up with something similar. I look forward to your findings.

          Thank you.

          Comment


            #6
            Thanks for your clarification - we looked into it again and your findings are accurate when calling indicators from different scripts.
            BertrandNinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_Bertrand View Post
              Thanks for your clarification - we looked into it again and your findings are accurate when calling indicators from different scripts.
              Is this something you plan to fix? And would it cache/singleton across charts as well, or only within the same chart?

              I have one very complex indicator that calculates things that I then have various other indicators that pull from it for display. Sharing that complex indicator efficiently is very important.

              I had expected that was the intended behavior. If not, I'll need to restructure my code so that I create my own singleton instead of relying on the indicator mechanism.

              Thank you.

              Comment


                #8
                Currently we have no plans to change the caching behavior you noted, if critical for you the custom singleton solution might be worth looking into.
                BertrandNinjaTrader Customer Service

                Comment


                  #9
                  Originally posted by NinjaTrader_Bertrand View Post
                  Currently we have no plans to change the caching behavior you noted, if critical for you the custom singleton solution might be worth looking into.
                  Where is your custom singleton solution available?

                  I have taken this from c# examples. The compilation error is "cannot find Print in the current context". It compiles fine without the Print statement.

                  #region Using declarations
                  using System;
                  using System.ComponentModel;
                  using System.Diagnostics;
                  using System.Drawing;
                  using System.Drawing.Drawing2D;
                  using System.Xml.Serialization;
                  using NinjaTrader.Cbi;
                  using NinjaTrader.Data;
                  using NinjaTrader.Gui.Chart;
                  using System.Drawing.Printing;

                  #endregion

                  /// <summary>
                  /// Sample singleton object.
                  /// </summary>
                  ///
                  ///

                  namespace NinjaTrader.Indicator
                  {
                  public sealed class Global
                  {
                  private int i = 0;
                  /// <summary>
                  /// This is an expensive resource we need to only store in one place.
                  /// </summary>
                  object[] _data = new object[10];

                  /// <summary>
                  /// Allocate ourselves. We have a private constructor, so no one else can.
                  /// </summary>
                  static readonly Global _instance = new Global();

                  /// <summary>
                  /// Access SiteStructure.Instance to get the singleton object.
                  /// Then call methods on that instance.
                  /// </summary>
                  public static Global Instance
                  {
                  get { return _instance; }
                  }


                  /// <summary>
                  /// This is a private constructor, meaning no outsiders have access.
                  /// </summary>
                  public Global()
                  {
                  i++;
                  Print (1);
                  // Initialize members, etc. here.
                  }
                  }
                  }


                  Any help from anyone?
                  Thank you.

                  Comment


                    #10
                    Global is not a subclass of Indicator, where Print() lives.

                    One way -- the easiest way -- to solve the problem is for the Global constructor to take an argument that is an indicator and save it. So an indicator might construct Global with "new Global(this);"

                    Alternatively, your static stuff can can create a helper class that is a subclass of Indicator and use that. There are a couple of gotcha's, though. I may have posted a demo of that in the unsupported thread. If not, contact me and I'll get you a copy of the demo.

                    The bottom line is that you need to access Print() through a subclass of Indicator. If "r" is such a subclass, then r.Print("This will work");

                    =====

                    Now that I take another look, try just making Global a subclass of Indicator. That path was not available to me since I was staying with purely statics, but since you are not similarly constrained then just making Global a subclass of Indicator should do it..

                    --EV
                    Last edited by ETFVoyageur; 01-26-2011, 10:42 PM.

                    Comment


                      #11
                      Originally posted by bjmoose View Post
                      Where is your custom singleton solution available?
                      I have taken this from c# examples. The compilation error is "cannot find Print in the current context". It compiles fine without the Print statement.
                      Any help from anyone?
                      Thank you.
                      I ended up moving on to other things and so didn't implement a singleton.

                      As for the Print in your code... that is because Print is only available inside the indicator, but you're writing code for something outside that, so the Print method is unavailable. You'll have to use some other mechanism for debugging.

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by prdecast, Today, 06:07 AM
                      1 response
                      4 views
                      0 likes
                      Last Post NinjaTrader_LuisH  
                      Started by Christopher_R, Today, 12:29 AM
                      1 response
                      14 views
                      0 likes
                      Last Post NinjaTrader_LuisH  
                      Started by chartchart, 05-19-2021, 04:14 PM
                      3 responses
                      577 views
                      1 like
                      Last Post NinjaTrader_Gaby  
                      Started by bsbisme, Yesterday, 02:08 PM
                      1 response
                      15 views
                      0 likes
                      Last Post NinjaTrader_Gaby  
                      Started by i019945nj, 12-14-2023, 06:41 AM
                      3 responses
                      60 views
                      0 likes
                      Last Post i019945nj  
                      Working...
                      X