R,请检查我最长的回撤函数

发布于 2024-10-04 03:12:04 字数 1402 浏览 1 评论 0原文

我无法让 PerformanceAnalytics 与我的动物园系列一起使用,因此我决定编写自己的脚本。

如果您想计算最长的回撤,应该将 cummax(equity)-equity 作为输入。它还给出了这些期间的最大回撤值。

更正后的版本如下。

拜托,你能检查一下我的脚本吗?它没有按预期工作。有些 maxDD 为零。 我希望它对其他人有用。我在论坛上看到很多消息,人们在寻找类似的东西。

我已经用里奇的建议纠正了它:

  findDD <- function(DD, n=5){
  rr <- rle(sign(coredata(DD)))
  lens <- rr$length
  lens[!rr$value] <- 0
  ll <- head(order(lens, decreasing=TRUE),n)
  sumas <- cumsum(c(1,rr$length)) # I need to access the original lenghts 
  maxDD <- sapply(ll,FUN = function(x) max(window(DD,start=index(DD)[sumas[x]],end=index(DD)[sumas[x+1]-1])))
  data.frame(start=index(DD)[sumas[ll]],end=index(DD)[sumas[ll+1]-1], length=(index(DD)[sumas[ll+1]-1]-index(DD)[sumas[ll]])+1, maxDD)
}

我还纠正了一个阻止我获得有序答案的问题,因为我写的是索引(DD [])而不是索引(DD)[]

现在它似乎可以工作,但我'我不确定。

对约书亚: 一开始我的数据是带有 chron 索引的 Zoo 现在我已经将其转换为带有 posixct 索引的 xts,

"2010-01-11 18:00:00" 9338.37028375963
"2010-01-11 18:15:00" 8086.45780960387
"2010-01-11 18:30:00" 7762.75622449016
"2010-01-11 18:45:00" 8358.3609798313
"2010-01-11 19:00:00" 8598.69695502083
"2010-01-11 19:15:00" 8568.56256494502
"2010-01-11 19:30:00" 8488.4281748692
...

但仍然无法与 PerformanceAnalytics 一起使用,尽管我可以将其绘制成图表并自行进行任何计算。 Drawdown(myData) 给出一个 xts 系列,其所有数据值均为 NaN。 我一直在查看 findDrawdown 代码,它与我的不同,因为它测量相对回撤而不是绝对回撤。

无论如何,我希望我的脚本对某人有用。

I can't get performanceAnalytics to work with my zoo series and I've decided to write my own script.

It's supposed to get cummax(equity)-equity as input if you want to calculate de longest Drawdowns. It also gives the max drawdown value on these periods.

corrected version is below.

Please, Could you check my script. It doesn't work as expected. Some maxDD are zero.
I hope it can be useful to other people. I've seen many messages at forums where people looks for something like this.

I've corrected it with Richie suggestions:

  findDD <- function(DD, n=5){
  rr <- rle(sign(coredata(DD)))
  lens <- rr$length
  lens[!rr$value] <- 0
  ll <- head(order(lens, decreasing=TRUE),n)
  sumas <- cumsum(c(1,rr$length)) # I need to access the original lenghts 
  maxDD <- sapply(ll,FUN = function(x) max(window(DD,start=index(DD)[sumas[x]],end=index(DD)[sumas[x+1]-1])))
  data.frame(start=index(DD)[sumas[ll]],end=index(DD)[sumas[ll+1]-1], length=(index(DD)[sumas[ll+1]-1]-index(DD)[sumas[ll]])+1, maxDD)
}

I've also corrected a problem that prevented me from getting an ordered answer because I was writting index(DD[]) instead of index(DD)[]

Now it seems to work but I'm not sure.

to Joshua:
At the beginning my data was zoo with chron index
Now I've transformed it to xts with posixct index,

"2010-01-11 18:00:00" 9338.37028375963
"2010-01-11 18:15:00" 8086.45780960387
"2010-01-11 18:30:00" 7762.75622449016
"2010-01-11 18:45:00" 8358.3609798313
"2010-01-11 19:00:00" 8598.69695502083
"2010-01-11 19:15:00" 8568.56256494502
"2010-01-11 19:30:00" 8488.4281748692
...

still doesn't work with performanceAnalytics, though I can graph it and make any calculations by my own.
Drawdown(myData) gives a xts series with all its data values NaN.
I've been looking at findDrawdown code and it's different to mine because it measures relative drawdowns instead of absolute drawdowns.

Anyway, I hope my script could be useful to someone.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烟柳画桥 2024-10-11 03:12:04

