Announcement

Collapse
No announcement yet.

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 Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            662 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            376 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            110 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            574 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            580 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X