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(Ad(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-1]]=monthlyReturn(Ad(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-2]]=monthlyReturn(Ad(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-4]]=monthlyReturn(Ad(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-6]]=monthlyReturn(Ad(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-7]]=monthlyReturn(Ad(na.approx(STOCK))) } for(i in 1259: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(Ad(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-9]]=monthlyReturn(Ad(na.approx(STOCK))) } for(i in 1440: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-11]]=monthlyReturn(Ad(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:1488)) mm=tk_xts(m,silent=T) history=mm[,1489:2976] return=mm[,1:1488] nh=history-return x=merge.xts(history,nh,type="outer") xx=as.matrix(x) h=na.approx(x[,1:1488]) r=na.approx(x[,1489:2976]) hh=na.approx(xx[,1:1488]) rr=na.approx(xx[,1489:2976]) for(i in 1:nrow(hh)){ m=matrix(nrow=2,ncol=ncol(hh)) m[1,]=rr[i,] m[2,]=hh[i,] mm=t(m) mm=tk_tbl(mm) colnames(mm)=c("returns","historyr") assign(paste0("perform",i),arrange(mm,desc(historyr))) } avr=vector(length=323) num=vector(length=323) for(j in 1:323){ cr=0 t=get(paste0("perform",j)) t=as.matrix(t) numh=1488-sum(is.na(t[,2])) numr=1488-sum(is.na(t[,1])) for(i in 1:numh){ num[j]=floor(numh/10) if(t[i,2]>quantile(t[1:numh,2],0.9)){ cr=cr+t[i,1] } else{0} } avr[j]=cr/num[j] } avr=data.frame(avr) num=data.frame(num) ggplot(data = avr) + geom_line(mapping = aes(x=index(h),y = avr),color="blue") ggplot(data = num) + geom_line(mapping = aes(x=index(h),y = num),color="blue") avr=vector(length=323) num=vector(length=323) for(j in 1:323){ t=get(paste0("perform",j)) t=as.matrix(t) numh=1488-sum(is.na(t[,2])) numr=1488-sum(is.na(t[,1])) num[j]=floor(numh/10) cr=sum(t[1:num[j],1]) avr[j]=cr/num[j] }