|
楼主 所提的是一个金融投资方面的问题,用一般的excel计算方法去解答可能不全面,推荐http://zhiqiang.org/blog/science/computer-science/max-drawdown-algorithm.html,网页的主人是一位高手,中信证券风险管理的副总裁,PHd,奥数金牌得主,他写了几个程序,实用性很强,其核心的算法如下,和8楼的略有不同,只循环一次即可,效率增强了:- 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
复制代码 |
|