加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

【数学】转载:最大回撤和最大短期回撤的线性算法

(2012-05-29 08:30:48)
标签:

回撤

对冲基金

波动率

指标

杂谈

分类: 数学
转载:http://zhiqiang.org/blog/science/computer-science/max-drawdown-algorithm.html

最大回撤是一个重要的风险指标。对于对冲基金和数量化策略交易,这个指标比波动率还重要。

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


0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有