Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # visit http://reakkt.com for description
- setwd("C:/!REPO2/Test")
- library(parallel)
- library(doSNOW)
- registerDoSNOW(cl <- makeCluster(detectCores(),outfile="cluster.txt"))
- library(TTR)
- ###
- asset <- "spx"
- data.source <- "stooq"
- sd.treshold <- 6
- test.period <- 250 # 1 year
- minimum.return <- 0.5
- repetitions <- 10
- l <- 1000 # 4 years
- ###
- source(file="../../!REPO/Common/stooq all.R")
- # source(file="../../!REPO/Common/quantmod data.R")
- source(file="../../!REPO/Common/zigzag.R")
- ### ladowanie danych (z pliku stooq albo z Yahoo!)
- if (data.source=="stooq") {
- stooq.file <- "../../!REPO/Stooq/stooq data.zip"
- x <- as.matrix(GetStooqDataZip(asset)$data)
- }
- if (data.source=="yahoo") {
- x <- getMarketData(asset,data.source)
- }
- ###
- x <- tail(x,l+test.period)
- n <- nrow(x)
- p <- x[,"Close"]
- r <- log(x[-1,"Close"]/x[-n,"Close"])
- v.month <- runSD(r,25)
- v.year <- runSD(r,250)
- change.treshold <- 100*stats::sd(r)*sd.treshold # dla SD=6, srednia istotnosc sygnalu jest wieksza niz dla SD=5
- # change.treshold <- 35
- zz <- TTR::ZigZag(p,change=change.treshold)
- zz.rev <- ZigZagReversals(zz)
- ###
- # reps - no. of repetitions
- GenerateDesiredReturn <- function(idx, reps, minimum.return=0.5, test.period=250) {
- cat(idx,"\n") # outfile
- market.result <- 0
- strategy.result <- rep(-Inf,reps)
- j <- numeric(reps)
- period.returns <- r[(idx-test.period+1):idx]
- market.result <- sum(period.returns)
- i2 <- 1
- for (i2 in 1:reps) {
- while (strategy.result[i2]<market.result ||
- strategy.result[i2]<=0 ||
- strategy.result[i2]<minimum.return) {
- j[i2] <- j[i2]+1
- signal <- runif(test.period,0,1)
- strategy.result[i2] <- sum(ifelse(signal>0.5,+1,-1)*period.returns)
- } # while
- } # for
- return( c(market.result,
- sum(abs(period.returns)), # theoretical max. return in the period
- mean(strategy.result), # mean (<-reps) strategy result in the period
- mean(j)) ) # average no. of draws to reach tresholds (>=market, >0, >=minimum.return)
- }
- test.window <- test.period:(n-1)
- returns <- matrix(NA,length(test.window),4)
- range(test.window)
- returns <- foreach ( idx=test.window, .combine=rbind ) %dopar% GenerateDesiredReturn(idx, reps=repetitions, minimum.return, test.period)
- colnames(returns) <- c("market","max. possible", "strategy", "intensity")
- stopCluster(cl)
- par(mfrow=c(5,1))
- plot(tail(p[-1],l),type="l", main="Price",ylab="price")
- abline(v=zz.rev$min[which(zz.rev$min>=(n-l))]-(n-l), col="Green")
- abline(v=zz.rev$max[which(zz.rev$max>=(n-l))]-(n-l), col="Red")
- plot(tail(r,l),type="l", main="Returns",ylab="%")
- abline(v=zz.rev$min[which(zz.rev$min>=(n-l))]-(n-l), col="Green")
- abline(v=zz.rev$max[which(zz.rev$max>=(n-l))]-(n-l), col="Red")
- matplot(cbind(tail(v.year,l),tail(v.month,l)),type="l", lty="solid", log="y", main="Realized Volatility 1Y & 1M",ylab="volatility")
- abline(v=zz.rev$min[which(zz.rev$min>=(n-l))]-(n-l), col="Green")
- abline(v=zz.rev$max[which(zz.rev$max>=(n-l))]-(n-l), col="Red")
- matplot(cbind( tail(returns[,"max. possible"],l),
- tail(returns[,"strategy"],l) ),
- type="l", lty="solid",log="y",
- main="Theoretical Max Return vs Strategy Return 1Y", ylab="return")
- abline(v=zz.rev$min[which(zz.rev$min>=(n-l))]-(n-l), col="Green")
- abline(v=zz.rev$max[which(zz.rev$max>=(n-l))]-(n-l), col="Red")
- plot(tail(returns[,"intensity"],l),type="l",log="y", main="Difficulty 1Y",ylab="trials")
- abline(v=zz.rev$min[which(zz.rev$min>=(n-l))]-(n-l), col="Green")
- abline(v=zz.rev$max[which(zz.rev$max>=(n-l))]-(n-l), col="Red")
- par(mfrow=c(1,1))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement