最大回撤原理與計(jì)算-python實(shí)現(xiàn)

最大回撤

最大回撤是指:在任一時(shí)間點(diǎn)向后推,產(chǎn)品凈值到達(dá)最低點(diǎn)時(shí),收益率回撤幅度的最大值。這一指標(biāo)描述了投資者買(mǎi)入某資產(chǎn)可能出現(xiàn)的最為糟糕的情況。

公式:maxdrawdown = min_{i\leq j } (x_j - x_i) = min_{j}(x_j - max_{i\leq j} x_i)

即,最大回撤是在每一個(gè)時(shí)間點(diǎn)上向后求其跌幅,然后找出最大的。從公式的第二個(gè)等式,我們首先給出最大回撤的一個(gè)直觀算法

最大回撤的算法

一、首先給出計(jì)算最大回撤的一個(gè)算法(先找出累計(jì)收益率的波峰點(diǎn),在向后尋找最大的跌幅,即為最大回撤)

  1. 將累計(jì)收益率按照日期進(jìn)行排列;
  2. 接著找到所有波峰的點(diǎn),即當(dāng)日的累計(jì)收益率大于前一日累計(jì)收益率的點(diǎn),并用數(shù)組記錄下來(lái),命名為maxcum
  3. 接著在第二步得到的每一個(gè)"波峰"處,向后尋找跌幅最大的那個(gè)點(diǎn),即為最大回撤
import numpy as np
def MaxDrawdown(return_list):
    
    # 1. find all of the peak of cumlative return 
    maxcum = np.zeros(len(return_list))
    b = return_list[0]
    for i in range(0,len((return_list))):
        if (return_list[i]>b):
            b = return_list[i]
        maxcum[i] = b
    
    # 2. then find the max drawndown point
    i = np.argmax((maxcum-return_list)/maxcum) 
    if i == 0:
        return 0
    j = np.argmax(return_list[:i])   
    
    # 3. return the maxdrawndown
    return (return_list[j]-return_list[i])/return_list[j]

二、接著,使用np.maximum.accumulate函數(shù)計(jì)算最大回撤

import numpy as np
def MaxDrawdown(return_list):
    i = np.argmax((np.maximum.accumulate(return_list)- return_list)/np.maximum.accumulate(return_list))
    if i == 0:
        return 0
    j = np.argmax(return_list[:i])
    return(return_list[j] - return_list[i]) / return_list[j],j,i

對(duì)滬深300指數(shù)計(jì)算累計(jì)收益率,并計(jì)算最大回撤

import pandas as pd
import matplotlib.pyplot as plt

# import data and cleaning
data0 = pd.read_csv('F:\\5_quant\\Chapter7_Algorithms and Numerical Methods\\300data.csv')
data0.index = pd.to_datetime(data0['Date'])
data0 = data0.dropna()

# calculate the cummulated return of HS300
data0['pct_chg'] = data0['pct_chg']/100 + 1
data0['retcum'] = data0['pct_chg'].cumprod()
plt.plot(data0['retcum'])
HS300cum.png

接著計(jì)算滬深300指數(shù)的最大回撤

drawndown,startdate,enddate = MaxDrawdown(data0['retcum'])

最大回撤結(jié)果為:72.30%。最大回撤開(kāi)始日期為:
Timestamp('2007-10-16 00:00:00')
最大回撤結(jié)束日期為:
Timestamp('2008-11-04 00:00:00')

可以看出,直接投資指數(shù)會(huì)有較大的回撤。需要進(jìn)一步地控制風(fēng)險(xiǎn)

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容