Calculating MACD from scratch with Python
Developed by J. Ross Cameron and George C. Lapp, the MACD (Multi-Exponential Moving Average Convergence Divergence) algorithm is a popular technical indicator used to analyze trends and momentum in various financial markets. In this article, we will explore how to calculate MACD values from scratch with Python.
What is MACD?
The MACD calculation includes two main parts:
- EMA (Exponential Moving Average) HMA (Heavenly Relative Moving Average)
- Convergence Line (C-Line)
The MACD calculation formula can be divided into several steps:
HMA Calculation
To calculate HMA, we need to perform an exponential smoothing calculation on the closing price of the asset.
import numpy as np
def hma(prices, alpha):
"""
Calculate the exponential moving average (EMA) of a price list.
Parameters:
prices(list): list of closing prices.
alpha(float): EMA smoothing factor.
Returns:
list: list of EMA values.
"""
n = len(prices)
hma_values = []
in range(n):
hma_values.append(alpha prices[i] + (1 - alpha) hma_values[-1])
return np.array(hma_values)
def hma smoothed(prices, alpha, window_size):
"""
Calculate the exponential moving average (EMA) of a price list using a moving average.
Parameters:
prices(list): list of closing prices.
alpha(float): EMA smoothing factor.
window_size (int): The size of the moving average window.
Returns:
list: A list of EMA values.
"""
n = len (prices)
hma_values = hma(prices, alpha)
hma_values = np.convolve(hma_values, np.ones(window_size) / window_size, mode='same')
returns hma_values
MACD Calculation
MACD calculation includes the following steps:
- Calculate the HMA EMA.
- Calculate row C by taking the average of the two EMA values.
“` python
def macd(prices, window_size):
“””
Compute the multi-exponential moving average convergence divergence (MACD) algorithm.
Parameters:
prices (list): list of closing prices.
window_size (int): size of the MACD signal.
Returns:
list: list of MACD values.
“””
hma_values = hma_smoothed(prices, 3, window_size)
ema smoothed_hma_values = hma_smoothed(hma_values, 12, window_size)
ema_values = ema_smoothed(ema_smoothed_hma_values, 26, window_size)
macd_values = []
in range (len(ema_values) – window_size):
macd_values.append((ema_values[i] – ema smoothed_hma_values[i]) / ema smoothed_hma_values[i + window_size -1])
return np.array(macd_values)
def ema smoothed(prices, alpha, window_size):
“””
Calculate the exponential moving average (EMA) of a price list using a moving average.
Parameters:
prices (list): list of closing prices.
alpha (float): EMA smoothing factor.
window_size (int): Size of the moving average window.
Returns:
list: list of EMA values.
“””
n = len (prices)
hma_values = hma(prices, alpha)
hma_values = np.convolve(hma_values, np.ones(window_size) / window_size, mode=’same’)
hma_values = np.roll(hma_values, -window_size)
return hma_values
def ema_smoothed(ema_values, alpha, window_size):
“””
Calculate the exponential moving average (EMA) of a list of EMA values using a moving average.
Parameters:
ema_values (list): List of EMA values.
alpha (float): EMA smoothing factor.
window_size (int): Size of the moving average window.
Returns:
list: List of smoothed EMA values.
“””
n = len(mother_values)
hma_values = hma_smoothed(ema_values, 3, window_size)
hma_values = np.convolve(hma_values, np.