Skip to content

Moving Average Convergence/Divergence

MACD is a trend-following momentum indicator that shows the relationship between two EMAs.

Method

tm.ta.macd(source, fastperiod, slowperiod, signalperiod)

Inputs

ParameterArgument typeDescriptionDefault Value
sourcepd.SeriesInput data series-
fastperiodintFast period for the indicator12
slowperiodintSlow period for the indicator26
signalperiodintSignal period for the indicator9

Outputs

OutputType
macdnp.ndarray
macdsignalnp.ndarray
macdhistnp.ndarray

Example usage

strategy.py
import tradomate as tm
@tm.strategy()
def my_strategy(config: tm.TradomateConfig, data: tm.TradomateData):
# Trying out Moving Average Convergence/Divergence
macd, macdsignal, macdhist = tm.ta.macd(data.close, fastperiod=12, slowperiod=26, signalperiod=9)
# Get the last value and print
last_value = macd.iloc[-1]
tm.log(f"Last value of Moving Average Convergence/Divergence is {last_value}")
# Plot the values of macd
tm.plot(macd, title="Moving Average Convergence/Divergence", overlay=False)