library(stringr) szcode=read.table("clipboard",header=T)#上证代码 sccode=read.table("clipboard",header=T)#深证代码 zxcode=read.table("clipboard",header=T)#中小代码 code=paste0(szcode[,1],".ss") Z=paste0("A",1:length(code)) price=list() for(i in 1:length(code)){ name=code[i] from=szcode[i,3] setSymbolLookup(STOCK=list(name=name,src='yahoo')) getSymbols("STOCK",from=from,to="2020-01-01") price[[i]]=monthlyReturn(Cl(na.approx(STOCK))) } for(i in 57:length(code)){ name=code[i] from=szcode[i,3] setSymbolLookup(STOCK=list(name=name,src='yahoo')) getSymbols("STOCK",from=from,to="2020-01-01") price[[i-1]]=monthlyReturn(Cl(na.approx(STOCK))) } for(i in 499:length(code)){ name=code[i] from=szcode[i,3] setSymbolLookup(STOCK=list(name=name,src='yahoo')) getSymbols("STOCK",from=from,to="2020-01-01") price[[i-1]]=monthlyReturn(Cl(na.approx(STOCK))) } for(i in 500:length(code)){ name=code[i] from=szcode[i,3] setSymbolLookup(STOCK=list(name=name,src='yahoo')) getSymbols("STOCK",from=from,to="2020-01-01") price[[i-2]]=monthlyReturn(Cl(na.approx(STOCK))) } for(i in 500:length(code)){ name=code[i] from=szcode[i,3] setSymbolLookup(STOCK=list(name=name,src='yahoo')) getSymbols("STOCK",from=from,to="2020-01-01") price[[i-2]]=monthlyReturn(Cl(na.approx(STOCK))) } for(i in 807:length(code)){ name=code[i] from=szcode[i,3] setSymbolLookup(STOCK=list(name=name,src='yahoo')) getSymbols("STOCK",from=from,to="2020-01-01") price[[i-3]]=monthlyReturn(Cl(na.approx(STOCK))) } for(i in 957:length(code)){ name=code[i] from=szcode[i,3] setSymbolLookup(STOCK=list(name=name,src='yahoo')) getSymbols("STOCK",from=from,to="2020-01-01") price[[i-2]]=monthlyReturn(Cl(na.approx(STOCK))) } for(i in 1123:length(code)){ name=code[i] from=szcode[i,3] setSymbolLookup(STOCK=list(name=name,src='yahoo')) getSymbols("STOCK",from=from,to="2020-01-01") price[[i-3]]=monthlyReturn(Cl(na.approx(STOCK))) } for(i in 1162:length(code)){ name=code[i] from=szcode[i,3] setSymbolLookup(STOCK=list(name=name,src='yahoo')) getSymbols("STOCK",from=from,to="2020-01-01") price[[i-2]]=monthlyReturn(Cl(na.approx(STOCK))) } library(stringr) szcode=read.table("clipboard",header=T)#上证代码 sccode=read.table("clipboard",header=T)#深证代码 zxcode=read.table("clipboard",header=T)#中小代码 code=paste0(szcode[,1],".ss") Z=paste0("A",1:length(code)) price=list() for(i in 1:length(code)){ name=code[i] from=szcode[i,3] setSymbolLookup(STOCK=list(name=name,src='yahoo')) getSymbols("STOCK",from=from,to="2020-01-01") price[[i]]=monthlyReturn(Cl(na.approx(STOCK))) } for(i in 57:length(code)){ name=code[i] from=szcode[i,3] setSymbolLookup(STOCK=list(name=name,src='yahoo')) getSymbols("STOCK",from=from,to="2020-01-01") price[[i-1]]=monthlyReturn(Cl(na.approx(STOCK))) } for(i in 499:length(code)){ name=code[i] from=szcode[i,3] setSymbolLookup(STOCK=list(name=name,src='yahoo')) getSymbols("STOCK",from=from,to="2020-01-01") price[[i-1]]=monthlyReturn(Cl(na.approx(STOCK))) } for(i in 500:length(code)){ name=code[i] from=szcode[i,3] setSymbolLookup(STOCK=list(name=name,src='yahoo')) getSymbols("STOCK",from=from,to="2020-01-01") price[[i-2]]=monthlyReturn(Cl(na.approx(STOCK))) } for(i in 500:length(code)){ name=code[i] from=szcode[i,3] setSymbolLookup(STOCK=list(name=name,src='yahoo')) getSymbols("STOCK",from=from,to="2020-01-01") price[[i-2]]=monthlyReturn(Cl(na.approx(STOCK))) } for(i in 807:length(code)){ name=code[i] from=szcode[i,3] setSymbolLookup(STOCK=list(name=name,src='yahoo')) getSymbols("STOCK",from=from,to="2020-01-01") price[[i-3]]=monthlyReturn(Cl(na.approx(STOCK))) } for(i in 957:length(code)){ name=code[i] from=szcode[i,3] setSymbolLookup(STOCK=list(name=name,src='yahoo')) getSymbols("STOCK",from=from,to="2020-01-01") price[[i-2]]=monthlyReturn(Cl(na.approx(STOCK))) } for(i in 1123:length(code)){ name=code[i] from=szcode[i,3] setSymbolLookup(STOCK=list(name=name,src='yahoo')) getSymbols("STOCK",from=from,to="2020-01-01") price[[i-3]]=monthlyReturn(Cl(na.approx(STOCK))) } for(i in 1370:length(code)){ name=code[i] from=szcode[i,3] setSymbolLookup(STOCK=list(name=name,src='yahoo')) getSymbols("STOCK",from=from,to="2020-01-01") price[[i-8]]=monthlyReturn(Cl(na.approx(STOCK))) } p=price[[1]] for(i in 2:length(price)){ a=price[[i]] p=merge.xts(p,a,join="outer") } pp=tk_tbl(p) ppp=tk_xts(pp,silent=T) m=pp %>% tq_mutate(mutate_fun = rollapply, width = 10, FUN = sum, by.column = TRUE, col_rename = paste0("history",1:1430)) mm=tk_xts(m,silent=T) history=mm[,1431:2860] return=mm[,1:1430] x=merge.xts(history,return,type="outer") h=na.approx(x[,1:1430]) r=na.approx(x[,1431:2860]) 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,na.rm=T)){ a=a+r[i+1,j] } cr[i]=a/ncol(h) } }