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

which one would be faster

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

    which one would be faster

    Suppose I can use two alternative codes snippet in my OnBarUpdate() section:

    Code # 1:

    z.Set(EMA(x, 20)[0]-EMA(x, 40)[0]); // x is defined as a new DataSeries in Initialize()


    Code # 2:

    z1.Set(EMA(x, 20)[0]); // z1 is defined as a new DataSeries in Initialize()
    z2.Set(EMA(x, 40)[0]); // z2 is defined as a new DataSeries in Initialize()

    z.Set(z1[0]-z2[0]); // z is defined as a new DataSeries in Initialize()


    Which one of the two could be faster in live-trading (say based on 1 minute data)?

    The first Code Snippet doesn't store any intermediate values, while the second Code Snippet stores two intermediate values.

    #2
    z1.

    I think calling method Set has extra overhead.

    But it would probably be nothing you would notice unless you are running some really old machine.

    You could put it in a for loop and maybe get some checks for comparison.

    Comment


      #3
      Originally posted by sledge View Post
      z1.

      I think calling method Set has extra overhead.

      But it would probably be nothing you would notice unless you are running some really old machine.

      You could put it in a for loop and maybe get some checks for comparison.
      My worry is that when I do

      z.Set(EMA(x, 20)[0]-EMA(x, 40)[0]);

      Ninjatrader is not storing the intermediate values of EMA(x, 20) and EMA(x, 40) and then it might just recalculate the EMAs on the last 256 bars each time.

      While in the case of

      z1.Set(EMA(x, 20)[0]);
      z2.Set(EMA(x, 40)[0]);
      z.Set(z1[0]-z2[0]);

      it gives me a more accurate answer as it calculates z based on z1 and z2 both of which have been calculated (and stored) since the first bar.

      Then the choice would be between (i) storing two more DataSeries z1, z2, and the Set overhead that you mention, versus (ii) recalculating the two EMAs on 256 bars on each bar update.

      Would I be correct in my reasoning?

      Comment


        #4
        In my understanding, no, your reasoning is not correct


        How can it be more accurate? Do you see a difference in values?

        You call the same ema function which calculates everything. The one has an additional step of storing it in an "array" with the set command.

        Comment


          #5
          Originally posted by uday12 View Post
          My worry is that when I do

          z.Set(EMA(x, 20)[0]-EMA(x, 40)[0]);

          Ninjatrader is not storing the intermediate values of EMA(x, 20) and EMA(x, 40) and then it might just recalculate the EMAs on the last 256 bars each time.

          While in the case of

          z1.Set(EMA(x, 20)[0]);
          z2.Set(EMA(x, 40)[0]);
          z.Set(z1[0]-z2[0]);

          it gives me a more accurate answer as it calculates z based on z1 and z2 both of which have been calculated (and stored) since the first bar.

          Then the choice would be between (i) storing two more DataSeries z1, z2, and the Set overhead that you mention, versus (ii) recalculating the two EMAs on 256 bars on each bar update.

          Would I be correct in my reasoning?
          If you look at the NinjaTrader wrapper code, you will see that the EMAs would be cached. In this simple case, that means that you are unlikely to see a performance difference between your code snippets. In more elaborate cases, especially if CalculateOnBarClose is also false, trolling the cache itself can become a bottleneck. By far, the most scalable method is to create your EMAs as named instance objects, then query those.

          A similar question and some links are in this thread.

          ref: http://ninjatrader.com/support/forum...ad.php?t=57327

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by algospoke, Yesterday, 06:40 PM
          2 responses
          23 views
          0 likes
          Last Post algospoke  
          Started by ghoul, Today, 06:02 PM
          3 responses
          15 views
          0 likes
          Last Post NinjaTrader_Manfred  
          Started by jeronymite, 04-12-2024, 04:26 PM
          3 responses
          46 views
          0 likes
          Last Post jeronymite  
          Started by Barry Milan, Yesterday, 10:35 PM
          7 responses
          23 views
          0 likes
          Last Post NinjaTrader_Manfred  
          Started by AttiM, 02-14-2024, 05:20 PM
          10 responses
          181 views
          0 likes
          Last Post jeronymite  
          Working...
          X