Backtesting trading strategies in r
You can also store data between specific dates. In the example below, I first retrieve the data stored above, then select the first price observations and finally draw the chart. Two things to notice: You can refer to the Examples section on QuantTools website. Overall I find the package extremely useful and well documented. The only missing bit is the live feed between R and IQFeed which will make the package a real end to end solution.
A few months ago a reader point me out this new way of connecting R and Excel. At the time of writing the current version of BERT is 1. Ultimately I have a single Excel file gathering all the necessary tasks to manage my portfolio: In the next sections I present the prerequisite to developed such an approach and a step by step guide that explains how BERT could be used for simply passing data from R to Excel with minimal VBA code.
Once the installation has completed you should have a new Add-Ins menu in Excel with the buttons as shown below. This is what we want to retrieve in Excel. Save this in a file called myRCode. R any other name is fine in a directory of your choice.
In this file paste the following code. Then save and close the file functions. Create and save a file called myFile.
This is a macro-enabled file that you save in the directory of your choice. Once the file is saved close it. Once the file is open, paste the below code.
You should see something like this. Paste the code below in the newly created module. You should see something like the below appearing. From my perspective the interest of such an approach is the ability to glue together R and Excel obviously but also to include via XML and batch pieces of code from Python, SQL and more.
This is exactly what I needed. Making the most of the out of sample data August 19, , 9: Then a comparison of the in and out of sample data help to decide whether the model is robust enough. This post aims at going a step further and provides a statistical method to decide whether the out of sample data is in line with what was created in sample. There is a non-parametric statistical test that does exactly this: Using the Kruskal-Wallis Test , we can decide whether the population distributions are identical without assuming them to follow the normal distribution.
It exists other tests of the same nature that could fit into that framework. Then I tested each in sample subset against the out of sample data and I recorded the p-values. This process creates not a single p-value for the Kruskall-Wallis test but a distribution making the analysis more robust. As usual what is presented in this post is a toy example that only scratches the surface of the problem and should be tailored to individual needs.
As usual with those things just a kind reminder: This is a very first version of the project so do not expect perfection but hopefully it will get better over time. Please report any comment, suggestion, bug etc… to: Doing quantitative research implies a lot of data crunching and one needs clean and reliable data to achieve this.
What is really needed is clean data that is easily accessible even without an internet connection. The most efficient way to do this for me has been to maintain a set of csv files. I have one csv file per instrument and each file is named after the instrument it contains. The reason I do so is twofold: Simple yet very efficient so far.
The process is summarized in the chart below. In everything that follows, I assume that data is coming from Yahoo. The code will have to be amended for data from Google, Quandl etc… In addition I present the process of updating daily price data.
The code below is placed on a. Note that I added an output file updateLog. The process above is extremely simple because it only describes how to update daily price data. When it comes to managing a portfolio of stocks versus a benchmark the problem is very different from defining an absolute return strategy.
I strongly encourage anyone with an interest in the topic to read the book from the beginning to the end. In this post I focus on two simple and widely used metrics: The IC gives an overview of the factor forecasting ability. Obviously ICs must be as high as possible in absolute terms.
The usefulness of this tool is straightforward. A factor can have a good IC but its predictive power might be limited to a small number of stocks. Obviously there is a survival ship bias: This measure is much less sensitive to outliers than arithmetic mean. This is very significant and powerful for such a simple factor not really a surprise though…. Therefore there are greater chances to beat the index by overweighting the stocks falling into Q5 and underweighting those falling into Q1 relative to the benchmark.
An IC of 0. Formal significance tests can be evaluated but this is beyond the scope of this article. In the hedge fund world people have very low tolerance for drawdown. Generally, as a new trader in a hedge fund, assuming that you come with no reputation, you have very little time to prove yourself. You should make money from day 1 and keep on doing so for a few months before you gain a bit of credibility. In the chart below I simulated the experiment with one of my strategies.
You start trading in 1st June and all goes well until 23rd Jul. If you have kept the allocation unchanged, the high water mark level would have been crossed on 28th Oct. Still on the chart above, assume you get really unlucky and you start trading toward mid-June You would have started in early August your allocation would not have been cut at all and you end up doing a good year in only 4 full months of trading.
Starting off with library imports, and setting the config variables. The instrument and SMA length will be easy to tweak:. The variable names are self-explanatory. The next step is to add the signals, which become TRUE when an event happens in an indicator. And finally, the rules to complete the strategy. It's even got a chapter dedicated to quantstrat. It seems like the entries are late to the trend, and the zig-zag periods will tax this.
It would do better when there's a sustained trend over a long period. Feel free to download the script and play around by changing the instrument name, testing time period and SMA lengths. Backtesting a simple trading strategy in R with quantstrat Posted on: Here are the rules: