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

SMA and DataSeries

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

    SMA and DataSeries

    Hi, Please consider the following problem I have:
    private DataSeries ds;

    void Initialize()
    {

    ds = new DataSeries(this); // Create DataSeries
    }


    void OnBarUpdate()
    {

    for(int i = 0; i <= 20; i++)
    ds.Set(i,i); // fill the series again
    double f = SMA(ds, 3)[0]; // SMA of last 3 bars
    Print(f);

    }

    The problem is, that only at the first time f is correct (f = 1) i.e. (0 + 1 + 2) / 3
    and on every new Print, f is not the SMA of those last 3 bars in ds, which are always 0, 1, 2....
    also, I see that ds is growing itself with each new BarUpdate, why is that.
    Thanks in advance.
    Arnon.



    #2
    Hi Arnon_B,

    I'm looking into this and I will have an answer for you shortly.

    I appreciate your patience.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi Arnon_B,

      Thanks for your patience as I looked this over.

      The issue here is with the SMA. The SMA does not expect you to change the historical values of the fed dataseries.

      The line
      double last = Value[1] * Math.Min(CurrentBar, Period);
      is what is causing the issue.

      Instead of recalculating the values for the entire series after every bar, the SMA uses the previous value of the SMA (Value[1]) * the period (so the weight is correct) and then adds the last value from your input dataseries (Input[0]) and divides by the period.

      So basically, if you plan to constantly adjust your dataseries historically, you would need to recode the SMA so that it is forced to recalculate the entire historical series on every OnBarUpdate.
      Chelsea B.NinjaTrader Customer Service

      Comment


        #4
        Thanks a lot.
        Arnon.

        Comment


          #5
          Originally posted by NinjaTrader_ChelseaB View Post
          Hi Arnon_B,

          Thanks for your patience as I looked this over.

          The issue here is with the SMA. The SMA does not expect you to change the historical values of the fed dataseries.

          The line
          double last = Value[1] * Math.Min(CurrentBar, Period);
          is what is causing the issue.

          Instead of recalculating the values for the entire series after every bar, the SMA uses the previous value of the SMA (Value[1]) * the period (so the weight is correct) and then adds the last value from your input dataseries (Input[0]) and divides by the period.

          So basically, if you plan to constantly adjust your dataseries historically, you would need to recode the SMA so that it is forced to recalculate the entire historical series on every OnBarUpdate.
          Or you can Dispose() the SMA() each time, before you call it, so that it is always calculated afresh.

          Comment


            #6
            Thanks a lot.
            Arnon.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Irukandji, Today, 04:58 AM
            0 responses
            0 views
            0 likes
            Last Post Irukandji  
            Started by fitspressoburnfat, Today, 04:25 AM
            0 responses
            2 views
            0 likes
            Last Post fitspressoburnfat  
            Started by Skifree, Today, 03:41 AM
            1 response
            4 views
            0 likes
            Last Post Skifree
            by Skifree
             
            Started by usazencort, Today, 01:16 AM
            0 responses
            1 view
            0 likes
            Last Post usazencort  
            Started by kaywai, 09-01-2023, 08:44 PM
            5 responses
            604 views
            0 likes
            Last Post NinjaTrader_Jason  
            Working...
            X