• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Weird SMA issue

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

    Weird SMA issue

    Hi Folks,

    I am have having an odd issue with a simple SMA. Hoping you can suggest something to point me in the right direction.

    My primary data series (plot), is this

    Code:
            private Series<double>    P;
    private SMA PSma;
    
                else if (State == State.DataLoaded)
                {
                    P= new Series<double>(this, MaximumBarsLookBack.TwoHundredFiftySix); // have also tried infinite
                    PSma                 = SMA(P,10);
                }
    and I simply set P[0] = x in onbarupdate. It's all really very simple.

    Now, this actually works perfectly fine. BUT, somewhere along the line, the SMA calculation goes all wrong. What I am checking for is values above the 10 period SMA. After a few hours, SMA is reported clearly well below what should be expected. See this debug output for example of the last values output

    1037: Last pr = 7969, P = 36, Avg = 24.30, % = 1.48
    1038: Last pr = 7969.5, P = 46, Avg = 22.70, % = 2.03
    1039: Last pr = 7969, P = 50, Avg = 24.30, % = 2.06
    1040: Last pr = 7969, P = 43, Avg = 21.30, % = 2.02
    1041: Last pr = 7968, P = 33, Avg = 20.80, % = 1.59
    1042: Last pr = 7967.75, P = 40, Avg = 20.70, % = 1.93
    1043: Last pr = 7969, P = 41, Avg = 24.60, % = 1.67
    1044: Last pr = 7967, P = 29, Avg = 14.80, % = 1.96
    1045: Last pr = 7967, P = 61, Avg = 14.80, % = 4.12
    1046: Last pr = 7966.75, P = 32, Avg = 16.60, % = 1.93
    1047: Last pr = 7966.75, P = 56, Avg = 14.80, % = 3.78
    1048: Last pr = 7967.25, P = 49, Avg = 16.40, % = 2.99
    1049: Last pr = 7966.75, P = 61, Avg = 17.30, % = 3.53
    1050: Last pr = 7965.5, P = 39, Avg = 16.20, % = 2.41

    Given the P values above, and it's a simple 10 period SMA, you can see, the AVG value (PSma[0]) no longer makes any sense. It only does this after some time though! For first hour or so of the session or more, it's perfect!

    So, what could cause this? Even in the event a zero value or something made it in there and threw it off, I would expect it to self correct after 10 values had passed, given it should only be iterating those last 10 values to calculate the SMA? There are no errors I see reported, and this incorrect SMA calculation appears to be the only symptom.

    Just for additional info, here are some values from the indicator restarted, where they are being calculated sensibly...

    3: Last pr = 7949.75, P = 36, Avg = 11.90, % = 3.03
    4: Last pr = 7950.5, P = 35, Avg = 15.40, % = 2.27
    5: Last pr = 7952, P = 24, Avg = 17.80, % = 1.35
    6: Last pr = 7952, P = 43, Avg = 22.10, % = 1.95
    7: Last pr = 7952.75, P = 33, Avg = 25.30, % = 1.30

    A related query? An SMA(10), seems to calculate 10 values, even if you only populated less than 10. Should the SMA not calculate the average using the count, up until it reaches 10, then use a count of 10 for the average calculation? As you can see from above, SMA takes time to reach normality, whereas I would have expected it to be normal from the 1st value, i.e. SMA(10) with only 1 value as yet populated would return that value as the current average. Is that not normal / expected?


    Thanks for any pointers.
    Last edited by pjsmith; 07-16-2019, 01:35 PM.

    #2
    Hello pjsmith,

    Thanks for your post.

    I have shared a test the we can both take.

    Code:
    public class Test : Indicator
        {
            private Series<double> mySeries;
            private SMA SMA1;
    
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                    = @"Enter the description for your new custom Indicator here.";
                    Name                                        = "Test";
                    Calculate                                    = Calculate.OnBarClose;
                }
                else if (State == State.DataLoaded)
                {
                    mySeries = new Series<double>(this, MaximumBarsLookBack.Infinite);
                    SMA1 = SMA(mySeries, 10);
                }
            }
    
            protected override void OnBarUpdate()
            {
                mySeries[0] = CurrentBar;
    
                Print(String.Format("mySeries[0]: {0} SMA1[0]: {1}", mySeries[0], SMA1[0]));
            }
        }
    Output:
    mySeries[0]: 5402 SMA1[0]: 5397.5
    mySeries[0]: 5403 SMA1[0]: 5398.5
    mySeries[0]: 5404 SMA1[0]: 5399.5
    mySeries[0]: 5405 SMA1[0]: 5400.5
    mySeries[0]: 5406 SMA1[0]: 5401.5
    mySeries[0]: 5407 SMA1[0]: 5402.5
    mySeries[0]: 5408 SMA1[0]: 5403.5
    mySeries[0]: 5409 SMA1[0]: 5404.5
    mySeries[0]: 5410 SMA1[0]: 5405.5
    mySeries[0]: 5411 SMA1[0]: 5406.5
    5411+5410+5409+54-8+5407+5406+5405+5404+5403+5402 = 54065.
    54065/10 = 5406.5.

    As we can see, each value is totalled and then divided by the period and results are consistent here. Please test the same to ensure that your are getting correct results.

    Many users use CurrentBar checks to ensure that their indicator are only calculated when there are enough data points in the series that is feeding the indicator to ensure that a "normal" value would be calculated. I.E.

    if (CurrentBar < 10) return;

    I look forward to being of any further assistance.
    JimNinjaTrader Customer Service

    Comment


      #3
      Thank you. I think the issue was I was updating the ISeries outside of OnBarUpdate, and I believe the CurrentBar was probably not synced. Since moving the assignments within onbarupdate, it seems OK so far today.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Debayan Sen, Today, 01:50 AM
      0 responses
      5 views
      0 likes
      Last Post Debayan Sen  
      Started by eleven, Yesterday, 10:03 PM
      1 response
      10 views
      0 likes
      Last Post eleven
      by eleven
       
      Started by tvbecv, Yesterday, 09:23 PM
      0 responses
      8 views
      0 likes
      Last Post tvbecv
      by tvbecv
       
      Started by reedstickets, Yesterday, 08:07 PM
      0 responses
      10 views
      0 likes
      Last Post reedstickets  
      Started by timko, Yesterday, 07:39 PM
      0 responses
      6 views
      0 likes
      Last Post timko
      by timko
       
      Working...
      X