Ethereum: calculating macd from scratch in python

Calculate MACD from scratch in Python

Developed by J. Ross Cameron and George C. Lapp, the Multi-Exponential Moving Average Convergence Divergence (MACD) algorithm is a popular technical indicator used in various financial markets to analyze trends and momentum. In this article, we will explore how to calculate MACD values ​​from scratch in Python.

What is MACD?

The MACD calculation includes two main components:

  • EMA (Exponential Moving Average) of the HMA (Heavenly Proportional Moving Average)
  • Convergence Line (C-Line)

The MACD calculation formula can be divided into several steps:

HMA Calculation

Ethereum: calculating macd from scratch in python

To calculate the 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 the price list.

Parameter:

prices(list): List of closing prices.

alpha(float): EMA smoothing factor.

Returns:

list: List of EMA values.

"""

n = len(prices)

hma_values ​​​​​​​​= []

i 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 the price list using the moving average.

Parameter:

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 EMA of the HMA.
  • Calculate the C-line by averaging the two EMA values.

“` python

def macd(prices, window_size):

“””

Calculate the Multi-Exponentential Moving Average Convergence Divergence (MACD) algorithm.

Parameter:

prices (list): List of closing prices.

window_size (int): Size of the MACD signal.

Return value:

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 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 list of prices 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(windowsize) / windowsize, mode=’equal’)

hma_values ​​​​​​​​= np.roll(hma_values, -window_size)

returns hma_values

def ema_smoothed(ema_values, Alpha, WindowSize):

“””

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 = length(mother_values)

hma_values ​​​​​​​​= hma_smoothed(ema_values, 3, window_size)

hma_values ​​​​​​​​​​= np.convolve(hma_values, np.

Comments

Bir yanıt yazın

E-posta adresiniz yayınlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir