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

Differing SMA Plots - same calculations???

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

    Differing SMA Plots - same calculations???

    Hello,

    I've been studying MTF code and am shocked by a test plot I received today. I'm testing the following plots:
    SMA - built-in NT code
    SMAtest - copy of built-in code with the addition of this line:
    Code:
    if (CurrentBar < period) return;
    SMAFromRangeBarstest - copy of SMAFromRangeBars found on this forum with this code
    Code:
    if (CurrentBar < period || CurrentBars[1] < period) return;
    and
    SMAFromRangeBarstest1 - with this commented out
    Code:
    //if (CurrentBar < period || CurrentBars[1] < period) return;
    Three of the Plots are close enough but the fourth is unsettling.

    I'm attaching the 3 files in a zip along with a screenshot.
    The SMAtest shows a different plot. I've been adding that piece of code to many files I've been working on and only now realize that it gives a different Plot.

    Please tell me if there's a problem with my code and/or why adding this piece of code is required/not required for certain code types.
    Kirk
    Attached Files

    #2
    Hi Kirk,

    Thanks for the report. First steps I would take is change this line:
    average = SMA(Inputs[1],period);

    Inputs[] array is not supported. Please try with hard coded series like Closes[1]. Also, use upper-case public property when using inputs in code - Period.

    To confirm the issue you're reporting: Your added series is the same as the primary series, so you're expecting same calculations between standard SMA and multiseries SMA? Which script is not working as you expect?
    Last edited by NinjaTrader_RyanM1; 07-21-2011, 11:50 AM.
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Thanks for your quick reply.


      I've changed the "period" to "Period"...
      There's still a difference of Plot between the Built-in SMA and SMAtest which only has this one differing line of code:
      Code:
      if (CurrentBar < Period) return;

      SMAtest is a copy of the built-in SMA code. It has Input[0] in the code. Are you saying that the built-in code "shouldn't" have Input[0]?

      SMAtest1 is a copy of the built-in SMA code. It has Close[0] coded in place of Input[0].

      Built-in SMA is the white dot plot.
      SMAtest and SMAtest1 are both producing plots different from the Built-in SMA.

      Trying to figure out why or modify my code so that this is correct and not producing inaccurate plots.
      Kirk
      Attached Files
      Last edited by zeller4; 07-21-2011, 09:39 AM.

      Comment


        #4
        Using Input[] is fine and documented here:


        Inputs[] array is not supported.

        I see now the issue you're reporting but the code changes you've made do have an effect on the results. For performance reasons our SMA does not iterate through the last Period # bars to produce a value. Instead, it carries its previous value forward, and the calculation only looks at the current / previous value to produce a result.

        If you are returning out when CurrentBar < Period, then you do not have the first Period # bars included in the calculation, and will see different results.
        Ryan M.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_RyanM View Post
          Using Input[] is fine and documented here:


          Inputs[] array is not supported.

          I see now the issue you're reporting but the code changes you've made do have an effect on the results. For performance reasons our SMA does not iterate through the last Period # bars to produce a value. Instead, it carries its previous value forward, and the calculation only looks at the current / previous value to produce a result.

          If you are returning out when CurrentBar < Period, then you do not have the first Period # bars included in the calculation, and will see different results.
          That implies an error in coding logic. By the very definition of SMA, as long as there are more bars than required to calculate the SMA over a period, that calculation MUST return the same value. In other words you need to rewrite the indicator to discard any values from any bars that exceeds the specified period.

          Whereas it is true that the EMA and myriad other averages may carry residuals from earlier bars, and so be different, the SMA definition does not carry any such overflow. The definition is very strict and straightforward: it contains only the values from the last "Period" number of bars, and so correctly the SMA should be the same as long as there are more bars than said "Period".

          If your calculation is different from adding up the values of the last "Period" bars, and dividing by the Period, then that value is wrong, and implicitly, so is the calculation. There is nothing to argue about.

          Comment


            #6
            We calculate SMA like this for performance reasons and the source code has always been available to see. If users prefer a different SMA calculation that only looks at the most recent x bars, they can code one.

            The technique for this type of SMA is demonstrated in this indicator tutorial:
            Ryan M.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_RyanM View Post
              We calculate SMA like this for performance reasons and the source code has always been available to see. If users prefer a different SMA calculation that only looks at the most recent x bars, they can code one.

              The technique for this type of SMA is demonstrated in this indicator tutorial:
              http://www.ninjatrader.com/support/h...ur_own_sma.htm
              I apologize. Slip of the finger, (and the brain ). Of course, the NT calculation is absolutely correct. I had already checked it a long time ago, and found that it was only correct for fixed periods, which is when I wrote another one that could use a variable period. That may have misled my mind into the parallel line of thinking that there was a fundamental issue, and my wrong critique.

              The problem is in the SMATest script. That early bars escape, means that the first value of the variable "last" will almost always be wrong, as the initial calculations that will accumulate Input values before the Period is reached, will not be run, so the value of "last" used for the initial calculation will be wrong.

              Code:
              		{
              			[COLOR="green"]if (CurrentBar < period) return;[/COLOR] [COLOR="Blue"]//because of this line, the line in [COLOR="Red"]red[/COLOR] below will never be run, so the initial calculation at the start of "Period" will be wrong, and be propagated[/COLOR]
              			if (CurrentBar == 0)
              				Value.Set(Input[0]);
              			else
              			{
              				double last = Value[1] * Math.Min(CurrentBar, Period); [COLOR="Blue"]//this line will almost certainly produce the wrong value when it is first run, because of the issues between the lines in [COLOR="Green"]green above[/COLOR], and [COLOR="Red"]red below.[/COLOR][/COLOR]
              
              				if (CurrentBar >= Period)
              					Value.Set((last + Input[0] - Input[Period]) / Math.Min(CurrentBar, Period));
              				else
              					[COLOR="Red"]Value.Set((last + Input[0]) / (Math.Min(CurrentBar, Period) + 1));[/COLOR] [COLOR="blue"]//this is calculating the SMA correctly before the "Period" number of bars, and is used to correctly initiate the transition. It will never be run, because of the line in [COLOR="Green"]green[/COLOR] above[/COLOR]
              			}
              		}

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by judysamnt7, 03-13-2023, 09:11 AM
              4 responses
              57 views
              0 likes
              Last Post DynamicTest  
              Started by ScottWalsh, Today, 06:52 PM
              4 responses
              36 views
              0 likes
              Last Post ScottWalsh  
              Started by olisav57, Today, 07:39 PM
              0 responses
              7 views
              0 likes
              Last Post olisav57  
              Started by trilliantrader, Today, 03:01 PM
              2 responses
              19 views
              0 likes
              Last Post helpwanted  
              Started by cre8able, Today, 07:24 PM
              0 responses
              9 views
              0 likes
              Last Post cre8able  
              Working...
              X