mt5.history_deals_get() in MetaTrader5 Python: read past deals and fills

Returns deals from the account's history as a tuple of named tuples, with price, volume, profit, commission and swap. By date range, order or position.

Signature

mt5.history_deals_get(date_from, date_to)
mt5.history_deals_get(date_from, date_to, group="...")
mt5.history_deals_get(ticket=...)
mt5.history_deals_get(position=...)

What it does

history_deals_get() is where realised profit lives. Each deal has fields such as ticket, order, time, type, entry, symbol, volume, price, commission, swap, profit, magic, comment and position_id.

entry says whether the deal opened a position, closed one, or reversed one.

Parameters

ParameterWhat it is
date_from, date_toThe range. Dates can be datetime objects or seconds since 1970. The terminal compares them with history times, which are on the broker's server clock, not true UTC.
groupOnly deals whose symbol matches a filter such as "*USD*". Optional, with the date form.
ticketOnly the deals of the order with this ticket. It is an order ticket, not a deal ticket.
positionOnly the deals of the position with this ticket.

What it returns

  • A tuple of deal records. An empty tuple means none matched.
  • None on failure. Check mt5.last_error().

Example

from datetime import datetime, timezone
import MetaTrader5 as mt5

if not mt5.initialize():
    raise SystemExit(mt5.last_error())

start = datetime(2026, 10, 1, tzinfo=timezone.utc)
end = datetime(2026, 10, 8, tzinfo=timezone.utc)
deals = mt5.history_deals_get(start, end)
if deals is None:
    raise SystemExit(mt5.last_error())

net = sum(d.profit + d.commission + d.swap for d in deals if d.symbol)
print(f"{len(deals)} deals, net trading result {net:.2f}")
mt5.shutdown()

Common mistakes

  • History times are the broker's server clock. A day drawn from UTC midnight to UTC midnight takes deals from the wrong trading day on most servers. Shift by the broker's offset first.
  • Balance deals, such as deposits, have an empty symbol. Leave them out when you add up trading results, as the example does.
  • After an unclear order_send() result, search the deals for your comment or magic before you send again.

The same with Tickwire on Linux

The package needs a Windows terminal on the same machine. Tickwire runs the terminal for you and gives you a REST API, so the code below runs on Linux, macOS or a server.

Tickwire has three history deal routes: by time range, by ticket and by position. On MT5 the ticket route reads one deal by its own ticket. Deals come as JSON with id, type, entryType, symbol, volume, price, profit, commission, swap and positionId. Balance and other non-trade deals are included.

Tickwire's time routes take ISO-8601 UTC times, start included and end left out, with offset and limit (1000 at most) to page. Tickwire turns broker times into UTC with the offset it measured for the server. If that offset is not known yet, the route answers 503 broker_time_unknown instead of guessing.

import os, requests

API = "https://api.tickwire.io"
h = {"auth-token": os.environ["TW_KEY"]}      # your Tickwire key
base = f"{API}/users/current/accounts/{os.environ['TW_ACCOUNT']}"

url = f"{base}/history-deals/time/2026-10-01T00:00:00Z/2026-10-08T00:00:00Z"
deals = requests.get(url, params={"limit": 1000}, headers=h).json()
net = sum(d.get("profit", 0) + d.get("commission", 0) + d.get("swap", 0)
          for d in deals if d.get("symbol"))
print(len(deals), "deals, net", round(net, 2))

Related error codes

All MetaTrader5 Python functions

MT5 from Python, on any machine

One REST API for MT4 and MT5, with idempotent orders and an audit log. Read the MT5 Python API page or the quickstart.