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.




Just found this blog-very interesting!
ReplyDeleteAre you using the ttr package in R for backtesting?
Do you have any example R code you can share of implementing a basic trading system and backtesting it? I'm familiar enough with R but find the bookkeeping of backtesting hard to do.
Do you have a contact email? I've looked at stocktwits and here and can't find one. Hence these questions posted in a rather random place.
Thanks!
@jf I do use TTR for the indicators above. It's not obvious because I called the quantmod package only. Once you do that, you get xts and TTR loaded gratis. (It saves typing though does not add to the code clarity). If you'd like to see an example backtest, I'd recommend the TTR package author's blog site: http://blog.fosstrading.com/.
ReplyDeleteMy email is my twitter name at gmail.
The trade system logic is first implemented in my position variable. It is also called signal in other implementations. You can call it anything you like. Let's say you have a system that you've dubbed the TX59. You can call your signal variable by that name. To the right side of the variable assignment is a control flow logic statement. The best for proper R best practices is to use a vectorized approach and avoid explicit loops. In my example, I use the ifelse() vectorized function. Mine is nested because there are three possible positions. If you're trading algorithm is either long or short, you don't need the nesting. The logic is if (condition) then (first options) else (second option). When you nest, the first option is a separate ifelse control flow.
Also, there is a trick in the code that allows my third option to replace "flat", which would be a zero, to NA. I then use a zoo function to convert the NA to what the system logic prefers. If you look up the help file for na.locf you will see what it does. (Shhh, don't tell anyone though).