# MT5 trade history to a spreadsheet with Python

Independent and unofficial. Not affiliated with or endorsed by any broker or platform named here. Educational only: nothing here is advice, and nothing here recommends a broker or a trade.

This is the markdown version of https://openbrokerapi.com/guides/mt5-trade-history-to-a-spreadsheet, written from the same records as that page.

Risk warning: CFDs and spread bets are complex instruments and come with a high risk of losing money rapidly due to leverage. You can lose money on these products, and each provider publishes its own loss figure under FCA rules. You should consider whether you understand how they work and whether you can afford to take the high risk of losing your money. Automated and API trading can amplify losses through bugs, connectivity failures and runaway orders.

Asked over and over on the MQL5 forum and answered in no single place. This is the whole job: the constraint that stops most people, the one call that does the work, and a script that writes a CSV.

- Last reviewed: 2026-09-20

Unverified in part: carried over from an earlier version of this site. The links below are the sources it was written from, but its statements have not been re-read at source since the date shown, so check anything you rely on.

## Remind me to do this again

One tap puts two reminders in your own calendar: every three months, and 6 April, the day after the UK tax year ends on 5 April. The file is built in this page and saved by your browser. There is no mailbox behind this site, no account to make and nothing to type, so nothing about you leaves this page.

## The steps

