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) {
require("quantmod")
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"))
return(Position)
}
my_data$Position <- white_bumblebee(my_data)
my_data <- my_data[,c(4,15)]
tail(my_data, n=tailsize)
}
thanks for this. nice to see traders using R.
ReplyDeleteany recommendations on a book where to find different algorithms. youre blog really has me interested now. i primarily trade biotechs based on countless hours of due diligence, but it would be interesting to explore how some of these work.
ReplyDelete@Patrick, I'm not aware of any trading algorithm recipe books. I'm near-term implementing the simple White Bumblebee in R, Ruby and C, and then I'll move on to a radial-basis function predictor algorithm.
ReplyDeleteI'm also working on a Rails project that I hope to use R with. So far, I'm playing around with the RSRuby gem and it looks like there is potential there.
Do you see any value in a machine learning model for your trading style? If you focus your trading on a sector, sometimes you get a feel for what information is most important. And then once you quantify it, you can build a model.
1- Well I am just looking for a general list of algorithms that are generally used and then the goal would be to implement them in R. I am not looking to trade at high frequencies but for my own source of buy/sell signals. Any pointers on where to look for more algorithms like the White Bull, Momentum strategies etc would be very helpful.
ReplyDelete2- I don't know much about Ruby, so not too sure about the potential. I think using Python is an interesting angle.
3- That's quite a good idea, however, I am not sure how much time I have to dedicate to a task like that. I also like modeling binary events will be quite difficult for a number of reasons. You have to be able to weight the relative importance of the news in a very sophisticated way, since sometimes even very positive news for companies leads to a major sell off. For me, I think fundamental analysis tends to be the most successful, since biotech require careful examination of many aspect of their business.
I thank you very much for this blog and look forward to more posts! Hopefully, you will continue using R which I am very fluent in.
@Patick,
ReplyDelete"Numerical Recipes in C" for how C does particular computations (can be called in R). If you go this route, checkout Rcpp R package.
AmiBroker, TradersStudio for off-the-shelf backtesting software packages. AmiBroker uses a C++ style domain specific language (DSL), while TradersStudio uses Visual Basic as its core DSL. There is a free trial period for AmiBroker in which you can probably get some basic trade a system algorithms.
Technical Analysis of Stocks and Commodities for articles about trade systems. These are usually implemented in vendor-specific code that is available in the back section of the magazine.
quantmod and TTR R package vignettes.
Keep us posted on your progress. I'll be implementing more trade systems in R after I get some basic SVM algorithms implemented. The machine learning algorithm can also be implemented in R, by the way.
As a side note, a machine learning algo will weigh your data by design and curve fits based on it's own determined data weighing.
Again, thank you for your commentary. The magazine idea seems good; I just don't know a good resource to find these, so that's a start.
ReplyDeleteSVM is fairly easy in R - unless you have some other classifiers in mind.
http://cran.r-project.org/web/packages/e1071/
http://cran.r-project.org/web/views/MachineLearning.html
http://cran.r-project.org/web/packages/e1071/vignettes/svmdoc.pdf