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