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

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.
    BertrandNinjaTrader Customer Service

    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
          BertrandNinjaTrader Customer Service

          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 inanazsocial, Today, 01:15 AM
                1 response
                6 views
                0 likes
                Last Post NinjaTrader_Jason  
                Started by rocketman7, Today, 02:12 AM
                0 responses
                10 views
                0 likes
                Last Post rocketman7  
                Started by dustydbayer, Today, 01:59 AM
                0 responses
                2 views
                0 likes
                Last Post dustydbayer  
                Started by trilliantrader, 04-18-2024, 08:16 AM
                5 responses
                23 views
                0 likes
                Last Post trilliantrader  
                Started by Davidtowleii, Today, 12:15 AM
                0 responses
                3 views
                0 likes
                Last Post Davidtowleii  
                Working...
                X