简体   繁体   English

如何修改PerformanceAnalytics程序包中的提取函数以获得价值

[英]How to modify drawdown functions in PerformanceAnalytics package for value

I am calculating the average drawdown, average length, recovery length, etc. in R for a PnL data series rather than return data. 我正在为PnL数据系列而不是返回数据计算R中的平均跌幅,平均长度,恢复长度等。 This is data frame like this 这是这样的数据帧

            PNL
2008-11-03  3941434
2008-11-04  4494446
2008-11-05  2829608
2008-11-06  2272070
2008-11-07 -2734941
2008-11-10 -2513580

I used the maxDrawDown function from fTrading package and it worked. 我使用了fTrading包中的maxDrawDown函数,它起作用了。 How could I get the other drawdown functions? 我如何获得其他提款功能? If I directly run AverageDrawdown(quantbook) function, it will give out error message like this 如果我直接运行AverageDrawdown(quantbook)函数,它将发出这样的错误消息

Error in if (thisSign == priorSign) { : missing value where TRUE/FALSE needed  

I checked the documentation for AverageDrawdown and it is as below: 我检查了AverageDrawdown的文档,如下所示:

findDrawdowns(R, geometric = TRUE, ...)

R    an xts, vector, matrix, data frame, timeSeries or zoo object of asset returns

My quantbook is a data frame but doesn't work for this function. 我的quantbook是一个数据框,但不适用于此功能。 Or do you have anything other packages to get the same funciton, please advise. 还是您还有其他获得相同功能的软件包,请告知。

I've modified the package's functions. 我已经修改了程序包的功能。 Here is one solution in PnL case (or any other case you want to get the value rather than the return) and hope you find it useful. 这是PnL情况下的一种解决方案(或您希望获取值而不是返回值的任何其他情况),并希望您发现它有用。 The parameter x is a dataframe and the row.names for x are dates so you don't bother to convert amongst different data types (which I actually suffer a lot). 参数x是一个dataframe row.names ,而xrow.namesdates因此您不必费心在不同的数据类型之间进行转换(实际上我受了很多苦)。 With the function findPnLDrawdown , you could perform a lot other functions to calculate averageDrawDown , averageLength , recovery , etc. 使用函数findPnLDrawdown ,您可以执行许多其他函数来计算averageDrawDownaverageLengthrecovery等。

PnLDrawdown <- function(x) {
  ts = as.vector(x[,1])
  cumsum = cumsum(c(0, ts))
  cmaxx = cumsum - cummax(cumsum)
  cmaxx = cmaxx[-1]
  cmaxx = as.matrix(cmaxx)
  row.names(cmaxx) = row.names(x)
  cmaxx = timeSeries(cmaxx)
  cmaxx
}


findPnLDrawdown <- function(R) {
  drawdowns = PnLDrawdown(R)
  draw = c()
  begin = c()
  end = c()
  length = c(0)
  trough = c(0)
  index = 1
  if (drawdowns[1] >= 0) {
    priorSign = 1
  } else {
    priorSign = 0
  }
  from = 1
  sofar = as.numeric(drawdowns[1])
  to = 1
  dmin = 1
  for (i in 1:length(drawdowns)) {
    thisSign =ifelse(drawdowns[i] < 0, 0, 1)
    if (thisSign == priorSign) {
      if (as.numeric(drawdowns[i]) < as.numeric(sofar)) {
        sofar = drawdowns[i]
        dmin = i
      }
      to = i+ 1
    }
    else {
      draw[index] = sofar
      begin[index] = from
      trough[index] = dmin
      end[index] = to
      from = i
      sofar = drawdowns[i]
      to = i + 1
      dmin = i
      index = index + 1
      priorSign = thisSign
    }
  }
  draw[index] = sofar
  begin[index] = from
  trough[index] = dmin
  end[index] = to
  list(pnl = draw, from = begin, trough = trough, to = end, 
       length = (end - begin + 1),
       peaktotrough = (trough - begin + 1),
       recovery = (end - trough))
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用PerformanceAnalytics套件计算风险价值 - Calculating Value at Risk with performanceanalytics package PerformanceAnalytics + data.frame:使用包函数时的格式问题 - PerformanceAnalytics + data.frame: Formatting issue when using package functions PerformanceAnalytics 包中的某些函数在与 zoo::rollapply 一起使用时失败 - Some functions in PerformanceAnalytics package are failing when use with zoo::rollapply 如何更改 RStudio 中 PerformanceAnalytics 包生成的绘图的大小(宽度和高度)? - How to change size (width & height) of a plot produced by PerformanceAnalytics package in RStudio? 如何在图表中覆盖多个时间序列.R中的包PerformanceAnalytics的滚动性能 - How to overlay multiple timeseries in chart.RollingPerformance of package PerformanceAnalytics in R 使用textplot()进行文本对齐(PerformanceAnalytics软件包) - text alignment with textplot() (PerformanceAnalytics package) 从PerformanceAnalytics包中保存图表对象 - Save chart object from performanceanalytics package R包'performanceanalytics'优化器中的最大资产数量 - Maximum number of assets in R package 'performanceanalytics' optimizer 使用“PerformanceAnalytics”包来计算性能度量 - Using 'PerformanceAnalytics' package to calculate Performance Measures 使用PerformanceAnalytics包下标超出范围 - Subscript out of bound using the PerformanceAnalytics package
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM