Advertisement
Guest User

Untitled

a guest
Nov 8th, 2016
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.96 KB | None | 0 0
  1.  
  2. R version 3.3.2 (2016-10-31) -- "Sincere Pumpkin Patch"
  3. Copyright (C) 2016 The R Foundation for Statistical Computing
  4. Platform: x86_64-w64-mingw32/x64 (64-bit)
  5.  
  6. R is free software and comes with ABSOLUTELY NO WARRANTY.
  7. You are welcome to redistribute it under certain conditions.
  8. Type 'license()' or 'licence()' for distribution details.
  9.  
  10. Natural language support but running in an English locale
  11.  
  12. R is a collaborative project with many contributors.
  13. Type 'contributors()' for more information and
  14. 'citation()' on how to cite R or R packages in publications.
  15.  
  16. Type 'demo()' for some demos, 'help()' for on-line help, or
  17. 'help.start()' for an HTML browser interface to help.
  18. Type 'q()' to quit R.
  19.  
  20. > install.packages("devtools")
  21. Installing package into ‘C:/Users/user/Documents/R/win-library/3.3’
  22. (as ‘lib’ is unspecified)
  23. --- Please select a CRAN mirror for use in this session ---
  24. trying URL 'https://cloud.r-project.org/bin/windows/contrib/3.3/devtools_1.12.0.zip'
  25. Content type 'application/zip' length 432238 bytes (422 KB)
  26. downloaded 422 KB
  27.  
  28. package ‘devtools’ successfully unpacked and MD5 sums checked
  29.  
  30. The downloaded binary packages are in
  31. C:\Users\user\AppData\Local\Temp\RtmpayQjeU\downloaded_packages
  32. > library("devtools")
  33. > install_github("RNeat","ahunteruk") #Install from github as not yet on CRAN
  34. Downloading GitHub repo ahunteruk/RNeat@master
  35. from URL https://api.github.com/repos/ahunteruk/RNeat/zipball/master
  36. Installing RNeat
  37. "C:/PROGRA~1/R/R-33~1.2/bin/x64/R" --no-site-file --no-environ --no-save --no-restore --quiet CMD INSTALL \
  38. "C:/Users/user/AppData/Local/Temp/RtmpayQjeU/devtools25f0467a70b0/ahunteruk-RNeat-f7c7eec" \
  39. --library="C:/Users/user/Documents/R/win-library/3.3" --install-tests
  40.  
  41. * installing *source* package 'RNeat' ...
  42. ** R
  43. ** preparing package for lazy loading
  44. Error in loadNamespace(j <- i[[1L]], c(lib.loc, .libPaths()), versionCheck = vI[[j]]) :
  45. there is no package called 'magrittr'
  46. ERROR: lazy loading failed for package 'RNeat'
  47. * removing 'C:/Users/user/Documents/R/win-library/3.3/RNeat'
  48. Error: Command failed (1)
  49. In addition: Warning message:
  50. Username parameter is deprecated. Please use ahunteruk/RNeat
  51. > library("RNeat")
  52. Error in library("RNeat") : there is no package called ‘RNeat’
  53. > library("quantmod")
  54. Loading required package: xts
  55. Loading required package: zoo
  56.  
  57. Attaching package: ‘zoo’
  58.  
  59. The following objects are masked from ‘package:base’:
  60.  
  61. as.Date, as.Date.numeric
  62.  
  63. Loading required package: TTR
  64. Version 0.4-0 included new data defaults. See ?getSymbols.
  65. >
  66. > marketSymbol <- "^GSPC"
  67. > econmicDataSymbols <- c("UNRATE","PAYEMS","GDP")
  68. >
  69. > mktData <- new.env() #Make a new environment for quantmod to store data in
  70. > economicData <- new.env() #Make a new environment for quantmod to store data in
  71. >
  72. > #Specify dates for downloading data, training models and running simulation
  73. > dataDownloadStartDate <- as.Date("2000-06-01")
  74. >
  75. > trainingStartDate = as.Date("2001-01-01") #Specify the date to start training (yyyy-mm-dd)
  76. > trainingEndDate = as.Date("2006-12-31") #Specify the date to end training
  77. >
  78. > outOfSampleStartDate = as.Date("2007-01-01")
  79. > outOfSampleEndDate = as.Date("2016-07-15")
  80. >
  81. > #Download Data
  82. > getSymbols(marketSymbol,env=mktData,from=dataDownloadStartDate) #S&P 500
  83. As of 0.4-0, ‘getSymbols’ uses env=parent.frame() and
  84. auto.assign=TRUE by default.
  85.  
  86. This behavior will be phased out in 0.5-0 when the call will
  87. default to use auto.assign=FALSE. getOption("getSymbols.env") and
  88. getOptions("getSymbols.auto.assign") are now checked for alternate defaults
  89.  
  90. This message is shown once per session and may be disabled by setting
  91. options("getSymbols.warning4.0"=FALSE). See ?getSymbols for more details.
  92. [1] "GSPC"
  93. > getSymbols(econmicDataSymbols,src="FRED",env=economicData,from=dataDownloadStartDate) #Payems is non-farms payrolls
  94. [1] "UNRATE" "PAYEMS" "GDP"
  95. >
  96. > nEconomicDataPercentileLookbackShort <- 20
  97. > nEconomicDataPercentileLookbackMedium <- 50
  98. > nEconomicDataPercentileLookbackLong <- 100
  99. >
  100. > rollingPercentile <- function(data,n){
  101. + percentile <- function(dataBlock){
  102. + last(rank(dataBlock)/length(dataBlock))
  103. + }
  104. + return (as.zoo(rollapply(as.zoo(data),width=n,percentile,align="right",by.column=TRUE)))
  105. + }
  106. >
  107. > stockCleanNameFunc <- function(name){
  108. + return(sub("^","",name,fixed=TRUE))
  109. + }
  110. >
  111. > clClRet <- as.zoo((lag(Cl(get(stockCleanNameFunc(marketSymbol),mktData)),-1)/Cl(get(stockCleanNameFunc(marketSymbol),mktData))-1))
  112. >
  113. > payemsShortPercentile <- rollingPercentile(economicData$PAYEMS,nEconomicDataPercentileLookbackShort)
  114. > payemsMediumPercentile <- rollingPercentile(economicData$PAYEMS,nEconomicDataPercentileLookbackMedium)
  115. > payemsLongPercentile <- rollingPercentile(economicData$PAYEMS,nEconomicDataPercentileLookbackLong)
  116. >
  117. > unrateShortPercentile <- rollingPercentile(economicData$UNRATE,nEconomicDataPercentileLookbackShort)
  118. > unrateMediumPercentile <- rollingPercentile(economicData$UNRATE,nEconomicDataPercentileLookbackMedium)
  119. > unrateLongPercentile <- rollingPercentile(economicData$UNRATE,nEconomicDataPercentileLookbackLong)
  120. >
  121. > gdpShortPercentile <- rollingPercentile(economicData$GDP,nEconomicDataPercentileLookbackShort)
  122. > gdpMediumPercentile <- rollingPercentile(economicData$GDP,nEconomicDataPercentileLookbackMedium)
  123. > gdpLongPercentile <- rollingPercentile(economicData$GDP,nEconomicDataPercentileLookbackLong)
  124. >
  125. > #join the data sets, fill in any missing dates with the previous none NA value
  126. > mergedData <- na.locf(merge(economicData$PAYEMS,merge(Cl(get(stockCleanNameFunc(marketSymbol),mktData)),
  127. + economicData$PAYEMS,payemsShortPercentile,payemsMediumPercentile,payemsLongPercentile, economicData$UNRATE,unrateShortPercentile,unrateMediumPercentile,unrateLongPercentile,
  128. + economicData$GDP,gdpShortPercentile,gdpMediumPercentile,gdpLongPercentile
  129. + ,all.x=T),all=T))
  130. > mergedData <- mergedData[,-1]
  131. > ClClRet <- as.zoo(lag(mergedData[,1],-1)/mergedData[,1]-1)
  132. > ClTZero <- as.zoo(mergedData[,1])
  133. > ClTOne <- as.zoo(lag(mergedData[,1],-1))
  134. > mergedData <- merge(ClClRet,ClTOne,ClTZero,mergedData)
  135. > mergedData <- window(mergedData,start=dataDownloadStartDate)
  136. >
  137. > colnames(mergedData) <- c("ClClRet","ClTOne","ClTZero","Price","Payems","Payems.short","Payems.medium","Payems.long",
  138. + "Unrate","Unrate.short","Unrate.medium","Unrate.long",
  139. + "Gdp","Gdp.short","Gdp.medium","Gdp.long","all.x")
  140. >
  141. >
  142. > dev.new()
  143. > par(mfrow=c(4,2))
  144. > plot(mergedData[,"Price"], main="S&P Close Price",ylab="Close Price")
  145. > plot(mergedData[,"ClClRet"], main="S&P Close Price",ylab="Close Price")
  146. >
  147. > plot(mergedData[,"Payems"], main="Non-Farm Payrolls",ylab="Thousands of Persons")
  148. > plot(mergedData[,"Payems.short"], main="Non-Farm Payrolls Rolling Percentile",ylab="Percentile")
  149. > lines(mergedData[,"Payems.medium"], col="red")
  150. > lines(mergedData[,"Payems.long"], col="blue")
  151. > legend(x='bottomright', c(paste(nEconomicDataPercentileLookbackShort,"Points"),
  152. + paste(nEconomicDataPercentileLookbackMedium,"Points"),
  153. + paste(nEconomicDataPercentileLookbackLong,"Points")), fill=c("black","red","blue"), bty='n')
  154. >
  155. > plot(mergedData[,"Unrate"], main="Unemployment Rate",ylab="Percent")
  156. > plot(mergedData[,"Unrate.short"], main="Unemployment Rate Rolling Percentile",ylab="Percentile")
  157. > lines(mergedData[,"Unrate.medium"], col="red")
  158. > lines(mergedData[,"Unrate.long"], col="blue")
  159. > legend(x='bottomright', c(paste(nEconomicDataPercentileLookbackShort,"Points"),
  160. + paste(nEconomicDataPercentileLookbackMedium,"Points"),
  161. + paste(nEconomicDataPercentileLookbackLong,"Points")), fill=c("black","red","blue"), bty='n')
  162. > plot(mergedData[,"Gdp"], main="GDP",ylab="Billions of USD")
  163. > plot(mergedData[,"Gdp.short"], main="GBP Rolling Percentile",ylab="Percentile")
  164. > lines(mergedData[,"Gdp.medium"], col="red")
  165. > lines(mergedData[,"Gdp.long"], col="blue")
  166. > legend(x='bottomright', c(paste(nEconomicDataPercentileLookbackShort,"Points"),
  167. + paste(nEconomicDataPercentileLookbackMedium,"Points"),
  168. + paste(nEconomicDataPercentileLookbackLong,"Points")), fill=c("black","red","blue"), bty='n')
  169. >
  170. > featuresTrainingData <- window(mergedData,start=trainingStartDate,end=trainingEndDate)
  171. > featuresOutOfSampleData <- window(mergedData,start=outOfSampleStartDate,end=outOfSampleEndDate)
  172. >
  173. >
  174. > #Genetic algo setup
  175. > simulationData <- featuresTrainingData
  176. >
  177. > trading.InitialState <- function(){
  178. + state <- list()
  179. + state[1] <- 100 #Equity
  180. + state[2] <- 0 #% of Equity allocated to share (-ve for shorts)
  181. + state[3] <- state[1] #Maximum equity achieved
  182. + state[4] <- 1 #Trading day number
  183. + state[5] <- simulationData[1,"Price"]
  184. + state[6] <- simulationData[1,"Payems.short"]
  185. + state[7] <- simulationData[1,"Payems.medium"]
  186. + state[8] <- simulationData[1,"Payems.long"]
  187. + state[9] <- simulationData[1,"Unrate.short"]
  188. + state[10] <- simulationData[1,"Unrate.medium"]
  189. + state[11] <- simulationData[1,"Unrate.long"]
  190. + state[12] <- simulationData[1,"Gdp.short"]
  191. + state[13] <- simulationData[1,"Gdp.medium"]
  192. + state[14] <- simulationData[1,"Gdp.long"]
  193. + return(state)
  194. + }
  195. >
  196. > trading.ConvertStateToNeuralNetInputs <- function(currentState){
  197. + return (currentState)
  198. + }
  199. >
  200. > trading.UpdateState <- function(currentState,neuralNetOutputs){
  201. + #print(currentState)
  202. + equity <- currentState[[1]]
  203. + equityAllocation <- neuralNetOutputs[[1]]
  204. + maxEquityAchieved <- currentState[[3]]
  205. + tradingDay <- currentState[[4]]
  206. +
  207. + pctChange <- as.double((simulationData[tradingDay+1,"Price"]))/as.double((simulationData[tradingDay,"Price"]))-1
  208. + #print(paste("pctChange",pctChange))
  209. + #print(paste("equityAllocation",equityAllocation))
  210. +
  211. + pnl <- equity * equityAllocation * pctChange
  212. +
  213. + equity <- equity + pnl
  214. + maxEquityAchieved <- max(maxEquityAchieved,equity)
  215. +
  216. + tradingDay <- tradingDay + 1
  217. + currentState[1] <- equity
  218. + currentState[2] <- equityAllocation
  219. + currentState[3] <- maxEquityAchieved
  220. + currentState[4] <- tradingDay
  221. + currentState[5] <- simulationData[tradingDay,"Price"]
  222. + currentState[6] <- simulationData[tradingDay,"Payems.short"]
  223. + currentState[7] <- simulationData[tradingDay,"Payems.medium"]
  224. + currentState[8] <- simulationData[tradingDay,"Payems.long"]
  225. + currentState[9] <- simulationData[tradingDay,"Unrate.short"]
  226. + currentState[10] <- simulationData[tradingDay,"Unrate.medium"]
  227. + currentState[11] <- simulationData[tradingDay,"Unrate.long"]
  228. + currentState[12] <- simulationData[tradingDay,"Gdp.short"]
  229. + currentState[13] <- simulationData[tradingDay,"Gdp.medium"]
  230. + currentState[14] <- simulationData[tradingDay,"Gdp.long"]
  231. + return (currentState)
  232. + }
  233. >
  234. >
  235. >
  236. > trading.UpdateFitness <- function(oldState,updatedState,oldFitness){
  237. + return (as.double(updatedState[1])) #equity achieved
  238. + }
  239. >
  240. > trading.CheckForTermination <- function(frameNum,oldState,updatedState,oldFitness,newFitness){
  241. + equity <- updatedState[[1]]
  242. + equityAllocation <- updatedState[[2]]
  243. + maxEquityAchieved <- updatedState[[3]]
  244. + tradingDay <- updatedState[[4]]
  245. + if(tradingDay >= nrow(simulationData)){
  246. + return(T)
  247. + }
  248. +
  249. + if(abs(equityAllocation) > 2){ #Too much leverage
  250. + return(T)
  251. + }
  252. +
  253. + if(equity/maxEquityAchieved < 0.8){ #20% draw down
  254. + return(T)
  255. + } else {
  256. + return (F)
  257. + }
  258. + }
  259. >
  260. > trading.PlotState <-function(updatedState){
  261. + equity <- currentState[[1]]
  262. + equityAllocation <- currentState[[2]]
  263. + maxEquityAchieved <- currentState[[3]]
  264. + plot(updatedState)
  265. + }
  266. >
  267. > plotStateAndInputDataFunc <- function(stateData, inputData, titleText){
  268. + buyandholdret <- inputData[,"Price"]/coredata(inputData[1,"Price"])
  269. + strategyret <- stateData[,"Equity"]/100
  270. +
  271. + maxbuyandholdret <- cummax(buyandholdret)
  272. +
  273. + buyandholddrawdown <- (buyandholdret/maxbuyandholdret-1)
  274. + strategydrawdown <- (stateData[,"Equity"]/stateData[,"MaxEquity"]-1)
  275. +
  276. + dev.new()
  277. + par(mfrow=c(4,2),oma = c(0, 0, 2, 0))
  278. + plot(stateData[,"Price"],main="Price",ylab="Price")
  279. + plot(buyandholdret,main="Performance (Return on Initial Equity)", ylab="Return", ylim=c(min(buyandholdret,strategyret),max(buyandholdret,strategyret)))
  280. + lines(strategyret,col="red")
  281. + legend(x='bottomright', c('Buy & Hold','Strategy'), fill=c("black","red"), bty='n')
  282. + plot(inputData[,"ClClRet"],main="Stock Returns", ylab="Return")
  283. + plot(maxbuyandholdret*100,main="Max Equity", ylim=c(min(maxbuyandholdret*100,stateData[,"MaxEquity"]),max(maxbuyandholdret*100,stateData[,"MaxEquity"])),ylab="Equity $")
  284. + lines(stateData[,"MaxEquity"],col="red")
  285. + legend(x='bottomright', c('Buy & Hold','Strategy'), fill=c("black","red"), bty='n')
  286. + plot(inputData[,"Payems.short"], main="Payrolls Rolling Percentile",ylab="Percentile")
  287. + lines(inputData[,"Payems.medium"], col="red")
  288. + lines(inputData[,"Payems.long"], col="blue")
  289. + legend(x='bottomright', c(paste(nEconomicDataPercentileLookbackShort,"Points"),
  290. + paste(nEconomicDataPercentileLookbackMedium,"Points"),
  291. + paste(nEconomicDataPercentileLookbackLong,"Points")), fill=c("black","red","blue"), bty='n')
  292. +
  293. + plot(buyandholddrawdown,main="Draw Down",ylab="Percent (%)")
  294. + lines(strategydrawdown,col="red")
  295. + legend(x='bottomright', c('Buy & Hold','Strategy'), fill=c("black","red"), bty='n')
  296. + plot(stateData[,"Allocation"],main="Allocation",ylab="Allocation")
  297. +
  298. +
  299. + mtext(titleText, outer = TRUE, cex = 1.5)
  300. + }
  301. >
  302. >
  303. > config <- newConfigNEAT(14,1,500,50)
  304. Error: could not find function "newConfigNEAT"
  305. > tradingSimulation <- newNEATSimulation(config, trading.InitialState,
  306. + trading.UpdateState,
  307. + trading.ConvertStateToNeuralNetInputs,
  308. + trading.UpdateFitness,
  309. + trading.CheckForTermination,
  310. + trading.PlotState)
  311. Error: could not find function "newNEATSimulation"
  312. >
  313. > tradingSimulation <- NEATSimulation.RunSingleGeneration(tradingSimulation)
  314. Error: could not find function "NEATSimulation.RunSingleGeneration"
  315. >
  316. > for(i in seq(1,35)){
  317. + save.image(file="tradingSim.RData") #So we can recover if we crash for any reason
  318. + tradingSimulation <- NEATSimulation.RunSingleGeneration(tradingSimulation)
  319. + }
  320. Error: could not find function "NEATSimulation.RunSingleGeneration"
  321. >
  322. > dev.new()
  323. > plot(tradingSimulation)
  324. Error in plot(tradingSimulation) : object 'tradingSimulation' not found
  325. >
  326. > stateHist <- NEATSimulation.GetStateHistoryForGenomeAndSpecies(tradingSimulation)
  327. Error: could not find function "NEATSimulation.GetStateHistoryForGenomeAndSpecies"
  328. >
  329. >
  330. > colnames(stateHist) <- c("Equity","Allocation","MaxEquity","TradingDay","Price",
  331. + "Payems.short","Payems.medium","Payems.long",
  332. + "Unrate.short","Unrate.medium","Unrate.long",
  333. + "Gdp.short","Gdp.medium","Gdp.long")
  334. Error in colnames(stateHist) <- c("Equity", "Allocation", "MaxEquity", :
  335. object 'stateHist' not found
  336. >
  337. > row.names(stateHist)<-row.names(as.data.frame(simulationData[1:nrow(stateHist),]))
  338. Error in nrow(stateHist) : object 'stateHist' not found
  339. > stateHist <- as.zoo(stateHist)
  340. Error in as.zoo(stateHist) : object 'stateHist' not found
  341. > plotStateAndInputDataFunc(stateHist,simulationData,"Training Data")
  342. Error in plotStateAndInputDataFunc(stateHist, simulationData, "Training Data") :
  343. object 'stateHist' not found
  344. >
  345. >
  346. > simulationData <- featuresOutOfSampleData
  347. > stateHist <- NEATSimulation.GetStateHistoryForGenomeAndSpecies(tradingSimulation)
  348. Error: could not find function "NEATSimulation.GetStateHistoryForGenomeAndSpecies"
  349. >
  350. > colnames(stateHist) <- c("Equity","Allocation","MaxEquity","TradingDay","Price",
  351. + "Payems.short","Payems.medium","Payems.long",
  352. + "Unrate.short","Unrate.medium","Unrate.long",
  353. + "Gdp.short","Gdp.medium","Gdp.long")
  354. Error in colnames(stateHist) <- c("Equity", "Allocation", "MaxEquity", :
  355. object 'stateHist' not found
  356. >
  357. > row.names(stateHist)<-row.names(as.data.frame(simulationData[1:nrow(stateHist),]))
  358. Error in nrow(stateHist) : object 'stateHist' not found
  359. > stateHist <- as.zoo(stateHist)
  360. Error in as.zoo(stateHist) : object 'stateHist' not found
  361. >
  362. > plotStateAndInputDataFunc(stateHist,simulationData,"Out of Sample Data")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement