White Bumblebee Implemented in R

December 18, 2010

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

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) {
    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")) 
    my_data$Position <- white_bumblebee(my_data)
    my_data <- my_data[,c(4,15)]
    tail(my_data, n=tailsize)

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 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...

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.


Mango solutions

RStudio homepage

Zero Inflated Models and Generalized Linear Mixed Models with R

Quantide: statistical consulting and training



CRC R books series

Contact us if you wish to help support R-bloggers, and place your banner here.

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)