# White Bumblebee Implemented in R

December 18, 2010
By

White Bumblebee is a trade system based on a simple moving average crossover, but with a special twist. Imagine your thermostat triggering your furnace to shut off or turn on every time a temperature crossed a threshold. If the thermostat didn’t have a buffer built in, you’d wear out your furnace with too many on/off cycles. I’m not an electrical engineer, but the basic concept behind this buffer is called hysteresis. In trading, it’s called a Bollinger band. And that’s the crux of White Bumblebee.

Before I show the code, let me explain the possible states. First is long. When the fast moving average crosses the upper Bollinger band of the slow moving average, that’s unambiguous long. Unambiguous short is the same in reverse. The two ambiguous states are when the fast moving average is between the slow moving average’s Bollinger bands. This is resolved as ‘same as yesterday’. Or, we remain in our position for now.

You can copy the code below into your R console and then type:

`   > nectar()`

You will get the last 12 days of GLD and whatever position White Bumblebee has over that span. But you can change up symbols (make sure they’re Yahoo Finance symbols), number of days that print or even the length of the fast and slow moving averages, and the size of the standard deviation. Let’s say you wanted the to get SPY positions over the last 20 days.

`    > nectar(ticker="SPY", tailsize=20)`

If you don’t specify a particular parameter, it simply defaults to the ones I’ve written in. If you’re fine with GLD but want to play with moving average length, type:

`    > nectar(fast=3, slow=9)`

Have fun, and now the code:

`nectar <- function(ticker="GLD", fast=10, slow=30, width=0.5, tailsize=12) {        require("quantmod")    x <-getSymbols(ticker, auto.assign=FALSE)    fastBB <- BBands(( x[,4] ), n=fast, sd=width)    slowBB <- BBands(( x[,4] ), n=slow, sd=width)    my_data <- merge(x, fastBB, slowBB)    my_data <- as.data.frame(my_data)        white_bumblebee <- function(my_data){    Position <- ifelse (my_data[,8] > my_data[,13], "long",                   ifelse(my_data[,8] < my_data[,11], "short",                     "same as yesterday"))      return(Position)    }        my_data\$Position <- white_bumblebee(my_data)    my_data <- my_data[,c(4,15)]    tail(my_data, n=tailsize)} `

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

Tags: , ,