Bug Collector

April 26, 2011
By

(This article was first published on Milk Trader, and kindly contributed to R-bloggers)

Most are quite unamused to find an ant infestation in their kitchen around this time of year. Time to start spraying that stuff that's not supposed to be harmful to humans but that you always wonder if it is anyway. Some bugs, such as ant attacks or software glitches, are bad. But I've got a growing collection of good bugs. Trading bugs written in R.

Below is the R code for the gnat algorithm. It is a simple moving-average crossover system, set with the golden-cross default settings of 50 and 200. You can simply copy it into your R console to play with it. It returns a new object into your global environment (fancy talk for your workspace). The object will be named by whatever symbol you pass into the function, appended with .gnat. So if you leave the parameters alone, you'll get GLD.gnat in your workspace. After running the program, you can check thus:

> gnat()
> ls()
> "GLD.gnat" "gnat" "/previously_loaded_objects/"

If you don't like GLD.gnat because it's awkward to type, you can change it to something more exciting such as this:

> a <- GLD.gnat

And perhaps you want to get a dozen markets in your workspace and you forgot what you assigned to the letter "k". Well, I've got you covered here my friend. I do this sort of thing all the time so I threw in a little comment attachment wizardry in the code. Simply type the following:

> comment(k)
[1] "This is the gnat algorithm for TLT with parameters of 50 and 200"

This is the cool part. To see what the equity curve looks like, type"

> plot(a$equity)


To see how daily returns look like, type:

> hist(exp(a$log_ret))



If you are a gentleman, you compute quick-and-dirty expected return like this:

> mean(exp(a$log_ret)

If this number is bigger than 1, you have something to work with.

Once you see the code below, you'll understand why I'm adding the exp() function to the returns. If you're a PhD in statistics, you already know. If you're not, well I'll just tell you. They're log returns, so this requires a switch-a-roo reversal exercise for them to become simple returns again. 

Here is the code. Any suggestions are welcome. My plans are to create an R package comprised of a bunch of these "quote" -- bugs, sometime soon.

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)
}

To leave a comment for the author, please follow the link and comment on his blog: Milk Trader.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags: , ,

Comments are closed.