Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- R version 3.3.2 (2016-10-31) -- "Sincere Pumpkin Patch"
- Copyright (C) 2016 The R Foundation for Statistical Computing
- Platform: x86_64-w64-mingw32/x64 (64-bit)
- R is free software and comes with ABSOLUTELY NO WARRANTY.
- You are welcome to redistribute it under certain conditions.
- Type 'license()' or 'licence()' for distribution details.
- Natural language support but running in an English locale
- R is a collaborative project with many contributors.
- Type 'contributors()' for more information and
- 'citation()' on how to cite R or R packages in publications.
- Type 'demo()' for some demos, 'help()' for on-line help, or
- 'help.start()' for an HTML browser interface to help.
- Type 'q()' to quit R.
- > install.packages("devtools")
- Installing package into ‘C:/Users/user/Documents/R/win-library/3.3’
- (as ‘lib’ is unspecified)
- --- Please select a CRAN mirror for use in this session ---
- trying URL 'https://cloud.r-project.org/bin/windows/contrib/3.3/devtools_1.12.0.zip'
- Content type 'application/zip' length 432238 bytes (422 KB)
- downloaded 422 KB
- package ‘devtools’ successfully unpacked and MD5 sums checked
- The downloaded binary packages are in
- C:\Users\user\AppData\Local\Temp\RtmpayQjeU\downloaded_packages
- > library("devtools")
- > install_github("RNeat","ahunteruk") #Install from github as not yet on CRAN
- Downloading GitHub repo ahunteruk/RNeat@master
- from URL https://api.github.com/repos/ahunteruk/RNeat/zipball/master
- Installing RNeat
- "C:/PROGRA~1/R/R-33~1.2/bin/x64/R" --no-site-file --no-environ --no-save --no-restore --quiet CMD INSTALL \
- "C:/Users/user/AppData/Local/Temp/RtmpayQjeU/devtools25f0467a70b0/ahunteruk-RNeat-f7c7eec" \
- --library="C:/Users/user/Documents/R/win-library/3.3" --install-tests
- * installing *source* package 'RNeat' ...
- ** R
- ** preparing package for lazy loading
- Error in loadNamespace(j <- i[[1L]], c(lib.loc, .libPaths()), versionCheck = vI[[j]]) :
- there is no package called 'magrittr'
- ERROR: lazy loading failed for package 'RNeat'
- * removing 'C:/Users/user/Documents/R/win-library/3.3/RNeat'
- Error: Command failed (1)
- In addition: Warning message:
- Username parameter is deprecated. Please use ahunteruk/RNeat
- > library("RNeat")
- Error in library("RNeat") : there is no package called ‘RNeat’
- > library("quantmod")
- Loading required package: xts
- Loading required package: zoo
- Attaching package: ‘zoo’
- The following objects are masked from ‘package:base’:
- as.Date, as.Date.numeric
- Loading required package: TTR
- Version 0.4-0 included new data defaults. See ?getSymbols.
- >
- > marketSymbol <- "^GSPC"
- > econmicDataSymbols <- c("UNRATE","PAYEMS","GDP")
- >
- > mktData <- new.env() #Make a new environment for quantmod to store data in
- > economicData <- new.env() #Make a new environment for quantmod to store data in
- >
- > #Specify dates for downloading data, training models and running simulation
- > dataDownloadStartDate <- as.Date("2000-06-01")
- >
- > trainingStartDate = as.Date("2001-01-01") #Specify the date to start training (yyyy-mm-dd)
- > trainingEndDate = as.Date("2006-12-31") #Specify the date to end training
- >
- > outOfSampleStartDate = as.Date("2007-01-01")
- > outOfSampleEndDate = as.Date("2016-07-15")
- >
- > #Download Data
- > getSymbols(marketSymbol,env=mktData,from=dataDownloadStartDate) #S&P 500
- As of 0.4-0, ‘getSymbols’ uses env=parent.frame() and
- auto.assign=TRUE by default.
- This behavior will be phased out in 0.5-0 when the call will
- default to use auto.assign=FALSE. getOption("getSymbols.env") and
- getOptions("getSymbols.auto.assign") are now checked for alternate defaults
- This message is shown once per session and may be disabled by setting
- options("getSymbols.warning4.0"=FALSE). See ?getSymbols for more details.
- [1] "GSPC"
- > getSymbols(econmicDataSymbols,src="FRED",env=economicData,from=dataDownloadStartDate) #Payems is non-farms payrolls
- [1] "UNRATE" "PAYEMS" "GDP"
- >
- > nEconomicDataPercentileLookbackShort <- 20
- > nEconomicDataPercentileLookbackMedium <- 50
- > nEconomicDataPercentileLookbackLong <- 100
- >
- > rollingPercentile <- function(data,n){
- + percentile <- function(dataBlock){
- + last(rank(dataBlock)/length(dataBlock))
- + }
- + return (as.zoo(rollapply(as.zoo(data),width=n,percentile,align="right",by.column=TRUE)))
- + }
- >
- > stockCleanNameFunc <- function(name){
- + return(sub("^","",name,fixed=TRUE))
- + }
- >
- > clClRet <- as.zoo((lag(Cl(get(stockCleanNameFunc(marketSymbol),mktData)),-1)/Cl(get(stockCleanNameFunc(marketSymbol),mktData))-1))
- >
- > payemsShortPercentile <- rollingPercentile(economicData$PAYEMS,nEconomicDataPercentileLookbackShort)
- > payemsMediumPercentile <- rollingPercentile(economicData$PAYEMS,nEconomicDataPercentileLookbackMedium)
- > payemsLongPercentile <- rollingPercentile(economicData$PAYEMS,nEconomicDataPercentileLookbackLong)
- >
- > unrateShortPercentile <- rollingPercentile(economicData$UNRATE,nEconomicDataPercentileLookbackShort)
- > unrateMediumPercentile <- rollingPercentile(economicData$UNRATE,nEconomicDataPercentileLookbackMedium)
- > unrateLongPercentile <- rollingPercentile(economicData$UNRATE,nEconomicDataPercentileLookbackLong)
- >
- > gdpShortPercentile <- rollingPercentile(economicData$GDP,nEconomicDataPercentileLookbackShort)
- > gdpMediumPercentile <- rollingPercentile(economicData$GDP,nEconomicDataPercentileLookbackMedium)
- > gdpLongPercentile <- rollingPercentile(economicData$GDP,nEconomicDataPercentileLookbackLong)
- >
- > #join the data sets, fill in any missing dates with the previous none NA value
- > mergedData <- na.locf(merge(economicData$PAYEMS,merge(Cl(get(stockCleanNameFunc(marketSymbol),mktData)),
- + economicData$PAYEMS,payemsShortPercentile,payemsMediumPercentile,payemsLongPercentile, economicData$UNRATE,unrateShortPercentile,unrateMediumPercentile,unrateLongPercentile,
- + economicData$GDP,gdpShortPercentile,gdpMediumPercentile,gdpLongPercentile
- + ,all.x=T),all=T))
- > mergedData <- mergedData[,-1]
- > ClClRet <- as.zoo(lag(mergedData[,1],-1)/mergedData[,1]-1)
- > ClTZero <- as.zoo(mergedData[,1])
- > ClTOne <- as.zoo(lag(mergedData[,1],-1))
- > mergedData <- merge(ClClRet,ClTOne,ClTZero,mergedData)
- > mergedData <- window(mergedData,start=dataDownloadStartDate)
- >
- > colnames(mergedData) <- c("ClClRet","ClTOne","ClTZero","Price","Payems","Payems.short","Payems.medium","Payems.long",
- + "Unrate","Unrate.short","Unrate.medium","Unrate.long",
- + "Gdp","Gdp.short","Gdp.medium","Gdp.long","all.x")
- >
- >
- > dev.new()
- > par(mfrow=c(4,2))
- > plot(mergedData[,"Price"], main="S&P Close Price",ylab="Close Price")
- > plot(mergedData[,"ClClRet"], main="S&P Close Price",ylab="Close Price")
- >
- > plot(mergedData[,"Payems"], main="Non-Farm Payrolls",ylab="Thousands of Persons")
- > plot(mergedData[,"Payems.short"], main="Non-Farm Payrolls Rolling Percentile",ylab="Percentile")
- > lines(mergedData[,"Payems.medium"], col="red")
- > lines(mergedData[,"Payems.long"], col="blue")
- > legend(x='bottomright', c(paste(nEconomicDataPercentileLookbackShort,"Points"),
- + paste(nEconomicDataPercentileLookbackMedium,"Points"),
- + paste(nEconomicDataPercentileLookbackLong,"Points")), fill=c("black","red","blue"), bty='n')
- >
- > plot(mergedData[,"Unrate"], main="Unemployment Rate",ylab="Percent")
- > plot(mergedData[,"Unrate.short"], main="Unemployment Rate Rolling Percentile",ylab="Percentile")
- > lines(mergedData[,"Unrate.medium"], col="red")
- > lines(mergedData[,"Unrate.long"], col="blue")
- > legend(x='bottomright', c(paste(nEconomicDataPercentileLookbackShort,"Points"),
- + paste(nEconomicDataPercentileLookbackMedium,"Points"),
- + paste(nEconomicDataPercentileLookbackLong,"Points")), fill=c("black","red","blue"), bty='n')
- > plot(mergedData[,"Gdp"], main="GDP",ylab="Billions of USD")
- > plot(mergedData[,"Gdp.short"], main="GBP Rolling Percentile",ylab="Percentile")
- > lines(mergedData[,"Gdp.medium"], col="red")
- > lines(mergedData[,"Gdp.long"], col="blue")
- > legend(x='bottomright', c(paste(nEconomicDataPercentileLookbackShort,"Points"),
- + paste(nEconomicDataPercentileLookbackMedium,"Points"),
- + paste(nEconomicDataPercentileLookbackLong,"Points")), fill=c("black","red","blue"), bty='n')
- >
- > featuresTrainingData <- window(mergedData,start=trainingStartDate,end=trainingEndDate)
- > featuresOutOfSampleData <- window(mergedData,start=outOfSampleStartDate,end=outOfSampleEndDate)
- >
- >
- > #Genetic algo setup
- > simulationData <- featuresTrainingData
- >
- > trading.InitialState <- function(){
- + state <- list()
- + state[1] <- 100 #Equity
- + state[2] <- 0 #% of Equity allocated to share (-ve for shorts)
- + state[3] <- state[1] #Maximum equity achieved
- + state[4] <- 1 #Trading day number
- + state[5] <- simulationData[1,"Price"]
- + state[6] <- simulationData[1,"Payems.short"]
- + state[7] <- simulationData[1,"Payems.medium"]
- + state[8] <- simulationData[1,"Payems.long"]
- + state[9] <- simulationData[1,"Unrate.short"]
- + state[10] <- simulationData[1,"Unrate.medium"]
- + state[11] <- simulationData[1,"Unrate.long"]
- + state[12] <- simulationData[1,"Gdp.short"]
- + state[13] <- simulationData[1,"Gdp.medium"]
- + state[14] <- simulationData[1,"Gdp.long"]
- + return(state)
- + }
- >
- > trading.ConvertStateToNeuralNetInputs <- function(currentState){
- + return (currentState)
- + }
- >
- > trading.UpdateState <- function(currentState,neuralNetOutputs){
- + #print(currentState)
- + equity <- currentState[[1]]
- + equityAllocation <- neuralNetOutputs[[1]]
- + maxEquityAchieved <- currentState[[3]]
- + tradingDay <- currentState[[4]]
- +
- + pctChange <- as.double((simulationData[tradingDay+1,"Price"]))/as.double((simulationData[tradingDay,"Price"]))-1
- + #print(paste("pctChange",pctChange))
- + #print(paste("equityAllocation",equityAllocation))
- +
- + pnl <- equity * equityAllocation * pctChange
- +
- + equity <- equity + pnl
- + maxEquityAchieved <- max(maxEquityAchieved,equity)
- +
- + tradingDay <- tradingDay + 1
- + currentState[1] <- equity
- + currentState[2] <- equityAllocation
- + currentState[3] <- maxEquityAchieved
- + currentState[4] <- tradingDay
- + currentState[5] <- simulationData[tradingDay,"Price"]
- + currentState[6] <- simulationData[tradingDay,"Payems.short"]
- + currentState[7] <- simulationData[tradingDay,"Payems.medium"]
- + currentState[8] <- simulationData[tradingDay,"Payems.long"]
- + currentState[9] <- simulationData[tradingDay,"Unrate.short"]
- + currentState[10] <- simulationData[tradingDay,"Unrate.medium"]
- + currentState[11] <- simulationData[tradingDay,"Unrate.long"]
- + currentState[12] <- simulationData[tradingDay,"Gdp.short"]
- + currentState[13] <- simulationData[tradingDay,"Gdp.medium"]
- + currentState[14] <- simulationData[tradingDay,"Gdp.long"]
- + return (currentState)
- + }
- >
- >
- >
- > trading.UpdateFitness <- function(oldState,updatedState,oldFitness){
- + return (as.double(updatedState[1])) #equity achieved
- + }
- >
- > trading.CheckForTermination <- function(frameNum,oldState,updatedState,oldFitness,newFitness){
- + equity <- updatedState[[1]]
- + equityAllocation <- updatedState[[2]]
- + maxEquityAchieved <- updatedState[[3]]
- + tradingDay <- updatedState[[4]]
- + if(tradingDay >= nrow(simulationData)){
- + return(T)
- + }
- +
- + if(abs(equityAllocation) > 2){ #Too much leverage
- + return(T)
- + }
- +
- + if(equity/maxEquityAchieved < 0.8){ #20% draw down
- + return(T)
- + } else {
- + return (F)
- + }
- + }
- >
- > trading.PlotState <-function(updatedState){
- + equity <- currentState[[1]]
- + equityAllocation <- currentState[[2]]
- + maxEquityAchieved <- currentState[[3]]
- + plot(updatedState)
- + }
- >
- > plotStateAndInputDataFunc <- function(stateData, inputData, titleText){
- + buyandholdret <- inputData[,"Price"]/coredata(inputData[1,"Price"])
- + strategyret <- stateData[,"Equity"]/100
- +
- + maxbuyandholdret <- cummax(buyandholdret)
- +
- + buyandholddrawdown <- (buyandholdret/maxbuyandholdret-1)
- + strategydrawdown <- (stateData[,"Equity"]/stateData[,"MaxEquity"]-1)
- +
- + dev.new()
- + par(mfrow=c(4,2),oma = c(0, 0, 2, 0))
- + plot(stateData[,"Price"],main="Price",ylab="Price")
- + plot(buyandholdret,main="Performance (Return on Initial Equity)", ylab="Return", ylim=c(min(buyandholdret,strategyret),max(buyandholdret,strategyret)))
- + lines(strategyret,col="red")
- + legend(x='bottomright', c('Buy & Hold','Strategy'), fill=c("black","red"), bty='n')
- + plot(inputData[,"ClClRet"],main="Stock Returns", ylab="Return")
- + plot(maxbuyandholdret*100,main="Max Equity", ylim=c(min(maxbuyandholdret*100,stateData[,"MaxEquity"]),max(maxbuyandholdret*100,stateData[,"MaxEquity"])),ylab="Equity $")
- + lines(stateData[,"MaxEquity"],col="red")
- + legend(x='bottomright', c('Buy & Hold','Strategy'), fill=c("black","red"), bty='n')
- + plot(inputData[,"Payems.short"], main="Payrolls Rolling Percentile",ylab="Percentile")
- + lines(inputData[,"Payems.medium"], col="red")
- + lines(inputData[,"Payems.long"], col="blue")
- + legend(x='bottomright', c(paste(nEconomicDataPercentileLookbackShort,"Points"),
- + paste(nEconomicDataPercentileLookbackMedium,"Points"),
- + paste(nEconomicDataPercentileLookbackLong,"Points")), fill=c("black","red","blue"), bty='n')
- +
- + plot(buyandholddrawdown,main="Draw Down",ylab="Percent (%)")
- + lines(strategydrawdown,col="red")
- + legend(x='bottomright', c('Buy & Hold','Strategy'), fill=c("black","red"), bty='n')
- + plot(stateData[,"Allocation"],main="Allocation",ylab="Allocation")
- +
- +
- + mtext(titleText, outer = TRUE, cex = 1.5)
- + }
- >
- >
- > config <- newConfigNEAT(14,1,500,50)
- Error: could not find function "newConfigNEAT"
- > tradingSimulation <- newNEATSimulation(config, trading.InitialState,
- + trading.UpdateState,
- + trading.ConvertStateToNeuralNetInputs,
- + trading.UpdateFitness,
- + trading.CheckForTermination,
- + trading.PlotState)
- Error: could not find function "newNEATSimulation"
- >
- > tradingSimulation <- NEATSimulation.RunSingleGeneration(tradingSimulation)
- Error: could not find function "NEATSimulation.RunSingleGeneration"
- >
- > for(i in seq(1,35)){
- + save.image(file="tradingSim.RData") #So we can recover if we crash for any reason
- + tradingSimulation <- NEATSimulation.RunSingleGeneration(tradingSimulation)
- + }
- Error: could not find function "NEATSimulation.RunSingleGeneration"
- >
- > dev.new()
- > plot(tradingSimulation)
- Error in plot(tradingSimulation) : object 'tradingSimulation' not found
- >
- > stateHist <- NEATSimulation.GetStateHistoryForGenomeAndSpecies(tradingSimulation)
- Error: could not find function "NEATSimulation.GetStateHistoryForGenomeAndSpecies"
- >
- >
- > colnames(stateHist) <- c("Equity","Allocation","MaxEquity","TradingDay","Price",
- + "Payems.short","Payems.medium","Payems.long",
- + "Unrate.short","Unrate.medium","Unrate.long",
- + "Gdp.short","Gdp.medium","Gdp.long")
- Error in colnames(stateHist) <- c("Equity", "Allocation", "MaxEquity", :
- object 'stateHist' not found
- >
- > row.names(stateHist)<-row.names(as.data.frame(simulationData[1:nrow(stateHist),]))
- Error in nrow(stateHist) : object 'stateHist' not found
- > stateHist <- as.zoo(stateHist)
- Error in as.zoo(stateHist) : object 'stateHist' not found
- > plotStateAndInputDataFunc(stateHist,simulationData,"Training Data")
- Error in plotStateAndInputDataFunc(stateHist, simulationData, "Training Data") :
- object 'stateHist' not found
- >
- >
- > simulationData <- featuresOutOfSampleData
- > stateHist <- NEATSimulation.GetStateHistoryForGenomeAndSpecies(tradingSimulation)
- Error: could not find function "NEATSimulation.GetStateHistoryForGenomeAndSpecies"
- >
- > colnames(stateHist) <- c("Equity","Allocation","MaxEquity","TradingDay","Price",
- + "Payems.short","Payems.medium","Payems.long",
- + "Unrate.short","Unrate.medium","Unrate.long",
- + "Gdp.short","Gdp.medium","Gdp.long")
- Error in colnames(stateHist) <- c("Equity", "Allocation", "MaxEquity", :
- object 'stateHist' not found
- >
- > row.names(stateHist)<-row.names(as.data.frame(simulationData[1:nrow(stateHist),]))
- Error in nrow(stateHist) : object 'stateHist' not found
- > stateHist <- as.zoo(stateHist)
- Error in as.zoo(stateHist) : object 'stateHist' not found
- >
- > plotStateAndInputDataFunc(stateHist,simulationData,"Out of Sample Data")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement