Milktrader

Iterating Until Convergence

Saturday, December 11, 2010

White Bull, An Algorithm in R

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. 

8 comments:

  1. I don't know R. What is this formula saying in English? If a stock's close is above its 50 and 200 SMA, then bull market? Is there more? Thanks.

    ReplyDelete
  2. @Chris, yes. That was part of the point of the post, to start thinking algorithmically. Also, now that you've cracked the code, you can add other definitions because the heavy lifting is done with hidden functions. And it also brings up another point about using algorithms to trade. What do you do with this very broad information?

    ReplyDelete
  3. Totally awesome. It took me a bit of finagling to get R working properly on my ole Mac, but I did it. More! We want more!

    ReplyDelete
  4. @sarre9 Thanks. I'm working on White Bumblebee right now and the partial implementation is available on my github account at RMilk/hackscratch

    ReplyDelete
  5. Moved white_bull, yellow_bull and white_bumblebee to https://github.com/milktrader/RMilk/tree/master/algo

    ReplyDelete
  6. Another very good blog on R and trading

    http://www.aphysicistinwallstreet.com/

    ReplyDelete
  7. He's also on twitter, and I agree.

    ReplyDelete