最大回撤是一个重要的风险指标。对于对冲基金和数量化策略交易,这个指标比波动率还重要。
1.最大回撤
http://s14/middle/8654c87agc122107df6fd&690
3.VBA程序
对上面的算法做了实现,见下面的Excel模板,内含函数演示。VBA代码有详细注释。
详细VBA算法程序,参见:http://zhiqiang.org/blog/science/computer-science/max-drawdown-algorithm.html
drawdown(x) compute the max
drawdown of an array
drawup(x)
compute the max drawdown of an array
Function
Drawdown(series As Variant, _
Optional inputType As String = "nav",
_
Optional downtype As String = "absolute") As
Double
Dim i As
Long, s As Long, e As Long
' when the input data is an Excel range, convert
it to an array (two dimensions)
If IsObject(series) Then
If
series.Rows().Count = 1 Then
Drawdown =
Drawdown(series.Transpose().Value(), inputType,
downtype)
Else
Drawdown =
Drawdown(series.Value(), inputType, downtype)
End
If
Exit
Function
End If
' convert the three other case into the case
of
' Drawdown(nav_series, 'nav',
'absolute')
PrepareDraw series, inputType,
downtype
Dim res As Double, max As
Double, min As Double
s = LBound(series, 1)
e = UBound(series, 1)
res = 0
max = series(s, 1)
If start = True Then
If
series(s, 1) < 0 Then res = series(s, 1): max =
0
End If
For i = s + 1 To e
If
series(i, 1) - max < res Then
res = series(i, 1) -
max
ElseIf
series(i, 1) > max Then
max = series(i,
1)
End
If
Next i
' if the downtype is relative, we need to
convert it the absolute case
If downtype = "absolute" Then
Drawdown =
res
Else
Drawdown =
Math.Exp(res) - 1
End If
End
Function
//////////////////////////////////////////////////////////
Function Drawup(series As Variant, _
Optional inputType As String
= "nav", Optional upType As String = "relative", _
Optional start As Boolean =
False) As Double
Dim i As
Long, s As Long, e As Long
' when the
input data is an Excel range, convert it to an array (two
dimensions)
If IsObject(series) Then
If series.Rows().Count = 1 Then
Drawup =
Drawup(series.Transpose().Value(), inputType, upType)
Else
Drawup =
Drawup(series.Value(), inputType, upType)
End If
Exit Function
End If
' convert the three other
case into the case of
'
Drawup(nav_series, 'nav', 'absolute')
PrepareDraw series,
inputType, upType
' here we deal with the
case
'
Drawup(return_series, 'return', 'relative')
Dim res As Double, min As
Double
s = LBound(series, 1)
e = UBound(series, 1)
res = 0
min = series(s, 1)
If start = True Then
If series(s, 1) > 0 Then res =
series(s, 1): min = 0
End If
For i = s + 1 To e
If series(i, 1) - min > res
Then
res =
series(i, 1) - min
ElseIf series(i, 1) < min
Then
min =
series(i, 1)
End If
Next i
' if the upType is relative, we
need to convert it the absolute case
If upType = "absolute"
Then
Drawup = res
Else
Drawup = Math.Exp(res) - 1
End If
End Function