I'm glad that I can share my issue with you all and looking forward to learning from you all.
My current issue is with def calculating_hma
where I can't get the right results:
#python27
#inputs
period = 9
Coin_pair = "USD-BTC"
Unit = thirtyMin''
def getClosingPrices(coin_pair, period, unit):
historical_data = api.getHistoricalData(coin_pair, period, unit)
closing_prices = []
for i in historical_data:
closing_prices.append(i['C'])
return closing_prices
def calculate_sma(coin_pair, period, unit):
total_closing = sum(getClosingPrices(coin_pair, period, unit))
return (total_closing / period)
def calculate_ema(coin_pair, period, unit):
closing_prices = getClosingPrices(coin_pair, period, unit)
previous_EMA = calculate_sma(coin_pair, period, unit)
constant = (2 / (period + 1))
current_EMA = (closing_prices[-1] * (2 / (1 + period))) + (previous_EMA * (1 - (2 / (1 + period))))
def calculate_hma(coin_pair, period, unit):
"""
Hull Moving Average.
Formula:
HMA = WMA(2*WMA(n/2) - WMA(n)), sqrt(n)
"""
# MY Try of calculation ?
ma = calculate_sma(coin_pair, period, unit)
HMA = ma(2*ma(period/2) - ma(period)), sqrt(period)
# my question ?
# where to use the unit and pierod and coin_pair in the calculation ?
# check inputs above
return hma
ema = calculate_ema(market, period=9, unit=timeframe)
sma = calculate_sma(market, period=9, unit=timeframe)
hma = calculate_sma(market, period=9, unit=timeframe) ?
print (ema)
print (sma)
print (hma)
Moving averages are usually defined with a signature of
ma(series) -> series
. I think a big part of your confusion roots in WMA being defined to return a series, not a single value as you are expecting.A good, formal definition of the problem can be found here: https://oxfordstrat.com/trading-strategies/hull-moving-average/
here's a python implementation of a single-point HMA: