Using R in Algorithmic Trading: Back-testing a machine learning strategy that retrains every day

On my last post we went into the world of machine learning with the building of a simple machine learning model using a support vector machine to attempt to predict the daily returns of the UUP in R. However failing to or being able to predict the return of a series is no indicative of actual trading success or failure since trading returns are clearly not homogeneous (not all bad days are the same and not all good days are the same). This means that you can have a machine learning technique that has quite low accuracy (below random chance) but it can still be successful if it tends to predict positively the days that have the highest returns. When using machine learning strategies for trading it is not only a matter of what percentage of the time you’re right/wrong but how much money you make or lose when you trade. Due to this fact it becomes very important to test our models within an actual back-test, so that we can actually find out whether our machine learning algorithmic works, in practice, when trading (at least from a historical perspective). On today’s tutorial we’re going to learn exactly how to do that.

To do the above we are going to be needing the following R packages: e1071, quantmod and PerformanceAnalytics. Note that the packages need to be loaded in the exact order above, because we’re going to be using some functions that are defined on e1071 and PerformanceAnalytics but we want the PerformanceAnalytics functions (so you’ll get errors if you load PerformanceAnalytics first and the links to the function names get overwritten internally). Today we’re going to run a back-test on a gold ETF (GLD), attempting to predict it using an SVM. Our SVM model will retrain on every bar using the past X bars. First of all, we load the libraries and we then obtain the data for the GLD ETF using the quantmod getSymbols function. Once this is done we will be using a simple function created by this guy that allows us to create a data frame containing all the predictors in a very organized manner (I fully recommend the post linked before, which shows you how to create/test a classification model using an SVM on the SPY). I also changed this function to also return the plain return of the series so that I can assign it to another array and later use it to get the profit/loss for the strategy. Define the function first in R as detailed below:

Note that the function defines 17 different predictors that we use as inputs to predict a binary classifier (1 bullish, -1 bearish) which we will then use to train our SVM. Also notice how the function takes advantage of the ROC function and other nifty vector based functions that are an order of magnitude more efficient than the loop I shared with you on my last post. Once the function is defined we can then simply call it to populate a data frame called “data” . We are then going to create a data frame called “daily” and assign it the daily returns, after which we will be deleting them from the main data array (because having the return in the predictor array would lead to snooping).

After this we can now run the back-test for our system. What we will do first is choose a learning period (the algorithm will be trained with the past learningPeriod number of bars) and we then create an empty data frame called “results” where we will be saving the daily returns of our trading strategy. In the case below I have chosen a learning period of 200. After this we loop through all the points of our data frame from lerningPeriod+1 to the length of the array and on each point we’re going to create a training subset using a learningPeriod number of bars prior to the current data point and we are then going to use it to train our SVM. After this we’re then going find the prediction of the newly built SVM model for the next return and we’re going to add a positive or negative return to our results array depending on whether our result matches or mismatches the actual real output class. Note that in any case (whether we’re right or wrong) I have subtracted 0.0001 from the return, which is the commission burden I have chosen to put on all trades (0.01% of trade volume).  I then use the chart functionality of the performance analytics library to display a graph of trading progress every 200 bars.

After this is finished we will have a graph showing the results of the system in a format given by the PerformanceAnalytics package. Since you have an array of returns in xts format you will also be able to invoke any of the functions within this package to further analyze the statistics of your trading system. The image below shows you the return we have obtained for this strategy on the GLD ETF, we can see that the machine learning technique used was not able to properly predict directionality in a large variety of cases. This shows, as we also saw on our previous post, that achieving profitable returns using machine learning procedures is not very easy (even when using as many inputs as we have used here).



However by choosing inputs a bit better, doing some data pre-processing and modifying the gamma and c characteristics of the SVM we can indeed obtain some profitable results for the daily retrained SVM on GLD as showed below(note that the y axis is logarithmic). By using ensemble techniques, including several different machine learning algorithms and choosing even better inputs we can improve the results below even further, however we’ll leave this discussion for future posts.


If you would like to learn more about machine learning techniques and how you too can create your own systems that re-train on every bar and give historically profitable results please consider joining, a website filled with educational videos, trading systems, development and a sound, honest and transparent approach towards automated trading in general . I hope you enjoyed this article ! :o)

Print Friendly, PDF & Email
You can leave a response, or trackback from your own site.

4 Responses to “Using R in Algorithmic Trading: Back-testing a machine learning strategy that retrains every day”

  1. fab says:

    i am a bit puzzled. From your previous posts you seemed to be few steps further than this post. What happened to your own framework for machine learning strategy creation and how are you guys with the GPU approach on the lower time frames. I recall you concluded that there is more potential in the intra day strategies (which much lower drawdowns). I would be curious to see an update there :-) So to me your post on R strategy building on the DTF seems like a result from some drawbacks on your former track? I cross fingers that is not the case..

    Greetings and still thanks for this “howto”


    • admin says:

      Hi Fab,

      Thanks for your post :o) Don’t worry, we continue to research machine learning on the lower time frames and the creation of systems using data-mining and GPUs. We’re making some good advances in both of these fields and our cloud data-mining is already generating many promising systems (many of them already under live testing). These articles are meant as tutorials for those who don’t have access to our tools but wish to carry out some machine learning experiments using R with freely available data and software. Please don’t take all article I post as the “state of the art” of what we are doing, many are meant as educational tools for those who want to start their journey into our research paths. Thanks again for posting,

      Best Regards,


Leave a Reply

WordPress › Error

The site is experiencing technical difficulties.