Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Compiler efficiency when calling (including) local Indicators and Strategies?

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

    Compiler efficiency when calling (including) local Indicators and Strategies?

    Hi,

    When calling calling (including) existing local Indicators and public functions in Strategies from within an Indicator or Strategy under development is the NinjaTrader compiler so efficient at removing all redundancies and wastefulness that there is no CPU or memory management benefit to manually collapsing the logic from multiple indicators into one cs file prior to compilation?

    Or we see performance and memory management gains by collapsing as much clean logic as we can to a single CS file prior to compilation?

    Thanks!

    #2
    Hello hedgeplay,

    Thanks for the post.

    The platform does have some optimizations in place, I would likely need to know a more specific example here to know if that would apply. One area where you will see an optimization is surrounding indicators as there is a cache. If you call the same indicator twice it will utilize the cache, the same meaning that all the parameters and data are the same. Otherwise if you are doing extra work the platform is not going to change your code to be more efficient, it will just compile the code into a DLL so it can be loaded and run it.

    If you are worried about performance and you see that calling some indicator takes a toll you could review if that is possible to reduce or otherwise incorporate the calculation into your strategy. In some cases you can extract what an indicator is doing to get just the math from it which avoids any costly drawing or other things the indicator may be doing. This really depends on the situation, calling most indicators is not a costly procedure but can be dependent on the indicator and what data requirements/other processing it is doing.
    .


    I look forward to being of further assistance.

    Comment


      #3
      Jesse,

      Thanks for the reply.

      The cache looks like is a great feature.


      A few more questions to follow.

      First on the cache.

      If I first call a 200 period SMA of the primary data series and then immediately call for a 50 period SMA against the same data series will the 50 SMA request be fulfilled from the cache?


      The example below and other guide statements say that it is required to load the same data series, bar type and period into both the hosting strategy, and the indicator or strategy being called.

      1. Is loading the same data series in both ALWAYS really required? (Somewhere I saw a forum post saying it was just a recommendation)?

      2. Going toward "How efficient is the market data caching on the client?" ... When referencing the same exact data series from three indicators or strategies are all three just operating against pointers to the same exact shared memory or do the strategies / indicators need to always or sometimes need to create and manage in memory a unique additional copy data series?

      If the answer is "sometimes" how will I know when a unique copy will be required (Common Wisdom? Refereces? Examples/Samples)?

      Thanks!


      HTML Code:
      https://ninjatrader.com/support/helpGuides/nt8/?adddataseries.htm
      
      protected override void OnStateChange()
      {
         if (State == State.Configure)
         {
             // Our hosting script needs to have the AddDataSeries call included as well, which the Pivots indicator we call in the 2nd statement below
      
              // also has per default in it's own State.Configure method. This is required since our Pivots indicator below is created in State.DataLoaded           // (which is happening after State.Configure and it depends on the AddDataSeries call to have the bars available to properly calculate in
      
              // daily bars mode.
             AddDataSeries(BarsPeriodType.Day, 1);
         }
      
         else if (State == State.DataLoaded)
         {
             //In this state, we pass the 1 day series to the Pivots indicator (as BarsArray[1]) and create its instance
             pivots = Pivots(BarsArray[1], PivotRange.Weekly, HLCCalculationMode.DailyBars, 0, 0, 0, 20);
         }
      }  
      .

      Comment


        #4
        Hello hedgeplay,

        Thanks for the post.

        In your example because you used different parameters for the indicator a new instance of the indicator would be generated for each new set of parameters you used.

        The AddDataSeries is required for any situation where an indicator loads data, the host would also need to load that data and that is not a suggestion that will cause an error.

        Going toward "How efficient is the market data caching on the client?" ... When referencing the same exact data series from three indicators or strategies are all three just operating against pointers to the same exact shared memory or do the strategies / indicators need to always or sometimes need to create and manage in memory a unique additional copy data series?
        This really depends on the context of where those items were loaded from. If you mean they are all in the same chart, they would all share the charts dataseries. If they are loading in 3 different locations you may have 3 different dataseries being used. You really shouldn't need to worry about this type of memory management in your script, all you will need to do is make sure your logic makes sense and is efficient. If you are doing something that is taking a lot of resources we would generally suggest to make a simple example that shows what you are doing so that we can see what code does that to make suggestions.


        I look forward to being of further assistance.



        Comment


          #5
          Thanks Jesse.


          Originally posted by NinjaTrader_Jesse View Post
          Hello hedgeplay,

          "Going toward "How efficient is the market data caching on the client?" ... When referencing the same exact data series from three indicators or strategies are all three just operating against pointers to the same exact shared memory or do the strategies / indicators need to always or sometimes need to create and manage in memory a unique additional copy data series?"


          This really depends on the context of where those items were loaded from. If you mean they are all in the same chart, they would all share the charts dataseries. If they are loading in 3 different locations you may have 3 different dataseries being used. ...
          I will use multiple time frame dateseries and dataseries for a number of indexes and additional instruments. Dev work thus far has made it clear I need to get very serious about designing for performance and good memory management.

          "This really depends on the context of where those items were loaded from. If you mean they are all in the same chart, they would all share the charts dataseries"

          I expect to run a non-production version chart for developing and behavioral monitoring but what I am trying to design now is a strategy implementation that is as fast, reliable and low on memory usage as I can get it, enabling the top level strategy chart-less via the Strategies tab in the Control Center.

          I am just trying to as best I can choose a good design path at this stage. I can collapse quite a bit of logic into one .cs file the benefits (speed, less memory, lack of/fewer Async issues) justify the additional coding complexity throughout to deal array addressing for multiple data series, which series is driving this OnBarUpdate, more careful planning to throttle processing of algos to just as frequently as needed.

          Or I could spread the logic out to eight far simpler files, but a crowd of files to manage, and a crowd that may or may not be running more independently across multiple processors. Being new to this platform I am looking for all the NinjaTrader and NinjaScript design and trade-offs insight I can get before I spend days heads down on code.

          Thoughts? Tips? Awesome examples to send me to review?

          Thanks!


          Comment


            #6
            Hello hedgeplay,

            I believe you are reading too far into designing for performance here, you really don't need to look this deeply into the issue unless you are seeing a serious performance impact with something you are already doing. There are certain ways to do things which we suggest, that is the reference samples, however outside of that much of the efficiency would come from your own design of your C# code based on the task you are doing.

            If you are not certain on how to design a concept for efficiency I would suggest to put together a very specific example of what you wanted to do and just ask that specifically. For example something as simple as "How can I do X math in an indicator and have a strategy read the value" would be fine, we could help to point at samples for that. Otherwise I would suggest to just leave everything you are using as is and test it to see what kind of resources that uses to get a better idea of what might need to change.

            The platform will worry about managing the other resources efficiently like what has subscribed to data and what needs cached. If you want to invest here I would suggest to use external C# education resources to make sure you know C# well so you can design efficient code, NinjaScript is just C# so you can use any C# syntax which would make the job you are doing efficient. The platform is just executing your code for the various events so you want your code to be efficient if it should run fast.

            I would suggest looking over the educational resources section in the help guide for some examples of the "right way" to do tasks we have documented. You can also take a look at the user provided content on the user app share, while some of those may be hugely inefficient there are some items which explore concepts in very good ways that you can learn from. Again having a good backing of C# here will help to weed out the items that won't help with learning.



            I look forward to being of further assistance.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            668 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            377 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            110 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            575 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            580 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X