Using data(FANG), say I know that there is a smoothed relationship between volume and opening price. Also I know the length of the most predictive rolling mean varies by stock. For some it is short, day 2 days. For others 10. I’d like to create multiple rolling means of lengths between 2 and 10 days for each stock.
So far I tried the tibbletime package and got a start so that I can calculate the multiple rolling means for one.
FB <- FANG %>% filter(symbol == “FB”)
meanstep <- seq(2, 10, 1)
col_names <- map_chr(meanstep, ~paste0("rollmean_", .x))
rollers <- map(meanstep, ~rollify(mean, window = .x)) %>% set_names(nm = col_names)
FB_multiroll<- bind_cols(FB, invoke_map(rollers, x = FB$volume))
However, I can’t seem to figure out how to make this work when grouping by multiple stocks.
I tried adding:
FANG_with_multiroll<- FANG %>%
group_by(symbol) %>%
bind_cols(FANG, invoke_map(rollers, x =FANG$volume)
But that didn’t work. Any ideas would be appreciated. One I get it to work, I plan on finding the highest correlation or rsquared for each symbol. If you have ideas about better ways to do that too, I’m interested.