[英]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
,而x
的row.names
是dates
因此您不必费心在不同的数据类型之间进行转换(实际上我受了很多苦)。 With the function findPnLDrawdown
, you could perform a lot other functions to calculate averageDrawDown
, averageLength
, recovery
, etc. 使用函数findPnLDrawdown
,您可以执行许多其他函数来计算averageDrawDown
, averageLength
, recovery
等。
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.