您可以查看 tseries 包中的旧函数 maxdrawdown 。这是其手册页中的示例:

mxdrwdR> # Realistic example
mxdrwdR> data(EuStockMarkets)

mxdrwdR> dax <- log(EuStockMarkets[,"DAX"])

mxdrwdR> mdd <- maxdrawdown(dax)

mxdrwdR> mdd
$maxdrawdown
[1] 0.25647

$from
[1] 236

$to
[1] 331

You could look at the older function maxdrawdown from the tseries package. Here is an example from its manual page:

mxdrwdR> # Realistic example
mxdrwdR> data(EuStockMarkets)

mxdrwdR> dax <- log(EuStockMarkets[,"DAX"])

mxdrwdR> mdd <- maxdrawdown(dax)

mxdrwdR> mdd
$maxdrawdown
[1] 0.25647

$from
[1] 236

$to
[1] 331
忆依然 2024-10-11 03:12:04

我对最大回撤一无所知,但这里有一些关于您的代码的想法。


目前尚不完全清楚输入 DD 采用什么形式。您可能需要一些输入检查以确保其格式正确。


rr <- rle(coredata(sign(DD)))

除非您重载了此函数,否则 sign 将返回一个数字向量(包含 -1、0 和 1)。您的意思是 sign(coredata(DD)) 吗?


lens[rr$value == FALSE] <- 0

您定义了变量 lens,但稍后又返回使用 rr$length


rr$value == FALSE

使用 !rr$value 代替;更清楚了。


ll <- head(order(rr$length, decreasing=TRUE),5)

我不知道这对于该方法有多重要,但您可能希望允许用户输入在函数中使用最长运行的次数,而不是将其硬编码为 5。


当您保持一致时,代码更容易阅读与您放置空格的位置。就我个人而言,我更喜欢逗号后面的空格以及运算符前后的空格。

I don't know anything about max drawdowns, but here are some thoughts on your code.


It isn't entirely clear what form the input DD takes. You likely want some input checking to make sure that it is correctly formed.


rr <- rle(coredata(sign(DD)))

Unless you've overloaded this function, sign returns a numeric vector (containing -1s, 0s and 1s). Did you mean sign(coredata(DD))?


lens[rr$value == FALSE] <- 0

You define the variable lens but then go back to using rr$length later.


rr$value == FALSE

Use !rr$value instead; it's clearer.


ll <- head(order(rr$length, decreasing=TRUE),5)

I don't know how central this is to the method but you might want to allow the user to input how many of the longest runs are used into the function rather than hardcoding it to be 5.


Code is easier to read when you are consistent with where you put spaces. Personally, I prefer a space after a comma and spaces before and after operators.

べ映画 2024-10-11 03:12:04

仅当使用 chron 作为索引时,PerformanceAnalytics 中的函数才会失败。我之前曾建议过您不应依赖 chron 来获取 Zoo/xts 索引值。

该错误提示您为什么它不起作用:“行名应该具有标准日期格式,例如'1985-03-15'”。 chron 不使用标准日期格式,因此会出现错误。

library(quantmod)
library(PerformanceAnalytics)
library(chron)

getSymbols("SPY")
r <- na.omit(ROC(Cl(SPY)))
# xts object with 'Date' index
str(SPY)
table.Drawdowns(r)
table.Drawdowns(as.zoo(r))
# convert index to chron
index(r) <- as.chron(index(r))
table.Drawdowns(r)          # fails
table.Drawdowns(as.zoo(r))  # fails
# convert index to POSIXct
index(r) <- as.POSIXct(index(r))
table.Drawdowns(r)
table.Drawdowns(as.zoo(r))

The functions in PerformanceAnalytics only fail when using chron as the index. I've suggested to you before that you should not rely on chron for your zoo/xts index values.

The error gives you a hint why it doesn't work: "Rownames should have standard date formats, such as '1985-03-15'". chron does not use standard date formats, hence the error.

library(quantmod)
library(PerformanceAnalytics)
library(chron)

getSymbols("SPY")
r <- na.omit(ROC(Cl(SPY)))
# xts object with 'Date' index
str(SPY)
table.Drawdowns(r)
table.Drawdowns(as.zoo(r))
# convert index to chron
index(r) <- as.chron(index(r))
table.Drawdowns(r)          # fails
table.Drawdowns(as.zoo(r))  # fails
# convert index to POSIXct
index(r) <- as.POSIXct(index(r))
table.Drawdowns(r)
table.Drawdowns(as.zoo(r))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文