Skip to content

Instantly share code, notes, and snippets.

@zachmayer
Created September 16, 2011 17:36
Show Gist options
  • Save zachmayer/1222639 to your computer and use it in GitHub Desktop.
Save zachmayer/1222639 to your computer and use it in GitHub Desktop.
Backtesting a Simple Stock Trading Strategy 2
rm(list = ls(all = TRUE))
daysSinceHigh <- function(x, n){
apply(embed(x, n), 1, which.max)-1
}
myStrat <- function(x, nHold=100, nHigh=200) {
position <- ifelse(daysSinceHigh(x, nHigh)<=nHold,1,0)
c(rep(0,nHigh-1),position)
}
Performance <- function(x) {
cumRetx = Return.cumulative(x)
annRetx = Return.annualized(x, scale=252)
sharpex = SharpeRatio.annualized(x, scale=252)
winpctx = length(x[x > 0])/length(x[x != 0])
annSDx = sd.annualized(x, scale=252)
DDs <- findDrawdowns(x)
maxDDx = min(DDs$return)
maxLx = max(DDs$length)
Perf = c(cumRetx, annRetx, sharpex, winpctx, annSDx, maxDDx, maxLx)
names(Perf) = c("Cumulative Return", "Annual Return","Annualized Sharpe Ratio",
"Win %", "Annualized Volatility", "Maximum Drawdown", "Max Length Drawdown")
return(Perf)
}
testStrategy <- function(symbol, nHold=100, nHigh=200, cost=.005, ylog=FALSE, wealth.index = FALSE, ...) {
require(quantmod)
#Load Data
myStock <- getSymbols(symbol,from='1900-01-01')
myStock <- adjustOHLC(get(myStock),symbol.name=symbol)
myStock <- Cl(myStock)
#Determine position
myPosition <- myStrat(myStock,nHold,nHigh)
bmkReturns <- dailyReturn(myStock, type = "arithmetic")
myReturns <- bmkReturns*myPosition
names(bmkReturns) <- symbol
names(myReturns) <- 'Me'
#Add trading costs
trade = as.numeric(myPosition!=Lag(myPosition,1))
trade[1] = 1
trade = trade*cost
myReturns = myReturns-trade
#Make plot
require(PerformanceAnalytics)
symbol <- sub('^','',symbol,fixed=TRUE)
Title <- paste('High=',nHigh,' Hold=',nHold,' on ',symbol,sep='')
if (ylog) {wealth.index = TRUE}
layout(matrix(c(1, 2, 3)), height = c(2, 1, 1.3), width = 1)
par(mar = c(1, 4, 4, 2))
chart.CumReturns(cbind(bmkReturns,myReturns), main=Title, ylab = "Cumulative Return",
wealth.index = wealth.index,ylog=ylog,...)
chart.RelativePerformance(myReturns,bmkReturns, ylab = "Relative Return", main = "")
chart.Drawdown(cbind(bmkReturns,myReturns),legend.loc = 'bottomleft', ylab = "Drawdown", main = "")
#Return Benchmarked Stats
cbind(Me=Performance(myReturns),Index=Performance(bmkReturns))
}
testStrategy('^GSPC',100,200,ylog=TRUE)
testStrategy('^FTSE',100,200)
testStrategy('^DJI',100,200,ylog=TRUE)
round(testStrategy('^N225',100,200),8)
testStrategy('EEM',100,200)
testStrategy('EFA',100,200)
testStrategy('GLD',100,200)
GSPC (US Large Cap):
Me Index
Cumulative Return 68.51646034 71.57563025
Annual Return 0.07126447 0.07201349
Annualized Sharpe Ratio 0.61891754 0.46607583
Win % 0.54326744 0.53246332
Annualized Volatility 0.11514373 0.15451024
Maximum Drawdown -0.33509517 -0.56775389
Max Length Drawdown 1378.00000000 1898.00000000
FTSE (UK and Ireland):
Me Index
Cumulative Return 3.27952156 3.81680354
Annual Return 0.05424115 0.05878095
Annualized Sharpe Ratio 0.43889935 0.32904067
Win % 0.53084556 0.52392656
Annualized Volatility 0.12358449 0.17864343
Maximum Drawdown -0.40641281 -0.52569911
Max Length Drawdown 2415.00000000 2957.00000000
DJI (US Large Industrial):
Me Index
Cumulative Return 54.55858145 46.63626516
Annual Return 0.04979339 0.04784172
Annualized Sharpe Ratio 0.39235672 0.25958143
Win % 0.53111604 0.52284558
Annualized Volatility 0.12690847 0.18430331
Maximum Drawdown -0.47873652 -0.89185928
Max Length Drawdown 2936.00000000 6301.00000000
N225 (Japan):
Me Index
Cumulative Return 1.19191792 -0.12673920
Annual Return 0.02945282 -0.00500012
Annualized Sharpe Ratio 0.20222700 -0.02149834
Win % 0.52668464 0.51338235
Annualized Volatility 0.14564236 0.23258184
Maximum Drawdown -0.48041098 -0.81871261
Max Length Drawdown 5340.00000000 5340.00000000
EEM (Emerging Markets):
Me Index
Cumulative Return 1.3593876 2.8207362
Annual Return 0.1073174 0.1725555
Annualized Sharpe Ratio 0.4722376 0.4880397
Win % 0.5393858 0.5440758
Annualized Volatility 0.2272529 0.3535685
Maximum Drawdown -0.2623762 -0.6709596
Max Length Drawdown 488.0000000 977.0000000
EFA (Developed Markets):
Me Index
Cumulative Return 0.49566232 0.41127015
Annual Return 0.04092907 0.03492237
Annualized Sharpe Ratio 0.24790378 0.13942465
Win % 0.53234116 0.53253493
Annualized Volatility 0.16510063 0.25047485
Maximum Drawdown -0.25703652 -0.61761011
Max Length Drawdown 589.00000000 977.00000000
GLD (Gold):
Me Index
Cumulative Return 1.7094607 2.9296981
Annual Return 0.1573355 0.2221688
Annualized Sharpe Ratio 0.9320789 1.0453248
Win % 0.5545603 0.5517847
Annualized Volatility 0.1688006 0.2125357
Maximum Drawdown -0.2326084 -0.2941414
Max Length Drawdown 433.0000000 379.0000000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment