Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

CPU efficiency question re calls to Indicators

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

    #16
    Hi Dierk,
    the question relates to CPU usage. Does it take a lot of CPU for Ninja to supple an indicator value multiple times?
    The answer depends on if Ninja calculates the value from scratch everytime, or if it retains the calculation somewhere behind the scenes so that another access of it does not incur any overhead.

    Let's take an example. If I want to know Stochastics(...)[0], Ninja has to calculate the Stochastics series, then give me the value for the current bar.
    Then imagine I want to know Stochastics(...)[5]. Does it calculate all over again? Or does it retain the DataSeries from before so it can just immediately grab the required data without more CPU?

    I'm asking because with pattern recognition of oscillators there is a LOT of accessing across past data, over and over again.

    What method is the most CPU efficient?
    Thanks,
    saltminer

    Comment


      #17
      >> Then imagine I want to know Stochastics(...)[5]. Does it calculate all over again?
      No, the same Stochastics "object" will be reused, provided you called "Stochastics(...)" with the same parameter set.

      Comment


        #18
        Isn't it effective to define a DataSeries XXX in your indicator and set it to the Stochastics(...)[0] external indicator and then refer to it as XXX[0], XXX[1] or XXX[5]? That's what I do. If this is not any more efficient it sure is cleaner looking code.
        eDanny
        NinjaTrader Ecosystem Vendor - Integrity Traders

        Comment


          #19
          Again as per below: as an experienced C#/NinjaScript user you certainly could squeeze out the "last CPU cycles" by optimizing your code. Unfortunately we are unable provide support for that. You would be on your own.

          Comment


            #20
            saltminer - thanks for your lengthy pursuit of the final simple answer ... i.e. NT does not "re-calculate" for values it already has....good to know

            for my purposes, I often only look back a bar or two and so instead of a series just use the code...

            if (FirstTickOfBar)
            {
            var2=var1;
            var1=var;
            var = indicator(p1, p2, p3, p4, p5, p6)[0];
            }

            I will then access var1 multiple times on the same tick...which is clearly faster than accessing an NT-stored indicator value the same number of times

            why? because each value is stored in RAM and requires access time....so why not access RAM once for 'var1' instead of a total of at least 7 times (i.e. once for each of p1, p2, p3, p4, p5, p6 and finally NT-stored indicator(p1, p2, p3, p4, p5, p6)[1]....the cpu needs to access all the parameter values to verify/recognize it already has calculated and stored that specific previously-calculated indicator value (which takes time) and then access the stored value itself)

            so accessing var1 is at least 7 times faster (based on RAM access/CPU time)?....times say 10 accesses per tick?....times now many ticks per session?....times 500x in MarketReplay?....

            must save at least a nanosecond or two...and is easier for me to read (i.e. 'cleaner code' per eDanny)

            Comment


              #21
              The advantage of using a DataSeries over variables, for me, is variables can't be used in if(Rising()), if(Falling()), if(CrossAbove()), if(CrossBelow()). statements.
              eDanny
              NinjaTrader Ecosystem Vendor - Integrity Traders

              Comment


                #22
                Repeated calls with new data

                I want to call a moving average then change a bit of the input data and call it again to see how the MA changed. Here is a code snipet:

                protected override void OnBarUpdate()
                {
                if(CurrentBar < 5) return;
                hmaInput.Set( Close[
                0]+20);
                if(CurrentBar == 30){
                Print(hmaInput[
                0] +"; "+ HMA(hmaInput, 10)[0]);
                hmaInput.Set(Close[
                0] -20 );
                Print(hmaInput[
                0] +"; "+ HMA(hmaInput, 10)[0]);
                }

                NOTE: hmaInput is declared and initialized as a DataSeries
                hmaInput = new DataSeries(this);

                The Print output it:

                1256; 1256.24343434343
                1216; 1256.24343434343

                The last number in hmaInput has changed but the new call to
                HMA(hmaInput, 10) did not change the result!

                It is as if the compiler optimized out the second call to HMA and just used the last return value.

                HMA is not unique here, EMA gives the same result.

                My question is what is happening here and how can I get the results I expected?

                Last edited by LarryS; 01-06-2009, 11:13 AM.

                Comment


                  #23
                  LarryS,

                  Is this code in a NS strategy?
                  Josh P.NinjaTrader Customer Service

                  Comment


                    #24
                    It is in an indicator.

                    Comment


                      #25
                      LarryS,

                      We will take a look.
                      Josh P.NinjaTrader Customer Service

                      Comment


                        #26
                        Larry,

                        Here is the rundown. In OnBarUpdate(), NT will only call the indicator once per OnBarUpdate() event. Because you already called the indicator with Close[0] + 20, when you call it with the same series again it will just retrieve whatever it had before despite the fact that you changed the input series. Now, if you went ahead and called the indicator again on the next OnBarUpdate() event before changing any of the input series you will notice that you receive values for your Close[0] - 20 now.

                        If you wanted calculations twice there are two ways you can proceed, either have a second instance of HMA like HMA2 or something or just create two input series.
                        Josh P.NinjaTrader Customer Service

                        Comment


                          #27
                          Thanks for the explanation. It now makes sense.

                          My problem is that my intended plan was to iteratively call HMA() while changing the last piece of data until the result approached a desired value. Given this the number of times HMA will be called is unknown and will likely be greater than 2 (or 10 ...).

                          I see 2 possibilities:

                          1. is there some way to indicate that the data is not the same even though it is in the same DataSeries so that a method acting on the data will re-execute?

                          2. If i write my own version of HMA inside my indicator - myHMA - and call it instead of HMA then I should be able to have it execute as many times as needed.

                          My guess is the 1. is not gonna happen but 2. will work.

                          Thanks

                          Comment


                            #28
                            1. Will not work.

                            2. May work. I believe HMA calls WMA. If you migrate it all over it may work natively.
                            Josh P.NinjaTrader Customer Service

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                            0 responses
                            648 views
                            0 likes
                            Last Post Geovanny Suaza  
                            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                            0 responses
                            369 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
                            573 views
                            1 like
                            Last Post Geovanny Suaza  
                            Started by RFrosty, 01-28-2026, 06:49 PM
                            0 responses
                            575 views
                            1 like
                            Last Post RFrosty
                            by RFrosty
                             
                            Working...
                            X