MACD in Python calculated from scratch
The algorithm (Multi-Exponential Moving Average Convergence Divergence) developed by J. Ross Cameron and George C. Lapp is a popular technical indicator that is used in various financial markets to analyze the trend and dynamics. In this article we will examine how the MACD values ​​are calculated from scratch using Python.
What is MacD?
The MACD calculation includes two main components:
- The EMA (exponential sliding average) of the HMA (heavenly proportional moving average)
- The convergence line (C-line)
The formula for calculating MACD can be divided into several steps:
HMA calculation
In order to calculate the HMA, we have to carry out an exponential smoothing calculation for the closing course of the financial value.
`Python
Import nump as an NP
Def HMA (prices, alpha):
"" ""
Calculate the exponential moving average (EMA) of a list of prices.
Parameter:
Prices (list): List of final prices.
Alpha (Float): EMA smoothing factor.
Returns:
List: List of EMA values.
"" ""
n = len (prices)
Hma_values ​​= []
for i within reach (s):
Hma_values.append (alpha prices [i] + (1 - alpha) hma_values ​​[-1])
Return np.array (hma_values)
Def HMA smoothes (prices, alpha, Window_Size):
"" ""
Calculate the exponential sliding average (EMA) of a list of prices using a moving average.
Parameter:
Prices (list): List of final prices.
Alpha (Float): EMA smoothing factor.
Window_Size (Int): Size of the sliding 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 = 'equal')))
Gives HMA_Values ​​back
MACD calculation
The MACD calculation includes the following steps:
- Calculate the EMA of the HMA.
- Calculate the C line by average the two EMA values.
“ Python
DEF MACD (prices, Window_Size):
“” “”
Calculate the multi-exponential gliding average convergence-diversity algorithm (MACD).
Parameter:
Prices (list): List of final 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 ​​= []
For i within reach (len (eMa_values) – Wester_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 smoothes (prices, alpha, Window_Size):
“” “”
Calculate the exponential sliding average (EMA) of a list of prices using a moving average.
Parameter:
Prices (list): List of final prices.
Alpha (Float): EMA smoothing factor.
Window_Size (Int): Size of the sliding 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 = ‘equal’)))
hma_values ​​= np.roll (hma_values, -window_size)
Gives HMA_Values ​​back
DEF EMA_SMOOTED (EMA_Values, Alpha, Window_Size):
“” “”
Calculate the exponential sliding average (EMA) of a list of EMA values ​​using a sliding average.
Parameter:
EMA_Values ​​(list): List of EMA values.
Alpha (Float): EMA smoothing factor.
Window_Size (Int): Size of the sliding average window.
Returns:
List: List of the smoothed EMA values.
“” “”
n = len (ema_values)
Hma_values ​​= hma_smoothed (EMA_Values, 3, Window_Size)
Hma_values ​​= np.convolve (hma_values, np.