Bug Collector

[This article was first published on Milk Trader, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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 their blog: Milk Trader.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)