Powerful statistical analysis: Take your MT4 backtesting results to R

Although I stopped using Metatrader 4 for my personal trading and business almost two years ago it is still true that most people who start in Forex automated trading continue to use this trading platform. One of the most important limitations that you encounter when using MT4 for back-testing – at least one that I found most frustrating – was the lack of flexibility in the information that you can obtain and the difficulty that you have in manipulating the data that comes out of the program after you run a backtest. On today’s post I am going to show you how you can overcome this limitation so that you can take your MT4 backtesting reports, turn them into properly formatted csv data and load them into R in order to better analyse your trading system and get a much clearer picture of how your system performs. You can download this zip file containing the script and a sample back-testing report to process.

The Metatrader platform does not offer you any processing-friendly formatting after you run a strategy backtest. You obtain an htm based report that contains all your system’s signals and positions but you have no easy route to take this information and load it in a program like R. The python script presented above allows you to take these MT4 results and process them to generate a csv file that contains balance change information that can be loaded directly into R. The script requires installation of the bs4 python library and works with python 2.7. In order to use this script simply change the “test.htm” with the path to the htm from the MT4 backtesting report you want to process. After this you will see a file called “test.csv” (you can change that name too) created within the script’s folder.

After we have created the csv file with the balance changes we can now load that data into R to get some cool statistical analysis and graphs for the strategy. The code below shows you how you can load the above generated csv file into R. The code needs you to have both the quantmod and PerformanceAnalytics libraries installed within your R setup. What the code is doing is simply to load the data into a zoo object which is then turned into an xts, we then obtain monthly returns using the monthlyReturn function (it can be daily or weekly if you wish as well) to obtain the two main performance graphs we want to see. Note that the rollingPerformance graph will not work properly on a frequency lower than monthly.

test1

test2

The above two plots give us a lot of additional information compared to the simple MT4 back-testing report. We can see the rolling window evolution of Sharpe ratio, standard deviation and past 12 month returns as well as the drawdown periods and monthly returns of our trading strategy. The depth, duration and general presence of drawdown periods become quite evident while they are very difficult to see on pure return graphs, especially if you have a compounded system (like the above) where the lack of a logarithmic y axis in MT4 makes the interpretation of results much harder. Additionally you can also use functions like table.CalendarReturns (only works correctly over monthly returns) which shows us the average, yearly and monthly returns for the strategy for the entire back-testing period (see an example below).

Selection_370

However the most important thing that you gain by doing this is a complete freedom in the statistics you can calculate and how you can analyse the results of your trading strategies. Having your data in a format that is readily accessible by a significant number of R libraries and having the entire power of the PerformanceAnalytics library at your disposal means that you won’t be constrained by the very limited data that the MT4 platform chooses to give you. Furthermore you also avoid depending on any third party software and can get any additional statistics you wish out of your results. If you would like to learn more about my work in automated trading and how we develop, analyze and trade strategies in and outside of MT4  please consider joining Asirikuy.com, a website filled with educational videos, trading systems, development and a sound, honest and transparent approach towards automated trading.

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

Leave a Reply


Fatal error: Call to undefined function mcsp_html() in /home1/danielfp/public_html/mechanicalforex/wp-content/themes/modern-style/comments.php on line 71