# Asset price example example 103 prepare data set

• Notes
• 15

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

#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) )

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

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)))
{ 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) }

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

This is the end of the preview. Sign up to access the rest of the document.
• Fall '12
• Jababo

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern