日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢(xún)
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
使用Python實(shí)現(xiàn)HullMovingAverage(HMA)

赫爾移動(dòng)平均線(Hull Moving Average,簡(jiǎn)稱(chēng)HMA)是一種技術(shù)指標(biāo),于2005年由Alan Hull開(kāi)發(fā)。它是一種移動(dòng)平均線,利用加權(quán)計(jì)算來(lái)減少滯后并提高準(zhǔn)確性。

創(chuàng)新互聯(lián)是一家專(zhuān)注于網(wǎng)站建設(shè)、網(wǎng)站制作與策劃設(shè)計(jì),固安網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專(zhuān)注于網(wǎng)站建設(shè)10年,網(wǎng)設(shè)計(jì)領(lǐng)域的專(zhuān)業(yè)建站公司;建站業(yè)務(wù)涵蓋:固安等地區(qū)。固安做網(wǎng)站價(jià)格咨詢(xún):028-86922220

HMA對(duì)價(jià)格變動(dòng)非常敏感,同時(shí)最大程度地減少短期波動(dòng)可能產(chǎn)生的噪音。它通過(guò)使用加權(quán)計(jì)算來(lái)強(qiáng)調(diào)更近期的價(jià)格,同時(shí)平滑數(shù)據(jù)。

計(jì)算HMA的公式涉及三個(gè)步驟。首先,使用價(jià)格數(shù)據(jù)計(jì)算加權(quán)移動(dòng)平均線。然后,使用第一步的結(jié)果計(jì)算第二個(gè)加權(quán)移動(dòng)平均線。最后,使用第二步的結(jié)果計(jì)算第三個(gè)加權(quán)移動(dòng)平均線。最終計(jì)算的結(jié)果就是移動(dòng)赫爾平均線。

WMA_1 =一段時(shí)期內(nèi)價(jià)格的加權(quán)移動(dòng)平均值(WMA) /2

WMA_2 =價(jià)格在一段時(shí)間內(nèi)的WMA

HMA_non_smooth = 2 * WMA_1 - WMA_2

HMA = HMA_non_smooth的WMA除以根號(hào)(周期)

在下面的文章中,我們將介紹如何使用Python實(shí)現(xiàn)HMA。本文將對(duì)計(jì)算WMA的兩種方法進(jìn)行詳細(xì)比較。然后介紹它在時(shí)間序列建模中的作用。

Python實(shí)現(xiàn)HMA

方法1:將WMA計(jì)算為按時(shí)期加權(quán)的移動(dòng)平均價(jià)格:

