Turn MT5 history into a report.
Read real deals. Separate funding. Export the result.
Download the Python example · ZIP ↓
Step-by-step transcript
- 0:00
Turn MT5 history into a report.
This tutorial shows how to read MT5 trade history, calculate booked trading results, and export a clear report with Python.
- 0:11
Connect for read-only reporting.
Open M T five dot M R P C dot pro and choose ConnectEx. Enter your demo login, password and broker server. Use TRIAL or your API key. Leave the optional id blank. We use an investor password for read-only access. The console keeps the returned terminal id.
- 0:33
Check the account currency.
Run AccountSummary. This is a demo account, and its currency is U S dollars. Its balance is ninety-nine thousand, nine hundred ninety-nine dollars and ninety-six cents. Use the account currency when presenting profit and costs.
- 0:51
Choose one completed period.
Choose DealsHistory. Set input From U T C and input To U T C, using timestamps ending in Z. Leave the corresponding broker-time fields blank. This recording covers October first through the start of October twenty-eighth. Fix that range before requesting more pages.
- 1:12
Read every page.
After HTTP Status Code 200, check error first. Our response has no API error. With page size two, the first response contains only two of thirteen deals. Copy nextPageToken into pageToken and keep the same filters. Continue until nextPageToken is empty.
- 1:35
Orders, deals and positions.
An order is a trading instruction; a deal records an execution. AllHistoryOrders shows the order records. In DealsHistory, order links to that order ticket, while positionId groups related executions. Here one position has a zero point zero two lot entry and two zero point zero one lot exits.
- 1:58
Use the actual signed amounts.
For each deal, add profit, commission, swap and fee exactly as returned. Keep their signs; do not subtract an already negative commission again. This closing deal made two cents, with zero costs. Include entry deals too, because a broker can charge commission when opening.
- 2:19
Keep funding separate.
The first record is a one hundred thousand dollar demo deposit. Its type is BALANCE, with no order or position. It is not trading profit. Our report separates Buy and Sell deals from other types. Review standalone charges, credits and adjustments in the other-deals file.
- 2:39
Check the report scope.
All thirteen records are present. The twelve trading deals total minus four cents. Commission, swap and fee are zero in this example. The deposit is separate. These are booked amounts within the chosen dates, not unrealized profit. A position opened before the range may have earlier costs outside this report.
- 3:02
Export the same data in Python.
The companion script calls the same DealsHistory endpoint, follows cursor pages and checks the final count. It uses decimal arithmetic and groups amounts by position id. Set your API key and terminal id privately in environment variables. The script never opens or closes a trade.
- 3:25
Open the three report files.
Run the script with your fixed U T C dates. It writes trade-deals C S V, other-deals C S V, and summary dot J S O N. The sample export was checked against this captured history. If an API error occurs or pages are incomplete, the script stops instead of presenting an incomplete total.
- 3:48
Disconnect when finished.
Finally, call Disconnect in the console to release your terminal. Our session is now disconnected. You have retrieved the history, separated funding from trading results, and exported a report you can inspect and reuse.