{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Asset price example example 103 prepare data set

Info iconThis preview shows pages 12–15. Sign up to view the full content.

View Full Document Right Arrow Icon
#Asset Price Example #Example 10.3 #prepare data set library(quantmod) SS36=getSymbols("600036.SS", auto.assign=F) SS36=as.data.frame(SS36) names(SS36)[1]=paste("open") names(SS36)[2]=paste("high") names(SS36)[3]=paste("low") names(SS36)[4]=paste("close") names(SS36)[5]=paste("vol") SS36=cbind(SS36, date=as.Date(rownames(SS36)) ) SS36["code"]=600036 SS36=subset(SS36, date>="2012-10-01") r=rotate(SS36, c("high", "low", "close") ,4) coeff=4 sw=3 rr=NA rr=rollingSum(r, data.frame(r$date, lam=(coeff*abs(r$open-r$close1)/r$close1)/ sw ), sw) rr$lam=ifelse(rr$lam==0, 0.001, rr$lam) #(rr$open-rr$close1)/(rr$close1*rr$lam) x=data.frame(date=rr$date, x1=(rr$open-rr$close1)/(rr$close1*rr$lam)) rr=NA rr=rollingSum(r, data.frame(r$date, lam=(coeff*abs(r$close1- r$close2)/r$close1)/sw ),sw ) rr$lam=ifelse(rr$lam==0, 0.001, rr$lam) (rr$close1-rr$close2)/(rr$close1*rr$lam) x=cbind(x, x2=(rr$close1-rr$close2)/(rr$close1*rr$lam) ) rr=NA rr=rollingSum(r, data.frame(r$date, lam=(coeff*abs(r$high1- r$high2)/r$close1)/sw ), sw ) rr$lam=ifelse(rr$lam==0, 0.001, rr$lam) x=cbind(x, x3=(rr$high1-rr$high2)/(rr$close1*rr$lam) ) rr=NA rr=rollingSum(r, data.frame(r$date, lam=(coeff*abs(r$low1-r$low2)/r$close1)/sw ), sw ) rr$lam=ifelse(rr$lam==0, 0.001, rr$lam) x=cbind(x, x4=(rr$low1-rr$low2)/(rr$close1*rr$lam) ) rr=NA rr=rollingSum(r, data.frame(r$date, lam=(coeff*abs(r$close2- r$close3)/r$close2)/sw ),sw ) rr$lam=ifelse(rr$lam==0, 0.001, rr$lam) x=cbind(x, x5=(rr$close2-rr$close3)/(rr$close2*rr$lam) )
Background image of page 12

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
rr=NA rr=rollingSum(r, data.frame(r$date, lam=(coeff*abs(r$high2- r$high3)/r$close2)/sw ), sw ) rr$lam=ifelse(rr$lam==0, 0.001, rr$lam) x=cbind(x, x6=(rr$high2-rr$high3)/(rr$close2*rr$lam) ) rr=NA rr=rollingSum(r, data.frame(r$date, lam=(coeff*abs(r$low2-r$low3)/r$close2)/sw ), sw ) rr$lam=ifelse(rr$lam==0, 0.001, rr$lam) x=cbind(x, x7=(rr$low2-rr$low3)/(rr$close2*rr$lam) ) rr=NA rr=rollingSum(r, data.frame(r$date, lam=(coeff*abs(r$close3- r$close4)/r$close3)/sw ),sw ) rr$lam=ifelse(rr$lam==0, 0.001, rr$lam) x=cbind(x, x8=(rr$close3-rr$close4)/(rr$close3*rr$lam) ) rr=NA rr=rollingSum(r, data.frame(r$date, lam=(coeff*abs(r$high3- r$high4)/r$close3)/sw ), sw ) rr$lam=ifelse(rr$lam==0, 0.001, rr$lam) x=cbind(x, x9=(rr$high3-rr$high4)/(rr$close3*rr$lam) ) rr=NA rr=rollingSum(r, data.frame(r$date, lam=(coeff*abs(r$low3-r$low4)/r$close3)/sw ), sw ) rr$lam=ifelse(rr$lam==0, 0.001, rr$lam) x=cbind(x, x10=(rr$low3-rr$low4)/(rr$close3*rr$lam) ) rr=NA rr=rollingSum(r, data.frame(r$date, lam=(coeff*abs(r$high- r$close)/r$close1)/sw ),sw ) rr$lam=ifelse(rr$lam==0, 0.001, rr$lam) x=cbind(x, d1=(rr$high-rr$close)/(rr$close1*rr$lam) ) rr=NA rr=rollingSum(r, data.frame(r$date, lam=(coeff*abs(r$close- r$close1)/r$close1)/sw ), sw ) rr$lam=ifelse(rr$lam==0, 0.001, rr$lam) x=cbind(x, d2=(rr$close-rr$close1)/(rr$close1*rr$lam) ) rr=NA rr=rollingSum(r, data.frame(r$date, lam=(coeff*abs(r$close-r$low)/r$close1)/sw ), sw ) rr$lam=ifelse(rr$lam==0, 0.001, rr$lam) x=cbind(x, d3=(rr$close-rr$low)/(rr$close1*rr$lam) ) require ("neuralnet") net.out <- neuralnet(d1+d2+d3~x1+x2+x3+x4+x5+x6+x7+x8+x9+x10, data=x, hidden=18, threshold=0.001, stepmax=1e+6) rollingSum=function(x, y, sw) { # c(date, colman=forumula) y=data.frame(y) y=y[order( y[,1], decreasing=T),] y_colnames= colnames(y[2]) for (i in 1:(length(y[,1])-(sw-1)))
Background image of page 13
{ tmpSum=sum(y[i:(i+sw-1), 2]) tmpDate= y[i,1] singleRow=data.frame(tmpDate, tmpSum ) if (i==1) retV=rbind(singleRow) else retV=rbind(retV,singleRow) }
Background image of page 14

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 15
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}