最大回撤
最大回撤是指:在任一時(shí)間點(diǎn)向后推,產(chǎn)品凈值到達(dá)最低點(diǎn)時(shí),收益率回撤幅度的最大值。這一指標(biāo)描述了投資者買(mǎi)入某資產(chǎn)可能出現(xiàn)的最為糟糕的情況。
公式:
即,最大回撤是在每一個(gè)時(shí)間點(diǎn)上向后求其跌幅,然后找出最大的。從公式的第二個(gè)等式,我們首先給出最大回撤的一個(gè)直觀算法
最大回撤的算法
一、首先給出計(jì)算最大回撤的一個(gè)算法(先找出累計(jì)收益率的波峰點(diǎn),在向后尋找最大的跌幅,即為最大回撤)
- 將累計(jì)收益率按照日期進(jìn)行排列;
- 接著找到所有波峰的點(diǎn),即當(dāng)日的累計(jì)收益率大于前一日累計(jì)收益率的點(diǎn),并用數(shù)組記錄下來(lái),命名為
- 接著在第二步得到的每一個(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)