如何使用 xts 包计算日内数据的每日平均相关性? [英] How to compute the daily avg correlation on intraday data using the xts package?
问题描述
我有一堆股票的日内历史记录.我正在尝试每天计算股票之间的 1 分钟相关性.我的目标是使用一段时间内每对的日均值来确定特定交易策略的最佳对.
I have intra-day history for a bunch of stocks. I am trying to compute the 1-minute correlation between stocks on a daily basis. My aim is to use the daily average per pair over a period to identify optimal pairs for a specific trading strategy.
我的想法是遍历交易日,计算日内 1 分钟相关性,计算所有交易日的平均值,下一对.
My idea is to loop through the trading days, compute intra-day 1-minute correlation, compute avg over all trading days, next pair.
但是,我在交易日循环中陷入困境.
However, I am getting stuck at looping through the trading days.
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)
?
I get the trading days in my.index
, could someone please give me some guidance as to how to select a subset of my.min.A
where 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")))
推荐答案
这是一个可重复的示例,使用每日数据并计算价格之间的每月相关性.
Here's a reproducible example, using daily data and calculating monthly correlations between prices.
library(quantmod)
getSymbols("KO;PEP")
apply.monthly(merge(Cl(KO),Cl(PEP)), function(x) cor(x[,1],x[,2]))
在你的情况下,你会想要这样的:
In your case, you would want something like:
apply.daily(merge(Cl(my.min.A), Cl(my.min.B)), function(x) cor(x[,1],x[,2]))
这篇关于如何使用 xts 包计算日内数据的每日平均相关性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!