Advertisement
mjaniec

Beating the random walk

Apr 3rd, 2012
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 0.84 KB | None | 0 0
  1. library(TTR)
  2.  
  3. quotes  <- getStooqData("sc.f",static_cookie=TRUE)
  4. prices  <- quotes[,"Close"]
  5. Nquotes <- nrow(quotes)
  6. changes <- log(prices[-1]/prices[-Nquotes])
  7.  
  8. rw <- changes
  9.  
  10. n <- 25
  11. trade <- rep(0,length(rw))
  12.  
  13. for(i in (n+1):(length(rw)-1)) {
  14.  
  15.     m  <- ifelse(i>n ,mean(rw[(i-n):i]), mean(rw[1:i]) )
  16.    
  17.   if ( ifelse(tail(cumsum(rw[(i-n):i]),1)>0, rw[i] < m, rw[i] > m) )
  18.  
  19.     trade[i] <- log(prices[i+2]/prices[i+1]) else
  20.     trade[i] <- log(prices[i+1]/prices[i+2])    
  21.  
  22. }
  23.  
  24. eq_curve <- cumsum(trade)
  25.  
  26. par(mfrow=c(2,1))
  27. plot(rw,type='l',main='daily log changes',ylab="%")
  28. matplot(cbind(eq_curve,cumsum(changes)),type="l",main="cumulative log return",ylab="%")
  29. legend("topleft",legend=c("strategy","asset"),text.col=c("Black","Red"))
  30. abline(h=0,col="Red",lty="dotted")
  31.  
  32. tail(eq_curve,1)
  33. prices[Nquotes]/prices[1]-1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement