Simple Moving Average (SMA)
get_sma(quotes, lookback_periods, candle_part=CandlePart.CLOSE)
Parameters
name | type | notes |
---|---|---|
quotes |
Iterable[Quote] | Iterable(such as list or an object having __iter__() ) of the Quote class or its sub-class. • Need help with pandas.DataFrame? |
lookback_periods |
int | Number of periods (N ) in the lookback window. Must be greater than 0. |
candle_part |
CandlePart, default CandlePart.CLOSE | Specify candle part to evaluate. See CandlePart options below. |
Historical quotes requirements
You must have at least N
periods of quotes
to cover the warmup periods.
quotes
is an Iterable[Quote]
collection of historical price quotes. It should have a consistent frequency (day, hour, minute, etc). See the Guide for more information.
CandlePart options
from stock_indicators.indicators.common.enums import CandlePart
type | description |
---|---|
CandlePart.OPEN |
open price |
CandlePart.HIGH |
high price |
CandlePart.LOW |
low price |
CandlePart.CLOSE |
close price |
CandlePart.VOLUME |
volume |
CandlePart.HL2 |
(high+low)/2 |
CandlePart.HLC3 |
(high+low+close)/3 |
CandlePart.OC2 |
(open+close)/2 |
CandlePart.OHL3 |
(open+high+low)/3 |
CandlePart.OHLC4 |
(open+high+low+close)/4 |
Returns
SMAResults[SMAResult]
- This method returns a time series of all available indicator values for the
quotes
provided. -
SMAResults
is just a list ofSMAResult
. - It always returns the same number of elements as there are in the historical quotes.
- It does not return a single incremental indicator value.
- The first
N-1
periods will haveNone
values since there’s not enough data to calculate.
SMAResult
name | type | notes |
---|---|---|
date |
datetime | Date |
sma |
float, Optional | Simple moving average |
Utilities
See Utilities and Helpers for more information.
Example
from stock_indicators import indicators
from stock_indicators import CandlePart # Short path, version >= 0.8.1
# This method is NOT a part of the library.
quotes = get_history_from_feed("SPY")
# calculate 20-period SMA
results = indicators.get_sma(quotes, 20, CandlePart.CLOSE)
Extended analysis
This indicator has an extended version with more analysis.
get_sma_analysis(quotes, lookback_periods)
Return with analysis
SMAAnalysisResults[SMAAnalysisResult]
SMAAnalysisResult
name | type | notes |
---|---|---|
date |
datetime | Date |
sma |
float, Optional | Simple moving average |
mad |
float, Optional | Mean absolute deviation |
mse |
float, Optional | Mean square error |
mape |
float, Optional | Mean absolute percentage error |
Example for analysis
# usage
results = indicators.get_sma_analysis(quotes, lookback_periods)
About Simple Moving Average (SMA)
Simple Moving Average is the average price over a lookback window. The extended analysis option includes mean absolute deviation (MAD), mean square error (MSE), and mean absolute percentage error (MAPE).
[Discuss]