Advertisement
mjaniec

random strategy test

May 8th, 2013
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 3.80 KB | None | 0 0
  1. # visit http://reakkt.com for description
  2.  
  3. setwd("C:/!REPO2/Test")
  4.  
  5. library(parallel)
  6. library(doSNOW)
  7. registerDoSNOW(cl <- makeCluster(detectCores(),outfile="cluster.txt"))
  8.  
  9. library(TTR)
  10.  
  11. ###
  12.  
  13. asset <- "spx"
  14.  
  15. data.source <- "stooq"
  16.  
  17. sd.treshold     <- 6
  18.  
  19. test.period     <- 250 # 1 year
  20.  
  21. minimum.return  <- 0.5
  22.  
  23. repetitions     <- 10
  24.  
  25. l               <- 1000 # 4 years
  26.  
  27. ###
  28.  
  29. source(file="../../!REPO/Common/stooq all.R")
  30. # source(file="../../!REPO/Common/quantmod data.R")
  31. source(file="../../!REPO/Common/zigzag.R")
  32.  
  33. ### ladowanie danych (z pliku stooq albo z Yahoo!)
  34.  
  35. if (data.source=="stooq") {
  36.  
  37.   stooq.file   <- "../../!REPO/Stooq/stooq data.zip"
  38.  
  39.   x <- as.matrix(GetStooqDataZip(asset)$data)
  40.  
  41. }
  42.  
  43. if (data.source=="yahoo") {
  44.  
  45.   x <- getMarketData(asset,data.source)
  46.  
  47. }
  48.  
  49. ###
  50.  
  51. x <- tail(x,l+test.period)
  52.  
  53. n <- nrow(x)
  54.  
  55. p <- x[,"Close"]
  56.  
  57. r <- log(x[-1,"Close"]/x[-n,"Close"])
  58.  
  59. v.month <- runSD(r,25)
  60. v.year  <- runSD(r,250)
  61.  
  62. change.treshold <- 100*stats::sd(r)*sd.treshold # dla SD=6, srednia istotnosc sygnalu jest wieksza niz dla SD=5
  63.  
  64. # change.treshold <- 35
  65.  
  66. zz  <- TTR::ZigZag(p,change=change.treshold)
  67.  
  68. zz.rev <- ZigZagReversals(zz)
  69.  
  70. ###
  71.  
  72. # reps - no. of repetitions
  73. GenerateDesiredReturn <- function(idx, reps, minimum.return=0.5, test.period=250) {
  74.  
  75.   cat(idx,"\n") # outfile
  76.  
  77.   market.result   <- 0
  78.   strategy.result <- rep(-Inf,reps)
  79.   j               <- numeric(reps)
  80.  
  81.   period.returns <- r[(idx-test.period+1):idx]
  82.  
  83.   market.result  <- sum(period.returns)
  84.  
  85.   i2 <- 1
  86.  
  87.   for (i2 in 1:reps) {
  88.  
  89.     while (strategy.result[i2]<market.result ||
  90.            strategy.result[i2]<=0 ||
  91.            strategy.result[i2]<minimum.return) {
  92.      
  93.       j[i2] <- j[i2]+1
  94.      
  95.       signal <- runif(test.period,0,1)
  96.      
  97.       strategy.result[i2] <- sum(ifelse(signal>0.5,+1,-1)*period.returns)
  98.        
  99.     } # while
  100.    
  101.   } # for
  102.  
  103.   return( c(market.result,            
  104.             sum(abs(period.returns)), # theoretical max. return in the period
  105.             mean(strategy.result),    # mean (<-reps) strategy result in the period
  106.             mean(j)) )                # average no. of draws to reach tresholds (>=market, >0, >=minimum.return)
  107.  
  108. }
  109.  
  110. test.window <- test.period:(n-1)
  111.  
  112. returns <- matrix(NA,length(test.window),4)
  113.  
  114. range(test.window)
  115.  
  116. returns <- foreach ( idx=test.window, .combine=rbind ) %dopar% GenerateDesiredReturn(idx, reps=repetitions, minimum.return, test.period)
  117.  
  118. colnames(returns) <- c("market","max. possible", "strategy", "intensity")
  119.  
  120. stopCluster(cl)
  121.  
  122. par(mfrow=c(5,1))
  123.  
  124. plot(tail(p[-1],l),type="l", main="Price",ylab="price")
  125. abline(v=zz.rev$min[which(zz.rev$min>=(n-l))]-(n-l), col="Green")
  126. abline(v=zz.rev$max[which(zz.rev$max>=(n-l))]-(n-l), col="Red")
  127.  
  128. plot(tail(r,l),type="l", main="Returns",ylab="%")
  129. abline(v=zz.rev$min[which(zz.rev$min>=(n-l))]-(n-l), col="Green")
  130. abline(v=zz.rev$max[which(zz.rev$max>=(n-l))]-(n-l), col="Red")
  131.  
  132. matplot(cbind(tail(v.year,l),tail(v.month,l)),type="l", lty="solid", log="y", main="Realized Volatility 1Y & 1M",ylab="volatility")
  133. abline(v=zz.rev$min[which(zz.rev$min>=(n-l))]-(n-l), col="Green")
  134. abline(v=zz.rev$max[which(zz.rev$max>=(n-l))]-(n-l), col="Red")
  135.  
  136. matplot(cbind( tail(returns[,"max. possible"],l),
  137.                tail(returns[,"strategy"],l) ),
  138.         type="l", lty="solid",log="y",
  139.         main="Theoretical Max Return vs Strategy Return 1Y", ylab="return")
  140. abline(v=zz.rev$min[which(zz.rev$min>=(n-l))]-(n-l), col="Green")
  141. abline(v=zz.rev$max[which(zz.rev$max>=(n-l))]-(n-l), col="Red")
  142.  
  143. plot(tail(returns[,"intensity"],l),type="l",log="y", main="Difficulty 1Y",ylab="trials")
  144. abline(v=zz.rev$min[which(zz.rev$min>=(n-l))]-(n-l), col="Green")
  145. abline(v=zz.rev$max[which(zz.rev$max>=(n-l))]-(n-l), col="Red")
  146.  
  147. par(mfrow=c(1,1))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement