Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library(TTR)
- quotes <- getStooqData("sc.f",static_cookie=TRUE)
- prices <- quotes[,"Close"]
- Nquotes <- nrow(quotes)
- changes <- log(prices[-1]/prices[-Nquotes])
- rw <- changes
- n <- 25
- trade <- rep(0,length(rw))
- for(i in (n+1):(length(rw)-1)) {
- m <- ifelse(i>n ,mean(rw[(i-n):i]), mean(rw[1:i]) )
- if ( ifelse(tail(cumsum(rw[(i-n):i]),1)>0, rw[i] < m, rw[i] > m) )
- trade[i] <- log(prices[i+2]/prices[i+1]) else
- trade[i] <- log(prices[i+1]/prices[i+2])
- }
- eq_curve <- cumsum(trade)
- par(mfrow=c(2,1))
- plot(rw,type='l',main='daily log changes',ylab="%")
- matplot(cbind(eq_curve,cumsum(changes)),type="l",main="cumulative log return",ylab="%")
- legend("topleft",legend=c("strategy","asset"),text.col=c("Black","Red"))
- abline(h=0,col="Red",lty="dotted")
- tail(eq_curve,1)
- prices[Nquotes]/prices[1]-1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement