为什么包PerformanceAnalytics中的maxDrawdown函数返回错误的结果?

时间:2015-08-24 10:03:09

标签: r xts performanceanalytics

我想在PerformanceAnalytics包中使用maxDrawdown函数来计算最大亏损,但发现它总是返回零(不是)。

我像这样使用maxDrawdown

> maxDrawdown(my.xts)
[1] 0

我的xts是这样的:

> my.xts
           value
2004-06-16 4.150
2004-06-17 4.225
2004-06-18 4.025
2004-06-21 4.000
2004-06-23 4.425
2004-06-24 4.450
2004-06-25 4.400
2004-06-28 4.325
2004-06-29 4.325
2004-06-30 4.300
2004-07-02 4.350
2004-07-06 4.400
2004-07-07 4.275
2004-07-08 4.100
2004-07-09 4.075
2004-07-12 4.000
2004-07-13 4.025
2004-07-14 3.800
2004-07-15 3.675
2004-07-16 3.700

str(my.xts)返回

An ‘xts’ object on 2004-06-16/2004-07-16 containing:
  Data: num [1:20, 1] 4.15 4.22 4.03 4 4.42 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr "value"
  Indexed by objects of class: [POSIXct,POSIXt] TZ:-
  xts Attributes:--
 NULL

并且,我编写了一个验证函数

my.mmd <- function(v) {
  max(1 - v / cummax(v))
}

结果不是零。

> my.mmd(my.xts$value)
[1] 0.1741573

为什么maxDrawdown返回0?

我使用http://www.investopedia.com/terms/m/maximum-drawdown-mdd.asp构建案例。

other.xts <- xts(c(500,750,400,600,350, 800), Sys.Date() + 1:6)
maxDrawdown(other.xts) # --> here still returns 0!
my.mmd(other.xts)  # -> my function returns 0.5333333, seems right.

也许我以错误的方式使用maxDrawdown函数!但我再看一遍doc()仍然无法得到它。我错过了使用这个功能的东西吗?

1 个答案:

答案 0 :(得分:2)

您的xts对象包含价格数据与退货,这就是您获得maxDrawdown = 0

的原因

例如:

 # Using Prices instead of returns to calculate Drawdowns
a <- Ad(getSymbols("AAPL", auto.assign = FALSE))

head(a)
           AAPL.Adjusted
2007-01-03      11.19449
2007-01-04      11.44295
2007-01-05      11.36147
2007-01-08      11.41757
2007-01-09      12.36603
2007-01-10      12.95782

maxDrawdown(head(a))
[1] 0

现在使用退货:

# Nice wrapper to calculate returns via PerformanceAnalytics
ret <-CalculateReturns(head(a), "discrete")

> head(ret)
           AAPL.Adjusted
2007-01-03            NA
2007-01-04   0.022195570
2007-01-05  -0.007121151
2007-01-08   0.004938271
2007-01-09   0.083070194
2007-01-10   0.047855606
> maxDrawdown(ret)
[1] 0.007121151