Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

CPU efficiency question re calls to Indicators

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

    CPU efficiency question re calls to Indicators

    Hi,
    just wondering about when a Strategy has to make repeated calls to Indicator(s). Does it make a difference in CPU efficiency if the indicator is Added in Initialise(), rather than just called?
    I presume Indicators (some of them at least) need to calculate all the way from bar 0 to know what the current value is. If Indicators had to start fresh each time, rather than being retained in memory, that would be a lot of work.
    If there is a way of avoiding that, please let me know.
    Cheers,
    saltminer

    #2
    Hi saltminer, the Add() method is used to plot the indicators from the strategy, if you just need the indicator values for your trading logic it would be best to call to them...

    Comment


      #3
      Thank you Bertrand, but I know that. I'm quite an advanced programmer, no need for basics here. Please answer my question.
      It comes down to whether Ninja retains internally a DataSeries of any indicators called by a Strategy, or does it throw them away?
      If it does throw them away, does it do it immediately during OnBarUpdate() or wait til the end of OnBarUpdate()? That would make a big difference if, for instance, we were referencing something like Stochastic() many times during a single OnBarUpdate().

      Comment


        #4
        Originally posted by saltminer View Post
        Thank you Bertrand, but I know that. I'm quite an advanced programmer, no need for basics here. Please answer my question.
        It comes down to whether Ninja retains internally a DataSeries of any indicators called by a Strategy, or does it throw them away?
        If it does throw them away, does it do it immediately during OnBarUpdate() or wait til the end of OnBarUpdate()? That would make a big difference if, for instance, we were referencing something like Stochastic() many times during a single OnBarUpdate().
        I think the most efficient way to do it is call the indicator once and set the value to a variable with the variable being defined inside the OnBarUpdate(). If I need the values of that last 10 bars of Stochastics then I reference the indicator once and set all the values to individual variables or list or arrays depending on the size. I remember trying a little experiment a while ago this seemed to produced the best results. There may be another way that I am not familiar with but that is the way I currently do it.

        Comment


          #5
          Guys, please stick with calling e.g. Stochastics(...) as advised by all our docs.

          Different approaches like assigning it to a variable are not supported. They may or may not work. You'd need to check it out by yourself (which certainly is not a hard thing to do for an advanced programmer) and be aware of potential ramifications. However, we do not provide any support for that.

          Comment


            #6
            TAJTrades

            I do the same thing ...assuming NT will run faster ...especially for CalculateOnBarClose = false;...i.e. every tick....and especially at 500x in MarketReplay

            It seems logical to me that if I pick up the value from an indicator call once in a variable at the start of OnBarUpdate and reference that variable numerous times...instead of calling and therefore calculating the value numerous times....must be faster.

            Dierk

            1. if my assumption is false and it is not faster, please explain and I will stop doing it. I am trying to optimize 12,000 lines of code while waiting for NT7's multi-core capability

            2. Please explain your last post. I do not understand why NT would not support:

            private double stochValue = 0;

            stochValue = Stochastic(....);

            if (stochValue.....)
            if (stochValue.....)
            if (stochValue.....)
            if (stochValue.....)

            Comment


              #7
              Unfortunately we do not provide support for that approach.

              Comment


                #8
                Dierk, I am aware that NT does not officially support this method and can accept that. Heck, half of the stuff I do would not be officially supported. That is the main reason why I do not post indicators (NT will not support it and I don't want too). But then that does make it more fun! Kind of like seeing a sign on a door that says "Do Not Enter" well you have to Enter LOL.

                ATI user
                Just make sure you do extensive checking of the variables. I have not run into a problem doing it this way but that does not mean that a problem will not occur. I have found this way to be most effective when I am writing pattern recognition code that has a large number branches where I need the same variable many times to determine the next branch.

                Good luck

                Comment


                  #9
                  I must be missing something painfully obvious here.

                  I responded to this thread because, like saltminer, I was curious re time/resources wasted making repeated calls to the same indicator on the same tick

                  For 2 1/2 years with NT I have been picking up the indicator value in a variable and using that value multiple times on the same tick...without a problem. Logically it must be faster to repeatedly access a variable value than to repeatedly call the indicator which must calculate the value on each call.

                  As far as accuracy goes, the variable value and the value from repeated calls, all on the same tick, are identical to 11 decimal places....so where is the risk?...and why is this method 'not supported'?

                  Unless I am missing something, it could not be simpler nor more basic. Is there something that goes on behind the scene in NT of which I am not and should be aware?

                  Comment


                    #10
                    @ATI user: Unfortunately there is no more information available on the matter than: it's not supported. It may or may not work for you. You would need to try on your own and be aware that upcoming NT releases may or may not break any code which we consider as "not supported".

                    Thanks for your understanding.

                    Comment


                      #11
                      Originally posted by ATI user View Post
                      Unless I am missing something, it could not be simpler nor more basic. Is there something that goes on behind the scene in NT of which I am not and should be aware?
                      I think what you, I and others are doing can be considered advanced programming by many NT users who do not have advanced programming skills or any programming skills. So to keep the support issues as basic as possible for the majority of NT users they have chosen to "Not Support" anything that may be considered advanced. If I were in charge I would make the same decision. With C# if you can think it it can be done. Actually doing it is a different matter (for me at least). NT has to do what is best for the user community as a whole. C# programmers will find their own way.

                      No harm, no fowl. Thanks NT for a great product.

                      AND HURRY UP WITH 7.0!

                      Comment


                        #12
                        ok...fair enough

                        we have beat this topic to death anyway...

                        I will check each subsequent release of NT to see if somehow an indicator value can change during the same tick...if it can, I will change my code accordingly

                        Comment


                          #13
                          Thanks guys for all your input.
                          What I have been doing so far for a strategy that was doing a lot of pattern recognition across oscillator data was to call the oscillator once to get the value and put it into my own DataSeries.
                          Then all the times I needed that data, and also data from previous bars, it was just a simple access away, no computation involved.
                          That has simply GOT to be more efficient. (Not for RAM though, but I don't think that's a big deal unless we are talking years of 1 minute data.)
                          For the record, I don't understand why Ninja would not support the use of variable var after the following code:
                          var = Stochastics(...)[n];
                          But for sure we all know that will work, so let's get on with it.

                          Ultimately, though, the Ninja guys have not answered my original question about Ninja recalculating or retaining the values internally. Perhaps they don't know.
                          saltminer

                          Comment


                            #14
                            Hello,

                            Someone will respond to your post on Monday. Sorry for the delay.
                            DenNinjaTrader Customer Service

                            Comment


                              #15
                              Originally posted by saltminer View Post
                              Thanks guys for all your input.
                              What I have been doing so far for a strategy that was doing a lot of pattern recognition across oscillator data was to call the oscillator once to get the value and put it into my own DataSeries.
                              Then all the times I needed that data, and also data from previous bars, it was just a simple access away, no computation involved.
                              That has simply GOT to be more efficient. (Not for RAM though, but I don't think that's a big deal unless we are talking years of 1 minute data.)
                              For the record, I don't understand why Ninja would not support the use of variable var after the following code:
                              var = Stochastics(...)[n];
                              But for sure we all know that will work, so let's get on with it.

                              Ultimately, though, the Ninja guys have not answered my original question about Ninja recalculating or retaining the values internally. Perhaps they don't know.
                              saltminer
                              I'm not entirely sure I understand your issue but here is some info:
                              a) you certainly can access any past data point of any indicator you're using in your custom strategy/indicator like SMA(14)[5] (which would be the SMA value 5 bars back)
                              b) you could do the same for custom data series, since they would maintain their "past" values: http://www.ninjatrader-support2.com/...ead.php?t=7299
                              c) of course the "barsAgo" logic would not be applicable to simple C#/NinjaScript variables like "double", "int": you would need to resort back to (b) above

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              656 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              371 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              109 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              574 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              579 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X