# White Bull, An Algorithm in R

December 11, 2010
By

Algorithms are curious creatures. They behave in a very predictable way. They do as they are told and do it the same way every time. What they lack in imagination, they make up in reliability. You cannot talk an algorithm into saying something it's not programmed to say, and it will not offer you two sides to every story. No, these critters are pretty cut-and-dry.

You may choose to use them as you would a mule to haul lumber around the farm. Or as hens who lay eggs. Or, you might be more interested in having them as pets. Something to enjoy, but not something you're cooking for dinner.

The following code is written in R. It will tell you one of two things. Either "I am a bull market" or "I am NOT a bull market". All you need to do is supply it a Yahoo-specific ticker (it gets its data from Yahoo Finance, so you'll also need an internet connection). Assuming you have R installed on your computer, open the console, copy and past this function and then you are off to algorithm playland.
white_bull <- function(ticker) {        require("quantmod")        x <- getSymbols(ticker, auto.assign=FALSE)         c             <- x[,4]                                                           c50           <- SMA(c, n=50)                                               c200          <- SMA(c, n=200)                                                                                                                                                                                                last_c        <- tail(c, n=1)                                                                                                                          last_c50      <- tail(c50, n=1)                                                                                                                   last_c200     <- tail(c200, n=1)                                                                                                                                                                 if (last_c > last_c50 && last_c > last_c200 && last_c50 > last_c200)                                                                                              { print("I am a bull market") }                                                                                                                     else                                                                                                                                                            {  print("I am NOT a bull market") }                                                                                                                                                                                                    }
After you load this algorithm into your R console, you simply call it with your favorite tickers. within parens and quotes, like this: bull("myStock"). You may notice that this code requires the "quantmod" R package, so make sure you have that installed. (it's install.packages("quantmod") in case you forgot). Below are some examples.
> white_bull("GLD")[1] "I am a bull market"> white_bull("SLV")[1] "I am a bull market"> white_bull("TLT")[1] "I am NOT a bull market"> white_bull("C")[1] "I am a bull market"> white_bull("SPY")[1] "I am a bull market"> white_bull("^VIX")[1] "I am NOT a bull market"

Where you see the '>' symbol is where your prompt starts. From there, you type white_bull("mystock") and press return. The answer that follows the [1] is the white bull algorithm output. Notice that instead of VIX, you need to type ^VIX, because that's how Yahoo Finance references our beloved volatility index.

This algorithm is open-source, so feel free to distribute it. It was built with a little help from some of my friends at stackoverflow.com, and by the creators of the quantmod (Jeff Ryan) and TTR (Joshua Ulrich) R packages.