This commit is contained in:
Miha Kralj
2023-01-07 22:32:33 -08:00
parent 3c9bfb265d
commit 6db1ba7562
3 changed files with 74 additions and 69 deletions
+34 -26
View File
@@ -1,9 +1,5 @@
# SMA: Simple Moving Average
period = 10
![Alt text](./img/SMA_chart.svg)
SMA is is an arithmetic moving average where the weights in SMA are **equally** distributed across the given period, resulting in a mean() of the data within the period.
## Calculation
@@ -12,38 +8,50 @@ SMA is a rolling calculation that is looking backwards from the position ${n}$ a
$$
SMA_p{(data)} = \frac{1}{p}\sum_{i=n-p+1}^{n} data_i
$$
When calculating the value of the next $SMA_{p,next}$ while knowing all previous SMA values, SMA calculation can be reduced to:
When calculating the value of the next $SMA_{p,next}$ while knowing previous SMA values, SMA calculation can be reduced to:
$$
SMA_{p,next} = SMA_{p,prev}+\frac{1}{p}\left( data_{n+1}-data_{n+1-p}\right)
$$
## Reference Calculation
## Implementation
`TSeries SMA_Series (TSeries source, int period = 0, bool useNaN = false)`
- SMA_Series returns TSeries list
- `source`: input of type TSeries; SMA_Series automatically subscribes to events of new data added to the source
- `period`: optional size of a lookback window; if set to 0, SMA calculates cumulative average across the whole source
- `useNaN`: if set to _true_, SMA_Series will hide values within the initial period with NaN (for compatibility with other libraries)
[Link to source](..\Source\Trends\SMA_Series.cs)
## Behavior
![Alt text](./img/SMA_chart.svg)
## Reference Calculation & Validation
period = 5
```
TSeries data = new() {81.59, 81.06, 82.87, 83.00, 83.61, 83.15, 82.84, 83.99, 84.55, 84.36, 85.53, 86.54, 86.89, 87.77, 87.29};
SMA_Series sma = new(data, 5, useNaN: false);
SMA_Series sma_nan = new(data, 5, useNaN: true);
for (int i=0; i< data.Count; i++)
Console.WriteLine($"{i}\t{data[i].v,7:f2}\t{sma_nan[i].v,7:f3}\t{sma[i].v,7:f3}");
```
|#|input|sma_NaN|sma|
|--|:--:|:--:|:--:|
|0| 81.59| NaN| 81.590|
|1| 81.06| NaN| 81.325|
|2| 82.87| NaN| 81.840|
|3| 83.00| NaN| 82.130|
|4| 83.61| 82.426| 82.426|
|5| 83.15| 82.738| 82.738|
|6| 82.84| 83.094| 83.094|
|7| 83.99| 83.318| 83.318|
|8| 84.55| 83.628| 83.628|
|9| 84.36| 83.778| 83.778|
|10| 85.53| 84.254| 84.254|
|11| 86.54| 84.994| 84.994|
|12| 86.89| 85.574| 85.574|
|13| 87.77| 86.218| 86.218|
|14| 87.29| 86.804| 86.804|
| #| Input | **QuanTAlib** | _TA-LIB_ | _Skender_ | _Pandas-TA_ | _Tulip_ |
|--|:--:|:--:|:--:|:--:|:--:|:--:|
| 0| 212.80|**212.80**| _NaN_| _NaN_| _NaN_| _NaN_|
| 1| 214.06|**213.43**| _NaN_| _NaN_| _NaN_| _NaN_|
| 2| 213.89|**213.58**| _NaN_| _NaN_| _NaN_| _NaN_|
| 3| 214.66|**213.85**| _NaN_| _NaN_| _NaN_| _NaN_|
| 4| 213.95|**213.87**| _213.87_| _213.87_| _213.87_| _213.87_|
| 5| 213.95|**214.10**| _214.10_| _214.10_| _214.10_| _214.10_|
| 6| 214.55|**214.20**| _214.20_| _214.20_| _214.20_| _214.20_|
| 7| 214.02|**214.23**| _214.23_| _214.23_| _214.23_| _214.23_|
| 8| 214.51|**214.20**| _214.20_| _214.20_| _214.20_| _214.20_|
| 9| 213.75|**214.16**| _214.16_| _214.16_| _214.16_| _214.16_|
|10| 214.22|**214.21**| _214.21_| _214.21_| _214.21_| _214.21_|
|11| 213.43|**213.99**| _213.99_| _213.99_| _213.99_| _213.99_|
|12| 214.21|**214.02**| _214.02_| _214.02_| _214.02_| _214.02_|
|13| 213.66|**213.85**| _213.85_| _213.85_| _213.85_| _213.85_|
|14| 215.03|**214.11**| _214.11_| _214.11_| _214.11_| _214.11_|
|15| 216.89|**214.64**| _214.64_| _214.64_| _214.64_| _214.64_|
|16| 216.66|**215.29**| _215.29_| _215.29_| _215.29_| _215.29_|
## References
- https://en.wikipedia.org/wiki/Moving_average#Simple_moving_average
+1 -1
View File
@@ -75,7 +75,7 @@
|PWMA - Pascal's Weighted Moving Average||||pwma|
|⭐RMA - WildeR's Moving Average|`RMA_Series`|||✔️rma|✔️rma|
|SINWMA - Sine Weighted Moving Average||||sinwma|
|⭐SMA - Simple Moving Average|`SMA_Series`|✔️SMA|✔️GetSma|✔️sma|✔️sma|
|⭐[SMA - Simple Moving Average](SMA.md)|`SMA_Series`|✔️SMA|✔️GetSma|✔️sma|✔️sma|
|SMMA - Smoothed Moving Average|`SMMA_Series`||✔️GetSmma||
|SSF - Ehler's Super Smoother Filter||||ssf|
|SUPERTREND - Supertrend||||supertrend|