Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Is it Correct to calculate SMA like this?

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

    Is it Correct to calculate SMA like this?

    Ninja Trader has given the Algorithm of SMA like this :

    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));
    }

    So, for example, if the Period=14, and CurrentBar=15, SMA=(Value[1]*14+Input[0]-InPut[14])/14 . Then, instead of adding up the last 14 bars, it just simply use the Value*14 ?

    It seems quite confused to me, any suggestion ?

    #2
    Welcome here to our forums wolfcuring, the calculation is valid and correct, it's just a more elegant way saving processing power. If you compare the result to for example to SUM(14) / 14 >> it would be exactly the same.

    Comment


      #3
      Thank you Bertrand

      Comment


        #4
        Hi Bertrand,

        About the coding of SMA, then I guess the Value[1] must be representing the median from on bar ago? I checked carefully the help manual, but still get a little confused, what the "Value" is and how Ninja will process the price data.

        I have been through a C# tutorial and the Ninja Script reference. Any suggestion how to get further?

        Comment


          #5
          Hi wolfcuring, this would simply be the indicator main value series - http://www.ninjatrader.com/support/h.../nt7/value.htm

          Comment


            #6
            Originally posted by wolfcuring View Post
            Hi Bertrand,

            About the coding of SMA, then I guess the Value[1] must be representing the median from on bar ago? I checked carefully the help manual, but still get a little confused, what the "Value" is and how Ninja will process the price data.

            I have been through a C# tutorial and the Ninja Script reference. Any suggestion how to get further?
            SMA = RunningTotalValue / Period.

            Therefore, SMA(PreviousBar) = RunningTotalValue(PreviousBar) / Period.
            Therfefore, RunningTotalValue(PreviousBar) = SMA(PreviousBar) * Period.
            Therefore, RunningTotalValue(ThisBar) = RunningTotalValue(PreviousBar) + Value(ThisBar) - Value(PeriodBarsAgo).

            Finally, SMA(ThisBar) = RunningTotalValue(ThisBar) / Period.

            Comment


              #7
              Thank you Koganam.

              What seems to be most confusing to me is what does Value[int x] represent. The language of the user manual is not quite 'reader friendly'.

              Besides, as a newbie to coding, I enjoy the simplicity and beauty of these algorithm.

              Comment


                #8
                Originally posted by wolfcuring View Post
                Thank you Koganam.

                What seems to be most confusing to me is what does Value[int x] represent. The language of the user manual is not quite 'reader friendly'.

                Besides, as a newbie to coding, I enjoy the simplicity and beauty of these algorithm.
                Value is the default first Plot. if one is not explicitly defined in the public properties.

                It is indexed backwards from the CurrentBar, (CurrentBar is zero). Therefore, Value[x] where x is an integer, is the value of the Plot "x bars" ago, counting from the CurrentBar. eg., the previous bar is always Value[1], 2 bars ago is Value[2] etc.

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                0 responses
                574 views
                0 likes
                Last Post Geovanny Suaza  
                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                0 responses
                333 views
                1 like
                Last Post Geovanny Suaza  
                Started by Mindset, 02-09-2026, 11:44 AM
                0 responses
                101 views
                0 likes
                Last Post Mindset
                by Mindset
                 
                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                0 responses
                553 views
                1 like
                Last Post Geovanny Suaza  
                Started by RFrosty, 01-28-2026, 06:49 PM
                0 responses
                551 views
                1 like
                Last Post RFrosty
                by RFrosty
                 
                Working...
                X