def hma(period):
wma_1 = df['Adj Close'].rolling(period//2).apply(lambda x: \
np.sum(x * np.arange(1, period//2+1)) / np.sum(np.arange(1, period//2+1)), raw=True)
wma_2 = df['Adj Close'].rolling(period).apply(lambda x: \
np.sum(x * np.arange(1, period+1)) / np.sum(np.arange(1, period+1)), raw=True)
diff = 2 * wma_1 - wma_2
hma = diff.rolling(int(np.sqrt(period))).mean()
return hma
period = 20
df['hma'] = hma(period)
df['sma_20days'] = df['Adj Close'].rolling(period).mean()
figsize = (10,6)
df[['Adj Close','hma','sma_20days']].plot(figsize=figsize)
plt.title('Hull Moving Average {0} days'.format(period))
plt.show()

如圖所示,HMA比通常的SMA反應(yīng)更快:

還可以嘗試更短的時(shí)間框架,看看HMA與價(jià)格曲線的關(guān)系有多密切。

df['hma_short']=hma(14)
df['hma_long']=hma(30)
figsize = (12,6)
df[['Adj Close','hma_short','hma_long']].plot(figsize=figsize)
plt.title('Hull Moving Average')
plt.show()

方法2,使用體量計(jì)算加權(quán)平均值:

def hma_volume(period):
wma_1 = df['nominal'].rolling(period//2).sum()/df['Volume'].rolling(period//2).sum()
wma_2 = df['nominal'].rolling(period).sum()/df['Volume'].rolling(period).sum()
diff = 2 * wma_1 - wma_2
hma = diff.rolling(int(np.sqrt(period))).mean()
return hma
df['nominal'] = df['Adj Close'] * df['Volume']
period = 20
df['hma_volume']=hma_volume(period)
figsize=(12,8)
fig, (ax0,ax1) = plt.subplots(nrows=2, sharex=True, subplot_kw=dict(frameon=True),figsize=figsize)
df[['Adj Close','hma_volume','hma']].plot(ax=ax0)
ax0.set_title('HMA Volume vs HMA period')
df[['Volume']].plot(ax=ax1)
ax1.set_title('Hull Moving Average')
plt.show()

體量的HMA比第一種方法計(jì)算的HMA稍滯后:

策略的回溯測(cè)試

為了回測(cè)每種策略(方法1和2),我們將計(jì)算一個(gè)短期和一個(gè)長(zhǎng)期的HMA:

當(dāng)短線超過(guò)長(zhǎng)線時(shí),可以觸發(fā)買(mǎi)入指令。當(dāng)短線低于長(zhǎng)線時(shí),就會(huì)觸發(fā)賣(mài)出指令。

然后我們計(jì)算每個(gè)信號(hào)產(chǎn)生的pnl。

方法1:

#SIGNAL
df['hma_short']=hma(20)
df['hma_long']=hma(30)
df['signal'] = np.where(df['hma_short'] > df['hma_long'],1,-1)

#RETURN
df['signal_shifted']=df['signal'].shift()

## Calculate the returns on the days we trigger a signal
df['returns'] = df['Adj Close'].pct_change()

## Calculate the strategy returns
df['strategy_returns'] = df['signal_shifted'] * df['returns']

## Calculate the cumulative returns
df1=df.dropna()
df1['cumulative_returns'] = (1 + df1['strategy_returns']).cumprod()

#PLOT
figsize=(12,8)
fig, (ax0,ax1) = plt.subplots(nrows=2, sharex=True, subplot_kw=dict(frameon=True),figsize=figsize)
df[['Adj Close','hma_long','hma_short']].plot(ax=ax0)
ax0.set_title("HMA: Short vs Long")

df[['signal']].plot(ax=ax1,style='-.',alpha=0.4)
ax1.legend()
ax1.set_title("HMA - Signals")
plt.show()

df1['cumulative_returns'].plot(figsize=(10,4))
plt.title("Cumulative Return")
plt.show()

你可以看到每次產(chǎn)生的信號(hào)都有一條交叉線:

在數(shù)據(jù)集的整個(gè)時(shí)間段內(nèi)產(chǎn)生的總體回報(bào)是正的,即使在某些時(shí)期它是負(fù)的:

回報(bào)率:

df1['cumulative_returns'].tail()[-1]
#1.0229750801053696

方法2:

#SIGNAL
df['hma_volume_short']=hma_volume(20)
df['hma_volume_long']=hma_volume(30)
df['signal'] = np.where(df['hma_volume_short'] > df['hma_volume_long'],1,-1)

#RETURN
df['returns'] = df['Adj Close'].pct_change()

## Calculate the strategy returns
df['strategy_returns'] = df['signal'].shift() * df['returns']

## Calculate the cumulative returns
df2=df.dropna()
df2['cumulative_returns_volume'] = (1 + df2['strategy_returns']).cumprod()

# PLOT
figsize=(12,8)
fig, (ax0,ax1) = plt.subplots(nrows=2, sharex=True, subplot_kw=dict(frameon=True),figsize=figsize)
df[['Adj Close','hma_volume_short','hma_volume_long']].plot(ax=ax0)
df[['signal']].plot(ax=ax1,style='-.',alpha=0.4)
ax0.set_title("HMA - Volume: Short vs Long")
ax1.legend()
plt.title("HMA - Signals")
plt.show()

figs = (10,4)
df2['cumulative_returns_volume'].plot(figsize = figs)
plt.title("Cumulative Return")
plt.show()

看起來(lái)比第一種方法中的HMA更平滑,可以觸發(fā)的信號(hào)更少(在我們的例子中只有1個(gè)):

這種策略產(chǎn)生的回報(bào)不是很好:0.75(0.775-1?-24%)

df2['cumulative_returns_volume'].tail()[-1]
#0.7555329108482581

我們來(lái)比較兩種策略的信號(hào):

df['signal'] = np.where(df['hma_short'] > df['hma_long'],1,-1)
df['signal_volume'] = np.where(df['hma_volume_short'] > df['hma_volume_long'],1,-1)
figsize=(12,8)
df[['signal','signal_volume']].plot(figsize=figsize)
plt.show()

空頭頭寸的信號(hào)比多頭頭寸更多:

所以?xún)H使用HMA還不足以產(chǎn)生有利可圖的策略。我們可以使用相對(duì)強(qiáng)弱指數(shù)(RSI)和隨機(jī)指數(shù)(Stochastic Oscillator等其他指標(biāo)來(lái)確認(rèn)交易信號(hào)。但是對(duì)于時(shí)間序列來(lái)說(shuō),HMA是一個(gè)很好的特征工程的方法。

HMA信號(hào)的一些解釋

  • 交叉信號(hào):當(dāng)價(jià)格越過(guò)HMA上方時(shí),可以解釋為看漲信號(hào),當(dāng)價(jià)格越過(guò)HMA下方時(shí),可以解釋為看空信號(hào)。它也可以觸發(fā)買(mǎi)入和賣(mài)出信號(hào),正如我們之前已經(jīng)看到的。(上圖點(diǎn)1)。
  • 趨勢(shì)跟蹤信號(hào):HMA也可用于識(shí)別趨勢(shì)并生成趨勢(shì)跟蹤信號(hào)。當(dāng)HMA傾斜向上時(shí),它表示上升趨勢(shì),當(dāng)它傾斜向下時(shí),它表示下降趨勢(shì)(上圖點(diǎn)2)。
  • 反轉(zhuǎn)信號(hào):當(dāng)價(jià)格從下方接近HMA時(shí),看漲反轉(zhuǎn)趨勢(shì)可能在不久的將來(lái)發(fā)生(上圖點(diǎn)3)。

HMA在時(shí)間序列建模的作用

HMA在時(shí)間序列建模中的作用主要是作為一個(gè)平滑濾波器,可以在一定程度上減少噪聲并提高時(shí)間序列預(yù)測(cè)的準(zhǔn)確性。在時(shí)間序列建模中,經(jīng)常需要對(duì)數(shù)據(jù)進(jìn)行平滑處理,以消除異常值和噪聲,同時(shí)保留趨勢(shì)和季節(jié)性變化的信號(hào)。HMA是一種有效的平滑濾波器,它通過(guò)加權(quán)平均的方式來(lái)計(jì)算平均值,并對(duì)較早的數(shù)據(jù)施加更大的權(quán)重,從而可以更準(zhǔn)確地捕捉趨勢(shì)性信號(hào)。

除了作為一個(gè)平滑濾波器,HMA還可以作為一個(gè)特征提取器來(lái)提取時(shí)間序列中的特征,并用于建立預(yù)測(cè)模型。例如,可以使用HMA計(jì)算時(shí)間序列中的趨勢(shì)和季節(jié)性變化,并將其作為輸入特征用于構(gòu)建ARIMA、VAR或LSTM等預(yù)測(cè)模型。

總結(jié)

HMA不僅在交易中有廣泛的應(yīng)用,也是一種有用的時(shí)間序列分析工具。HMA作為一種移動(dòng)平均線,可以減少時(shí)間序列中的噪聲和突發(fā)性變化,從而更準(zhǔn)確地捕捉數(shù)據(jù)的趨勢(shì)性和周期性變化。在時(shí)間序列分析中,HMA通常用于平滑處理數(shù)據(jù),以提高預(yù)測(cè)的準(zhǔn)確性。在實(shí)際應(yīng)用中,HMA常常與其他技術(shù)指標(biāo)和時(shí)間序列分析方法相結(jié)合,在各種數(shù)據(jù)分析和預(yù)測(cè)任務(wù)中獲取更好的預(yù)測(cè)結(jié)果。


標(biāo)題名稱(chēng):使用Python實(shí)現(xiàn)HullMovingAverage(HMA)
URL分享:http://m.5511xx.com/article/ccicido.html