亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何用Python計算赫爾移動平均線?

如何用Python計算赫爾移動平均線?

月關寶盒 2023-12-26 14:46:58
我很高興能與大家分享我的問題,并期待向大家學習。我當前的問題是def calculating_hma無法獲得正確的結果:#python27#inputs period = 9Coin_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_pricesdef 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 hmaema = 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)
查看完整描述

3 回答

?
躍然一笑

TA貢獻1826條經驗 獲得超6個贊

使用 Pandas 系列可以輕松解決這個問題。整個公式:


HMA = WMA(2*WMA(period/2) - WMA(period)), sqrt(period))

給定一個輸入序列 s 和一個句點可以打包成一行:


import pandas as pd

import numpy as np


HMA = s.rolling(period//2).apply(lambda x: ((np.arange(period//2) + 1)*x).sum()/(np.arange(period//2) + 1).sum(), raw=True).multiply(2).sub(

                        s.rolling(period).apply(lambda x: ((np.arange(period) + 1)*x).sum()/(np.arange(period) + 1).sum(), raw=True)

                ).rolling(int(np.sqrt(period))).apply(lambda x: ((np.arange(int(np.sqrt(period))) + 1)*x).sum()/(np.arange(int(np.sqrt(period))) + 1).sum(), raw=True)

但為了清晰和方便起見,最好定義兩個函數:


def WMA(s, period):

       return s.rolling(period).apply(lambda x: ((np.arange(period)+1)*x).sum()/(np.arange(period)+1).sum(), raw=True)


def HMA(s, period):

       return WMA(WMA(s, period//2).multiply(2).sub(WMA(s, period)), int(np.sqrt(period)))



查看完整回答
反對 回復 2023-12-26
?
波斯汪

TA貢獻1811條經驗 獲得超4個贊

移動平均線通常用 的簽名來定義ma(series) -> series。我認為您的困惑很大一部分根源在于 WMA 被定義為返回一個系列,而不是您所期望的單個值。

這是單點 HMA 的 python 實現:

def weighted_moving_average(series: List[float], lookback: Optional[int] = None) -> float:

? ? if not lookback:

? ? ? ? lookback = len(series)

? ? if len(series) == 0:

? ? ? ? return 0

? ? assert 0 < lookback <= len(series)


? ? wma = 0

? ? lookback_offset = len(series) - lookback

? ? for index in range(lookback + lookback_offset - 1, lookback_offset - 1, -1):

? ? ? ? weight = index - lookback_offset + 1

? ? ? ? wma += series[index] * weight

? ? return wma / ((lookback ** 2 + lookback) / 2)



def hull_moving_average(series: List[float], lookback: int) -> float:

? ? assert lookback > 0

? ? hma_series = []

? ? for k in range(int(lookback ** 0.5), -1, -1):

? ? ? ? s = series[:-k or None]

? ? ? ? wma_half = weighted_moving_average(s, min(lookback // 2, len(s)))

? ? ? ? wma_full = weighted_moving_average(s, min(lookback, len(s)))

? ? ? ? hma_series.append(wma_half * 2 - wma_full)

? ? return weighted_moving_average(hma_series)


查看完整回答
反對 回復 2023-12-26
?
慕虎7371278

TA貢獻1802條經驗 獲得超4個贊

解決了


def calculate_hma(coin_pair, period, unit):


    HMA = ((calculate_wma(coin_pair, int(period / 2), unit) * 2 - calculate_wma(coin_pair, period, unit)) + (

        calculate_wma(coin_pair, int(math.sqrt(period)), unit))) / 2


查看完整回答
反對 回復 2023-12-26
  • 3 回答
  • 0 關注
  • 217 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號