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

Fast High Precision SMA

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

    Fast High Precision SMA

    For a specific application, I required a high-precision SMA. The key issue was that the incremental version supplied with NinjaTrader would accumulate computational errors leading to erroneous signals under certain conditions. This was particularly evident when dealing with a data-set of 50,000 minutes and long (200) moving averages.

    The challenge was that computing the SMA using the full calculation (sum all of the inputs over the window, and divide by window size) took FAR too long...

    The result is the attached script which periodically uses the full computation to compare to the incremental one. If the error exceeds a threshold, it increases the frequency with which it checks. If the error is less than the threshold, it decreases the frequency of checks (with limits in both directions). The result is an SMA with the precision required, and close to the speed of the incremental version.

    The maximum error is set statically (rMaxErr in the Variables region).

    If high precision is ALWAYS required, the window can be set to a negative value. So, -14 will do a 14-period SMA performing the full calculation each time. Using a 14 for the window will typically perform the incremental calculation, and checking this periodically against a full calculation.

    The window is limited to a maximum of 254 so that the cache is never exceeded.

    If you would like to see when the error threshold is exceeded, un-comment the "Print()" statement.

    Please post if you find this helpful.

    SharkCub
    Attached Files

    #2
    SharkCub, thanks for posting, although I'm curious as to why/how you suspect there is an error in the provided SMA() calculations, as the calculation is very simple - (sum of data point values)/(number of data points).
    AustinNinjaTrader Customer Service

    Comment


      #3
      Here is the code from the NT7 "SMA" that does the calculation


      if (CurrentBar == 0)
      Value.Set(Input[0]);
      else
      {
      double last = Value[1] * Math.Min(CurrentBar, Period);

      if (CurrentBar >= Period)
      Value.Set((last + Input[0] - Input[Period]) / Math.Min(CurrentBar, Period));
      else
      Value.Set((last + Input[0]) / (Math.Min(CurrentBar, Period) + 1));
      }

      You will notice that it is performing an incremental calculation - re-constructing the sum from the prior step, then merging the incremental value and dividing.... Lots of opportunities to introduce numerical precision errors...

      Edit the SMAhp, uncomment the "Print()", and you will observe that there ARE differences over the long-term...
      Last edited by SharkCub; 10-03-2010, 01:21 PM.

      Comment


        #4
        SharkCub, thank you for your comments.
        AustinNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by algospoke, 04-17-2024, 06:40 PM
        6 responses
        49 views
        0 likes
        Last Post algospoke  
        Started by arvidvanstaey, Today, 02:19 PM
        4 responses
        11 views
        0 likes
        Last Post arvidvanstaey  
        Started by samish18, 04-17-2024, 08:57 AM
        16 responses
        61 views
        0 likes
        Last Post samish18  
        Started by jordanq2, Today, 03:10 PM
        2 responses
        9 views
        0 likes
        Last Post jordanq2  
        Started by traderqz, Today, 12:06 AM
        10 responses
        21 views
        0 likes
        Last Post traderqz  
        Working...
        X