Skip to content

Instantly share code, notes, and snippets.

@zachmayer
Created October 20, 2011 20:19
Show Gist options
  • Save zachmayer/1302229 to your computer and use it in GitHub Desktop.
Save zachmayer/1302229 to your computer and use it in GitHub Desktop.
Random Strategies
#Setup
rm(list = ls(all = TRUE))
set.seed(1)
nsims <- 1000
library(quantmod)
library(PerformanceAnalytics)
#Load Data
getSymbols('^GSPC',from='1900-01-01')
spyReturns <- dailyReturn(GSPC$GSPC.Adjusted, type = "arithmetic")
#Make 1000 random position vectors
randomPositions <- sample(c(0,1),nsims*length(spyReturns),TRUE)
randomPositions <- matrix(randomPositions,ncol=nsims)
#Determine trades and calculate trade costs
trades <- apply(randomPositions,2,Lag,1) != randomPositions
trades[1,] <- trades[1,]==1
trades <- ifelse(trades,0.001,0) #Assume the commision is 0.5%
#Determine dailty returns of random strats
randomStrats <- randomPositions*matrix(rep(spyReturns,nsims),ncol=nsims)
randomStrats <- randomStrats-trades
randomStrats.return <- apply(randomStrats,2,Return.annualized,scale=252)
hist(randomStrats.return, freq=FALSE)
#Plot some of the strategies
library(PerformanceAnalytics)
charts.PerformanceSummary(na.omit(cbind(spyReturns,randomStrats)[,1:15]),colorset=redfocus,)
#Same plot, removes buy&hold
#charts.PerformanceSummary(na.omit(cbind(spyReturns,randomStrats)[,2:15]),colorset=redfocus,)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment