如何计算股票自 200 周期高点以来的周期

发布于 2024-12-03 19:17:34 字数 462 浏览 5 评论 0 原文

我想计算自单变量时间序列的 200 个周期高点以来经过的周期数。例如,这是 SPY 的收盘价:

require(quantmod)
getSymbols("SPY",from='01-01-1900')
Data <- Cl(SPY)

现在,我可以使用 quantmod 中的 Lag 函数找到该系列的 200 周期高点:

periodHigh <- function(x,n) {
    Lags <- Lag(x,1:n)
    High <- x == apply(Lags,1,max)
    x[High]
}
periodHigh(Data, 200)

但现在我陷入困境。如何将其合并回原始系列 (Data) 并计算该系列中的每个点自前一个 n 周期高点以来已经过去了多少个周期?

I would like to calculate the number of periods that have elapsed since the 200 period high of a univariate time series. For example, here's the closing price of SPY:

require(quantmod)
getSymbols("SPY",from='01-01-1900')
Data <- Cl(SPY)

Now, I can find the 200-period highs of this series using the Lag function in quantmod:

periodHigh <- function(x,n) {
    Lags <- Lag(x,1:n)
    High <- x == apply(Lags,1,max)
    x[High]
}
periodHigh(Data, 200)

But now I'm stuck. How do I merge this back onto the original series (Data) and calculate, for each point in the series, how many periods have elapsed since the previous n-period high?

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

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

发布评论

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