1. Install the packagepip install MetaTrader5 on a Windows machine with the terminal installed. The current release is 5.0.6231, published 27 Sep 2026.Step 1bashpip install MetaTrader5 Source[PyPI: MetaTrader5 release history](https://pypi.org/project/MetaTrader5/) checked 19 Sep 2026
2. Understand the one call that mattersmt5.history_deals_get(date_from, date_to) returns the filled trades between two dates as a tuple of named tuples, or None on error, in which case mt5.last_error() says why. It also accepts group="\*GBP\*" to filter by symbol, ticket= for a single order, or position= for every deal belonging to one position. Deals, not orders, are what a spreadsheet of your trading should be built from.Our sourced reference page for history_deals_getSource[MQL5 docs: history_deals_get (Python)](https://www.mql5.com/en/docs/python_metatrader5/mt5historydealsget_py) checked 18 Sep 2026
3. Build the dates in UTCMetaTrader 5 works in UTC. Build date_from and date_to as timezone-aware UTC datetimes, or a run from a machine set to British Summer Time will silently miss an hour at each end of the range.Source[MQL5 docs: history_deals_get (Python)](https://www.mql5.com/en/docs/python_metatrader5/mt5historydealsget_py) checked 18 Sep 2026
4. Write the CSVEach deal exposes _asdict(), so csv.DictWriter can take the field names straight from the first row. The sample below saves the last 30 days of deals to deals.csv, which any spreadsheet will open. It only reads: it never calls order_send.Source[MQL5 docs: history_deals_get (Python)](https://www.mql5.com/en/docs/python_metatrader5/mt5historydealsget_py) checked 18 Sep 2026
5. Check the totals before you rely on itThe column names come from the deal named tuple and MetaQuotes does not document this export as a tax record. Reconcile the totals against your broker's own statement before you use it for anything that matters.Source[MQL5 docs: history_deals_get (Python)](https://www.mql5.com/en/docs/python_metatrader5/mt5historydealsget_py) checked 18 Sep 2026
6. If the call returns Nonehistory_deals_get returns None rather than raising. Print mt5.last_error() and look the code up: the negative codes are IPC, version, login and timeout failures with a page of their own.[Every negative code mt5.last_error() returns](https://openbrokerapi.com/errors/mt5/internal-errors)Source[MQL5 docs: last_error (Python)](https://www.mql5.com/en/docs/python_metatrader5/mt5lasterror_py) checked 19 Sep 2026

## The script

Demo account only. It stops rather than run against a live account, it reads three values from the environment so no password is ever typed into the file, and it never calls order_send. Not yet run by us, so it carries no Tested badge.

```
# MetaTrader 5 Python: save your deals to CSV. DEMO ACCOUNT ONLY. Not yet run by us.
# Needs Windows with the MetaTrader 5 terminal installed, and: pip install MetaTrader5
# Set MT5_LOGIN, MT5_PASSWORD and MT5_SERVER as environment variables for a demo account.
import csv
import os
from datetime import datetime, timedelta, timezone
import MetaTrader5 as mt5

if not mt5.initialize(login=int(os.environ["MT5_LOGIN"]), password=os.environ["MT5_PASSWORD"], server=os.environ["MT5_SERVER"]):
    print("initialize() failed:", mt5.last_error())
    raise SystemExit(1)

try:
    acc = mt5.account_info()
    if acc is None or acc.trade_mode != mt5.ACCOUNT_TRADE_MODE_DEMO:
        raise SystemExit("Stopping: this sample runs on a demo account only.")

    # MetaTrader 5 times are UTC, so build the dates in UTC
    date_to = datetime.now(timezone.utc)
    date_from = date_to - timedelta(days=30)
    deals = mt5.history_deals_get(date_from, date_to)
    if deals is None:
        raise SystemExit(f"history_deals_get failed: {mt5.last_error()}")

    with open("deals.csv", "w", newline="") as f:
        writer = None
        for deal in deals:
            row = deal._asdict()
            if writer is None:
                writer = csv.DictWriter(f, fieldnames=list(row))
                writer.writeheader()
            writer.writerow(row)
    print(len(deals), "deals saved to deals.csv")
finally:
    mt5.shutdown()
```

Source: [MQL5 docs: history_deals_get (Python)](https://www.mql5.com/en/docs/python_metatrader5/mt5historydealsget_py) checked 18 Sep 2026

## If you would rather not write code

The desktop terminal saves the same history as an HTML or PDF report with no Python at all, and that route works on any machine the terminal runs on.

## Where to go next

- [Save the history from the terminal instead](https://openbrokerapi.com/guides/metatrader-trade-history)HTML or PDF report, no code, from MetaQuotes' help pages
- The MetaTrader 5 Python function referenceEvery function with its signature, return value and source
- [The MetaTrader 5 platform API page](https://openbrokerapi.com/platforms/metatrader-5)What the Python package and MQL5 can and cannot do
- [Get your data out of any broker](https://openbrokerapi.com/data)The same job for IG, OANDA, Capital.com, IBKR and eToro

## More on the MetaTrader 5 API

- [MetaTrader 5 API quickstart](https://openbrokerapi.com/platforms/metatrader-5/quickstart): From credentials to a first call on a demo account, step by step.
- [MetaTrader 5 API error codes](https://openbrokerapi.com/errors/mt5): 311 decoded, each with what it means, and the fix where the source gives one.
- [MetaTrader 5 functions](https://openbrokerapi.com/platforms/metatrader-5/functions): 78 documented, each with its signature, arguments, return value and source.
- [MetaTrader 5 API page](https://openbrokerapi.com/platforms/metatrader-5): The full record: access, authentication, limits and the source for each.
- [Claude with MetaTrader 5: a safe, read-first setup](https://openbrokerapi.com/guides/claude-mt5): Guide
- [MetaTrader 5 for Python: which platforms does it run on?](https://openbrokerapi.com/guides/metatrader-5-python-platforms): Guide

## Licence and how to cite

The records behind this page are all rights reserved. This file is the page itself, at its own address, in another format. It is not part of the CC BY 4.0 downloads listed at https://openbrokerapi.com/data. Quote from it with a link to the page.

Cite as: MT5 trade history to a spreadsheet with Python. Broker API Hub, https://openbrokerapi.com/guides/mt5-trade-history-to-a-spreadsheet, newest check 2026-09-20.

Where you rely on a single figure, cite the source and the checked date printed beside it as well. Where this page and a broker's own documentation differ, the documentation wins.
