Algorithm Whisperer

April 6, 2011
By

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

I was staring at my favorite trading algorithm the other day and I could swear it wanted to tell me something. I've made contact with it in the past, but our conversations pretty much ran along the lines of "What position are you in the market?" to which it would reply "long", "short" or "flat". And that was pretty much it. I would nod and it would just sit there. Like it wanted me to tell me more but was bound by secret oath to not say anything unless it was properly asked. It had a pale look on its face though, like it was holding out on me and didn't like it.

I'm getting around to accepting that I have to lead this conversation. We've spoken in the past in several languages, but R has always been the best algorithm-to-human interface.  I started out by picking the market.

"So White Bumblebee, do you want to talk about gold or silver?"

No reply. I felt a little cheap with that taunt so I started again.

"White Bumblebee, let's talk about the gold market, and let's use the GLD gold etf as a point of reference for our little chat."

Still no reply, but I could see an almost imperceptible smile.

I didn't really say "... let's talk about the gold market ...". I said the following:

require("quantmod")   
                                                          
getSymbols("GLD")    
                                      
GLD$fast    <- BBands(( GLD[,4] ), n=10, sd=0.5)                                        
GLD$slow    <- BBands(( GLD[,4] ), n=30, sd=0.5)     
GLD         <- na.locf(GLD, na.rm=TRUE)                                  
                                                                         
position     <- ifelse (GLD$mavg > GLD$up.1, 1,                            
                ifelse(GLD$mavg < GLD$dn.1, -1, NA))
           
position    <- na.locf(position, na.rm=TRUE)
  
Here, we continue our conversation. And start building a meaningful relationship.

Me: White Bumblebee, how often have you been long versus short the yellow metal in the last few years?

hist(position, 
     main="How often have you been long
           vs short in the last few years?",
     col="goldenrod")
White Bumblebee:



Me: What does your daily distribution of returns look like?:

returns     <- ROC(Cl(GLD))*position
hist(returns, main="What do your daily returns look like?", col="darkslategray2")
White Bumblebee:

Me: Ahem, without look-ahead bias, please?

returns     <- Lag(ROC(Cl(GLD)))*position
hist(returns, main="Ahem, without Look-Ahead Bias?", col="darkslategray")
White Bumblebee:



Me: Got anything more intuitive?:

require(vioplot)
returns <- na.locf(returns, na.rm=TRUE)
vioplot(returns, names="Got anything more intuitive?", col="blue") 
White Bumblebee:



Me: Where do you see gold closing tomorrow?:

paste("What is the closing price for gold tomorrow?")
trick_question <- Cl(GLD[NROW(GLD)+1, ])
trick_question
White Bumblebee:

Error in `[.xts`(GLD, NROW(GLD) + 1, ) : subscript out of bounds

That last one was a trick question. I couldn't resist. Good thing for me that White Bumblebee knows how to take a joke.

To leave a comment for the author, please follow the link and comment on his blog: Milk Trader.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: 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.