Australian (ASX) Stock Market Forum

MT4 HTM2CSV Statement converter using python

Joined
12 February 2014
Posts
138
Reactions
25
Thought I'd write a python script to convert a MT4 htm statement containing your closed trade history to a nicely formatted csv file. Note: you need to install python for this to work.

Attached is a zip file containing python script mt4htm2csv.py. Extract to where you store your Statement.htm file (generated from MT4 terminal.exe).

To generate the Statement.htm file, in MT4 you need to navigate to the Account history tab in your terminal pane, right click and select Save as Report. This will bring up the Save As dialog box and you just need to make sure it says Statement, then click save. Note: you can also select a custom history period beforehand.

Then execute my python script (command prompt:> python mt4htm2csv.py) in the directory location the Statement.htm file is stored and a corresponding Statement.csv file will be generated containing all the extracted closed trade history. You then open Statement.csv in a spreadsheet package such as Excel or Open/Libre Office and the following columns will be displayed:

Expert id, Ticket #, Open Time, Type, Lot Size, Symbol, Open Price, S/L, T/P, Close Time, Close Price, Raw Profit, Expenses, Profit

Expert id is just the magic number. This will be empty if you placed the trade manually.
The Expenses column is just the sum of Taxes, Commission and Swap. Everything else should be self explanatory.

In MT4 you can also save your trade history to a .xls file, but I think it's quite sloppy and csv files can be used for so many different things. Also my generated csv file is nicely formatted so that in excel you can easily sort by Expert id, Symbol and Close Time. One very cool and useful thing to do is create a cumulative profit column for say data sorted by Expert id/Symbol then Close Time (oldest to newest). This allows you to easily create charts where you can see the drawdown and profit for each group of related data instead of just looking at the data as a whole.

The other method I could of used to create a nicely formatted csv file is by mt4 script which iterates through the order history, but you can't beat a few lines of python code right?

Let me know if you come across any errors in the script, suggested enhancements or even what you think.

Cheers,

Andrew.
 

Attachments

  • mt4htm2csv20181123.zip
    1 KB · Views: 26
Top