Moving Average Convergence / Divergence (MACD)
get_macd(quotes, fast_periods=12, slow_periods=26, signal_periods=9)
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? |
fast_periods |
int, default 12 | Number of periods (F ) for the faster moving average. Must be greater than 0. |
slow_periods |
int, default 26 | Number of periods (S ) for the slower moving average. Must be greater than fast_periods . |
signal_periods |
int, default 9 | Number of periods (P ) for the moving average of MACD. Must be greater than or equal to 0. |
candle_part |
CandlePart, default CandlePart.CLOSE | Specify candle part to evaluate. See CandlePart options below. |
Historical quotes requirements
You must have at least 2×(S+P)
or S+P+100
worth of quotes
, whichever is more, to cover the convergence periods. Since this uses a smoothing technique, we recommend you use at least S+P+250
data points prior to the intended usage date for better precision.
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
MACDResults[MACDResult]
- This method returns a time series of all available indicator values for the
quotes
provided. -
MACDResults
is just a list ofMACDResult
. - 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
S-1
slow periods will haveNone
values since there’s not enough data to calculate.
Convergence warning: The first
S+P+250
periods will have decreasing magnitude, convergence-related precision errors that can be as high as ~5% deviation in indicator values for earlier periods.
MACDResult
name | type | notes |
---|---|---|
date |
datetime | Date |
macd |
float, Optional | The MACD line is the difference between slow and fast moving averages (macd = fast_ema - slow_ema ) |
signal |
float, Optional | Moving average of the macd line |
histogram |
float, Optional | Gap between of the macd and signal line |
fast_ema |
float, Optional | Fast Exponential Moving Average |
slow_ema |
float, Optional | Slow Exponential Moving Average |
Utilities
See Utilities and Helpers for more information.
Example
from stock_indicators import indicators
# This method is NOT a part of the library.
quotes = get_history_from_feed("SPY")
# calculate MACD(12,26,9)
results = indicators.get_macd(quotes, 12, 26, 9)
About Moving Average Convergence / Divergence (MACD)
Created by Gerald Appel, MACD is a simple oscillator view of two converging/diverging exponential moving averages. [Discuss]