【问题标题】:How to compute the daily avg correlation on intraday data using the xts package?如何使用 xts 包计算日内数据的每日平均相关性?
【发布时间】:2013-01-09 11:53:56
【问题描述】:

我有一堆股票的日内历史记录。我正在尝试每天计算股票之间的 1 分钟相关性。我的目标是使用一段时间内每对的每日平均值来确定特定交易策略的最佳货币对。

我的想法是遍历交易日,计算日内 1 分钟相关性,计算所有交易日的平均值,下一个交易对。

但是,我在交易日的循环中陷入困境。

my.xts.A <- xts(A_Frame[,-1], order.by=A_Frame[,1])
my.xts.B <- xts(B_Frame[,-1], order.by=B_Frame[,1])

my.min.A <- to.minutes(my.xts.A[,1],1,'minutes')
my.min.B <- to.minutes(my.xts.B[,1],1,'minutes')

my.day <- to.daily(my.xts.A[,1],1)

my.index <- index(my.day)

我在my.index 中获得交易日,有人可以指导我如何选择my.min.A 的子集my.index[i] == day(my.min.A) 吗?

谢谢

编辑:

dput(head(my.min.A, 20))
structure(c(3575, 3630, 3649, 3630, 3614, 3612, 3612, 3616, 3615, 
3602, 3602, 3602, 3605, 3605, 3605, 3605, 3605, 3604, 3604, 3605, 
3682, 3630, 3649, 3630, 3614, 3612, 3612, 3616, 3615, 3602, 3602, 
3606, 3605, 3605, 3605, 3605, 3605, 3605, 3604, 3608, 3575, 3630, 
3649, 3630, 3612, 3612, 3610, 3616, 3615, 3602, 3602, 3601, 3604, 
3603, 3604, 3604, 3604, 3604, 3604, 3604, 3682, 3630, 3649, 3630, 
3612, 3612, 3610, 3616, 3615, 3602, 3602, 3604, 3604, 3604, 3604, 
3604, 3605, 3604, 3604, 3605), tclass = c("POSIXct", "POSIXt"
), tzone = "", class = c("xts", "zoo"), .indexCLASS = c("POSIXct", 
"POSIXt"), .indexTZ = "", index = structure(c(1352790059, 1352790290, 
1352790306, 1352790467, 1352790521, 1352790547, 1352790757, 1352791124, 
1352791222, 1352791466, 1352791576, 1352791750, 1352791859, 1352791891, 
1352791970, 1352792006, 1352792041, 1352792149, 1352792181, 1352792227
), tzone = "", tclass = c("POSIXct", "POSIXt")), .Dim = c(20L, 
4L), .Dimnames = list(NULL, c("minutes.Open", "minutes.High", 
"minutes.Low", "minutes.Close")))

【问题讨论】:

  • this 问题的提问者想知道如何对一天中的特定时间进行子集化。这很容易使用xts 符号,如x["T09:30/T11:00"]。这与您的情况有关吗?
  • 基于时间的子集已经处理好了。我的问题是如何根据日期进行子集化?
  • 您能否编辑您的问题以包含dput(head(my.min.A, 20)) 或类似的输出,以便我们可以看到您的数据是什么样的。
  • @E.D. my.index[i] == day(my.min.A)?我在这里是什么?
  • i 在这种情况下是指 my.index 中的观察,因为 my.index 包含样本中的所有交易日。

标签: r xts


【解决方案1】:

这是一个可重复的示例,使用每日数据并计算价格之间的每月相关性。

library(quantmod)
getSymbols("KO;PEP")
apply.monthly(merge(Cl(KO),Cl(PEP)), function(x) cor(x[,1],x[,2]))

在你的情况下,你会想要这样的东西:

apply.daily(merge(Cl(my.min.A), Cl(my.min.B)), function(x) cor(x[,1],x[,2]))

【讨论】:

  • my.min.A(以及 my.min.B)将包含给定期间的打开 /close/high/.. 可能建议apply.daily(merge(my.min.A, my.min.B), function(x) cor(x[,1],x[,5]))
  • 谢谢大家,很有魅力,我刚刚添加了 na.locf() 用于 2 股之一没有交易的时期。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-01
  • 2021-12-18
  • 2019-03-07
  • 2021-02-05
  • 1970-01-01
  • 1970-01-01
  • 2021-05-09
相关资源
最近更新 更多