• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

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 ntbone, Today, 12:05 AM
          0 responses
          4 views
          0 likes
          Last Post ntbone
          by ntbone
           
          Started by cutzpr, Yesterday, 09:33 PM
          1 response
          10 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Started by hotsalami, Yesterday, 09:02 PM
          1 response
          9 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Started by Luigio, Yesterday, 04:50 AM
          1 response
          9 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Started by sidviciousX, 08-17-2019, 06:47 AM
          2 responses
          33 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Working...
          X