Skip to content

Instantly share code, notes, and snippets.

@milktrader
Created April 7, 2011 16:02
Show Gist options
  • Save milktrader/908077 to your computer and use it in GitHub Desktop.
Save milktrader/908077 to your computer and use it in GitHub Desktop.
require(quantmod)
require(vioplot)
getSymbols("GLD")
friend <- DonchianChannel( GLD[,c("GLD.High","GLD.Low")], n=20 )
buddy <- DonchianChannel( GLD[,c("GLD.High","GLD.Low")], n=10 )
pal <- DonchianChannel( GLD[,c("GLD.High","GLD.Low")], n=5 )
posse <- merge(friend$mid, buddy$mid, pal$mid)
GLD <- merge(GLD, posse)
GLD <- na.locf(GLD, na.rm=TRUE)
compadre <- ifelse(Cl(GLD) > GLD$mid & Cl(GLD) > GLD$mid.1 & Cl(GLD) > GLD$mid.2, 1,
ifelse(Cl(GLD) < GLD$mid & Cl(GLD) < GLD$mid.1 & Cl(GLD) < GLD$mid.2, -1, 0))
hist(compadre, main="What are your positions over the past few years?", col="goldenrod")
dinero <- Lag(ROC(Cl(GLD)))*compadre
dinero <- na.locf(dinero, na.rm=TRUE)
vioplot(dinero, names="Distribution of 'compadre' daily returns", col="blue")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment