library(tidyquant)
library(tidyverse)
library(timetk)
dd=tk_tbl(d) regr_fun <- function(data) { coef(lm(data[,1] ~ data[,2], data = dd)) } m=dd %>% tq_mutate(mutate_fun = rollapply, width = 4, FUN = regr_fun, by.column = FALSE, col_rename = c("coef.0", "coef.1"))
for(i in 1:length(weekprices)){ a=weekprices[[i]] p=merge(p,a,all=F) } p=monthlyReturn(weekprices[[1]]) for(i in 2:length(weekprices)){ a=monthlyReturn(weekprices[[i]]) p=merge(p,a,all=F) } pp=tk_tbl(p) ppp=tk_xts(pp,silent=T) m=pp %>% tq_mutate(mutate_fun = rollapply, width = 11, FUN = sum, by.column = TRUE, col_rename = paste0("history",1:497)) mm=tk_xts(m,silent=T) history=mm[,498:994] return=mm[,1:497] x=merge.xts(history,return,type="inner") h=na.approx(x[,1:497]) r=na.approx(x[,498:994]) cr=vector() for(i in 1:nrow(h)){ a=0 for(j in 1:ncol(h)){ if(h[i,j]>quantile(h[i,],0.9)){ a=a+r[i+1,j] } cr[i]=a/ncol(h) } }