Skip to content

Instantly share code, notes, and snippets.

@milktrader
Created April 27, 2011 14:21
Show Gist options
  • Save milktrader/944325 to your computer and use it in GitHub Desktop.
Save milktrader/944325 to your computer and use it in GitHub Desktop.
gnat trading algorithm based on a simple moving average crossover
gnat <- function(sym="GLD", fast=50, slow=200){
require("quantmod")
x <- getSymbols(sym, auto.assign=FALSE)
x$fast <- SMA(Cl(x), n=fast)
x$slow <- SMA(Cl(x), n=slow)
x$signal <- Lag(ifelse (x$fast > x$slow, 1, -1))
x <- na.omit(x)
x$log_ret <- dailyReturn(Cl(x), type="log")*x$signal
x$equity <- exp(cumsum(x$log_ret))
comment(x) <- paste("This is the gnat algorithm for", sym,
"with parameters of", fast, "and", slow)
bug <- paste(sym,"gnat", sep=".")
assign(bug, x, envir=.GlobalEnv)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment