mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-07-31 10:57:43 +00:00
2.8 KiB
2.8 KiB
SMA: Simple Moving Average
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
SMA is a rolling calculation that is looking backwards from the position {n} and is denoted as {SMA}_{p}{(data)} where p represents the period and data represents the list of data points:
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 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)
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 sourceperiod: optional size of a lookback window; if set to 0, SMA calculates cumulative average across the whole sourceuseNaN: if set to true, SMA_Series will hide values within the initial period with NaN (for compatibility with other libraries)
Behavior
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);
| # | 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
- Kaufman, Perry J. (2013) Trading Systems and Methods
- Murphy, J. (1999) Technical Analysis of the Financial Markets