Skip to content

Instantly share code, notes, and snippets.

@zachmayer
Created September 12, 2011 21:43
Show Gist options
  • Save zachmayer/1212547 to your computer and use it in GitHub Desktop.
Save zachmayer/1212547 to your computer and use it in GitHub Desktop.
200
rm(list = ls(all = TRUE))
#http://etfprophet.com/days-since-200-day-highs/
require(quantmod)
getSymbols('^GSPC',from='1900-01-01')
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)
}
myStock <- Cl(GSPC)
myPosition <- myStrat(myStock,100,200)
bmkReturns <- dailyReturn(myStock, type = "arithmetic")
myReturns <- bmkReturns*Lag(myPosition,1)
myReturns[1] <- 0
names(bmkReturns) <- 'SP500'
names(myReturns) <- 'Me'
require(PerformanceAnalytics)
charts.PerformanceSummary(cbind(bmkReturns,myReturns))
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)
}
cbind(Me=Performance(myReturns),SP500=Performance(bmkReturns))
Me SP500
Cumulative Return 77.03146070 71.15426170
Annual Return 0.07326067 0.07189781
Annualized Sharpe Ratio 0.63648679 0.46535064
Win % 0.54484242 0.53242454
Annualized Volatility 0.11510163 0.15450243
Maximum Drawdown -0.33509517 -0.56775389
Max Length Drawdown 1553.00000000 1898.00000000
testStrategy <- function(myStock, nHold=100, nHigh=200) {
myPosition <- myStrat(myStock,nHold,nHigh)
bmkReturns <- dailyReturn(myStock, type = "arithmetic")
myReturns <- bmkReturns*Lag(myPosition,1)
myReturns[1] <- 0
names(bmkReturns) <- 'Index'
names(myReturns) <- 'Me'
charts.PerformanceSummary(cbind(bmkReturns,myReturns))
cbind(Me=Performance(myReturns),Index=Performance(bmkReturns))
}
getSymbols('^FTSE',from='1900-01-01')
getSymbols('DJIA', src='FRED')
getSymbols('^N225',from='1900-01-01')
testStrategy(Cl(FTSE),100,200)
testStrategy(na.omit(DJIA),100,200)
round(testStrategy(Cl(N225),100,200),8)
FTSE:
Me Index
Cumulative Return 3.56248582 3.8404476
Annual Return 0.05667121 0.0589431
Annualized Sharpe Ratio 0.45907768 0.3298633
Win % 0.53216374 0.5239884
Annualized Volatility 0.12344579 0.1786895
Maximum Drawdown -0.39653398 -0.5256991
Max Length Drawdown 1633.00000000 2960.0000000
DJIA:
Me Index
Cumulative Return 364.53412355 277.66780655
Annual Return 0.05282208 0.05033327
Annualized Sharpe Ratio 0.40688871 0.27453532
Win % 0.53748349 0.52523599
Annualized Volatility 0.12981947 0.18333987
Maximum Drawdown -0.53822732 -0.89185928
Max Length Drawdown 3847.00000000 6301.00000000
N225:
Me Index
Cumulative Return 0.98108503 -0.12146268
Annual Return 0.02560154 -0.00477699
Annualized Sharpe Ratio 0.17677957 -0.02053661
Win % 0.52871073 0.51337842
Annualized Volatility 0.14482181 0.23260863
Maximum Drawdown -0.49361328 -0.81871261
Max Length Drawdown 5342.00000000 5342.00000000
@sanjidamahin
Copy link

I cannot use the code. It is shown that
"there is no package called ‘PerformanceAnalytics"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment