Skip to content

Instantly share code, notes, and snippets.

@milktrader
Created March 24, 2011 14:44
Show Gist options
  • Save milktrader/885171 to your computer and use it in GitHub Desktop.
Save milktrader/885171 to your computer and use it in GitHub Desktop.
weighted coin
lemon_lime <- function(sym="SLV", expiry=24, pct=.1){
require("quantmod")
ticker <- getSymbols(sym, auto.assign=FALSE)
ticker <- ticker[,4]
win <- 0
lose <- 0
for(i in expiry:NROW(ticker))
if(as.numeric(ticker[i]) > (as.numeric(ticker[i-(expiry-1)])*pct + as.numeric(ticker[i-(expiry-1)])) )
win <- win + 1
winners <- win/(NROW(ticker)-expiry)
for(i in expiry:NROW(ticker))
if(as.numeric(ticker[i]) < (as.numeric(ticker[i-(expiry-1)]) - as.numeric(ticker[i-(expiry-1)])*pct) )
lose <- lose + 1
losers <- lose/(NROW(ticker)-expiry)
weighted <- win/(win+lose)
cat( " Trading Days: ", NROW(ticker)-expiry, "\n",
"Winners: ", win, "\n",
"Percentage of winning days: ", round(winners*100, digits=2), "\n",
"Losers: ", lose, "\n",
"Percentage of losing days: ", round(losers*100, digits=2), "\n",
"Weighted Coin Towards Winners? ", round(weighted, digits=2), "\n")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment