3

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.

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)

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)?

thanks

edit:

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")))
E.D.
  • 319
  • 1
  • 5
  • 15
  • The asker of [this](http://stackoverflow.com/questions/13912282/time-series-subsetting-in-r-package-xts/13917847#13917847) question wanted to know how to subset particular times of the day. That's easy to do with `xts` notation like `x["T09:30/T11:00"]`. Is that relevant to your case? – SlowLearner Jan 09 '13 at 12:04
  • subset based on time is already taken care of. my question is how to subset based on date? – E.D. Jan 09 '13 at 12:06
  • Can you edit your question to include the output of `dput(head(my.min.A, 20))` or similar so that we can see what your data looks like. – SlowLearner Jan 09 '13 at 12:21
  • @E.D. my.index[i] == day(my.min.A)? what is i here? – agstudy Jan 09 '13 at 12:56
  • i in this case refers to a observation in my.index, as my.index contains all the trading days in the sample. – E.D. Jan 09 '13 at 13:24

1 Answers1

4

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]))
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
  • 1
    my.min.A (as well my.min.B) will contain the open /close/high/.. for the given period..Might suggest `apply.daily(merge(my.min.A, my.min.B), function(x) cor(x[,1],x[,5]))` – agstudy Jan 09 '13 at 13:12
  • Thanks guys, works a charm, I just added na.locf() for periods where one of the 2 shares did not trade. – E.D. Jan 10 '13 at 08:05