评论(3

oО清风挽发oО 2024-12-10 19:17:34

这个小函数返回一个列表,其中包含:

  • high 高日期的索引号
  • recentHigh 最近高日期的索引号
  • daysSince 日期的数量自上次高
  • 数据以来的天数,仅包含高天数的 xts 对象。对于绘图很有用。

代码:

daysSinceHigh <- function(data, days){
  highs <- days-1+which(apply(embed(data, days), 1, which.max)==1)
  recentHigh <- max(highs)
  daysSince <- nrow(data) - recentHigh
  list(
    highs=highs,
    recentHigh = recentHigh,
    daysSince = daysSince,
    data=data[highs, ])
}       

结果:

daysSinceHigh(Data, 200)$daysSince
[1] 90

plot(Data)
points(daysSinceHigh(Data, 200)$data, col="red")

在此处输入图像描述

This little function returns a list with:

  • high the index number of high dates
  • recentHigh the index number of the most recent high day
  • daysSince the number of days since the last high
  • data an xts object with only the high days. Useful for plotting.

The code:

daysSinceHigh <- function(data, days){
  highs <- days-1+which(apply(embed(data, days), 1, which.max)==1)
  recentHigh <- max(highs)
  daysSince <- nrow(data) - recentHigh
  list(
    highs=highs,
    recentHigh = recentHigh,
    daysSince = daysSince,
    data=data[highs, ])
}       

The results:

daysSinceHigh(Data, 200)$daysSince
[1] 90

plot(Data)
points(daysSinceHigh(Data, 200)$data, col="red")

enter image description here

ゞ花落谁相伴 2024-12-10 19:17:34

您修改后的问题的答案:

require(zoo)
x <- sample(300:500, 1000, replace=TRUE)
str(rollapply(x, 200, function(x) which.max(x)))
# int [1:801] 14 13 12 11 10 9 8 7 6 5 ...
 plot(x)
 plot(200:1000, rollapply(x, 200, function(x) 200-which.max(x)))

因此对于 xts 系列:

plot( rollapply(coredata(Data), 200, function(x) 200-which.max(x)))

在此处输入图像描述
显然,您无法将任何内容合并回前 200 个日期,除非您应用更宽松的滚动最大值定义。 (在另一个涉及“shifty”数据的SO会议中,我展示了如何使用嵌入来填充“尾随”句点:R 中的数据转换,但我不知道您是否想构造输入数据 200 倍大的矩阵。)

The answer to your revised question:

require(zoo)
x <- sample(300:500, 1000, replace=TRUE)
str(rollapply(x, 200, function(x) which.max(x)))
# int [1:801] 14 13 12 11 10 9 8 7 6 5 ...
 plot(x)
 plot(200:1000, rollapply(x, 200, function(x) 200-which.max(x)))

So for the xts series:

plot( rollapply(coredata(Data), 200, function(x) 200-which.max(x)))

enter image description here
You obviously cannot merge anything back to the first 200 dates unless you apply a looser definition of rolling maximum. (In another SO session involving "shifty" data I showed how to use embed to pad the "trailing" periods: Data transformation in R but I don't know if you want to construct matrices that are 200 times as large as your input data.)

肥爪爪 2024-12-10 19:17:34

我编辑了前面答案中的代码,使它们成为采用相同输入(单变量时间序列)并返回相同输出(自最后 n 天高点以来的天数向量)的函数:

daysSinceHigh1 <- function(x,n) {
    as.vector(n-rollapply(x, n, which.max))
}

daysSinceHigh2 <- function(x, n){
    apply(embed(x, n), 1, which.max)-1
}

第二个函数似乎是最快,但它们提供的结果略有不同:

> getSymbols("^GSPC",from='01-01-1900')
[1] "GSPC"
> system.time(x <- daysSinceHigh1(Cl(GSPC), 200))
   user  system elapsed 
   0.42    0.00    0.42 
> system.time(y <- daysSinceHigh2(Cl(GSPC), 200))
   user  system elapsed 
   0.24    0.00    0.24 
> all.equal(x,y)
[1] "Mean relative difference: 0.005025126"

经过仔细检查,第一个函数中似乎存在一些奇怪的边缘情况:

data <- c(1,2,3,4,5,6,7,7,6,5,6,7,8,5,4,3,2,1)
answer <- c(0,0,0,0,1,2,3,0,0,1,2,3,4,4)
x <- daysSinceHigh1(data, 5)
y <- daysSinceHigh2(data, 5)

> x
 [1] 0 0 0 1 2 3 4 4 0 1 2 3 4 4
> y
 [1] 0 0 0 0 1 2 3 0 0 1 2 3 4 4
> answer
 [1] 0 0 0 0 1 2 3 0 0 1 2 3 4 4
> all.equal(x,answer)
[1] "Mean relative difference: 0.5714286"
> all.equal(y,answer)
[1] TRUE

因此,第二个函数(基于 Andrie 的代码)似乎更好。

I edited the code from the previous answers such that they are functions that take the same inputs (a univariate time series) and return the same output (a vector of days since the last n-day high):

daysSinceHigh1 <- function(x,n) {
    as.vector(n-rollapply(x, n, which.max))
}

daysSinceHigh2 <- function(x, n){
    apply(embed(x, n), 1, which.max)-1
}

The second function seems to be the fastest, but they're providing slightly different results:

> getSymbols("^GSPC",from='01-01-1900')
[1] "GSPC"
> system.time(x <- daysSinceHigh1(Cl(GSPC), 200))
   user  system elapsed 
   0.42    0.00    0.42 
> system.time(y <- daysSinceHigh2(Cl(GSPC), 200))
   user  system elapsed 
   0.24    0.00    0.24 
> all.equal(x,y)
[1] "Mean relative difference: 0.005025126"

Upon closer inspection, it appears that there are some weird edge cases in the 1st function:

data <- c(1,2,3,4,5,6,7,7,6,5,6,7,8,5,4,3,2,1)
answer <- c(0,0,0,0,1,2,3,0,0,1,2,3,4,4)
x <- daysSinceHigh1(data, 5)
y <- daysSinceHigh2(data, 5)

> x
 [1] 0 0 0 1 2 3 4 4 0 1 2 3 4 4
> y
 [1] 0 0 0 0 1 2 3 0 0 1 2 3 4 4
> answer
 [1] 0 0 0 0 1 2 3 0 0 1 2 3 4 4
> all.equal(x,answer)
[1] "Mean relative difference: 0.5714286"
> all.equal(y,answer)
[1] TRUE

Therefore, it seems like the second function (based off Andrie's code) is better.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文