Using qqpat: Analyzing your MT4 back-testing results using Python

The open source qq-pat library – released last year – is a python library that allows you to easily analyze financial time series in order to obtain detailed statistics that are difficult to obtain using most retail trading platforms. Furthermore qq-pat allows you to manipulate these results as you see fit to obtain either more elaborate statistics or carry out actions in python related to a certain strategy’s end result. Today I want to share with you a script that allows you to use the qq-pat library in order to analyze your MT4 back-testing results. Using this script you can obtain important additional statistics that are not included in the MT4 strategy report and that may help you diagnose your MT4 trading strategies.

# Creado por Dr.Daniel Fernandez Ph.D.
# https://asirikuy.com       2016
# http://mechanicalforex.com 2016
 
import csv
from bs4 import BeautifulSoup
import qqpat
import pandas as pd
import datetime
import argparse
import sys
 
def main():
 
    parser = argparse.ArgumentParser()
    parser.add_argument('-f', '--filename')
    args = parser.parse_args()
    
    if args.filename == None:
        print "Please specify a file with -f."
        sys.exit()
    
    with open (args.filename, "r") as myfile:
        s=myfile.read()
        
    soup = BeautifulSoup(s, 'html.parser')
    table = soup.find_all('table')[1]
    
    # variable to check length of rows
    x = (len(table.findAll('tr')) - 1)
    
    times = []
    balances = []
    
    # set to run through x
    for row in table.findAll('tr')[1:x]:
        col = row.findAll('td') 
        try:
            balances.append(float(col[9].getText()))
            times.append(datetime.datetime.strptime(col[1].getText(), '%Y.%m.%d %H:%M'))
        except:
            continue
            
    time_series = pd.DataFrame(data=balances, index=pd.to_datetime(times)).pct_change(fill_method='pad').fillna(0)
    analyzer = qqpat.Analizer(serie_de_tiempo, column_type='return', titles=["system"])
    
    print analizer.get_statistics_summary()
    analizer.plot_mc_limits(index=0, iterations=4000, confidence=99)
    
    analizer.plot_analysis_returns()
    analizer.plot_analysis_rolling()
    analizer.plot_monthly_returns_heatmap()
    analizer.plot_annual_returns()
    analizer.plot_monthly_returns()
    analizer.plot_annual_returns()
    analizer.plot_monthly_return_distribution()
    analizer.plot_drawdown_distribution()
    analizer.plot_drawdown_length_distribution()
    analizer.plot_drawdown_periods()           
             
##################################
###           MAIN           ####
##################################

if __name__ == "__main__": main()

The back-testing analysis provided by the MT4 trading platform is very limited and additionally difficult to parse. You cannot control which statistics are calculated – or how they are calculated – and you cannot easily use these resulting statistics to carry out any additional actions (for example do things like sorting, graphing, classifying, etc). To solve these problems and obtain a much more rich set of statistics with the additional freedom to manipulate the nature of the calculations and what is done with the end results you can use the open source qq-pat library which can be downloaded and installed for free.

If you are using Windows you should first install python (2.7.x) , then add the python and python/scripts folders to your system PATH and then download and install the WHL binaries from the libraries required by qqpat (beautifulsoup, numpy, seaborn, matplotlib, pandas, scipy, cvxopt and cvxpy) from here. After downloading the WHL files you can install them by opening up a command prompt, going to the folder where you downloaded them and then using the “pip install whlfilename.whl” command. After you install all the dependencies you can then proceed to install qq-pat by doing “pip install qqpat”. If you try this command before installing the dependencies pip will automatically try to build and install the dependencies by itself but this is rarely a straightforward process on windows. If you are using linux then you have a much greater chance of a fully automatic installation just working properly.

imagen1

Once you have qqpat and all its dependencies installed you can then use the above script to analyse the resulting htm file that came out from saving your MT4 back-test detailed results. To do this just save the above script to a file (call it for example “analysis_script.py”) then simply enter the command “analysis_script.py -f mt4result.htm” where mt4result is the name of your MT4 back-testing output htm file. After this the script will perform some basic analysis for your trading strategy which includes the calculation of all summary statistics, monte Carlo worst case thresholds at 99% confidence, return analysis plots, rolling return plots, monthly return heatmaps, annual returns, monthly returns, drawdown distribution, monthly return distribution and drawdown period lengths and depths.

It is also worth noting that these are not all the functions available from qqpat but just an example of what the library can do. If you use the “pydoc -w qqpat” command the library will create an htm document containing all available functions plus a brief comment explaining what each function does. You can then use any of these functions and modify the script to come up with whatever set of statistics you wish to produce. Additionally the script can be further modified to just do a simple task and let you know the result. For example you could delete all the other functions and just use the get_sharpe_ratio function to just print the Sharpe ratio of the strategy being analyzed. You could also modify the script to analyse a batch of files or just save graphs to files, the sky’s the limit.

imagen2

Python plus qqpat provides you with a very powerful way to analyse your MT4 back-testing results. Coupled with the use of a library like beautifulsoup to carry out htm file parsing it provides a very convenient way to easily analyze results that come from this trading platform without having to go through a ton of processing. With your results now in python and analyzed by qqpat nothing stops you from creating some really cool stuff, such as a mysql repository containing all your back-testing statistics. Of course if you would like to learn more about trading system analysis and how you too can completely move away from the MT4 platform into much more flexible testing and trading methods please consider joining Asirikuy.com, a website filled with educational videos, trading systems, development and a sound, honest and transparent approach towards automated trading.strategies

You can skip to the end and leave a response. Pinging is currently not allowed.

Leave a Reply

Subscribe to RSS Feed Follow me on Twitter!
Show Buttons
Hide Buttons