mt5.history_deals_total() in MetaTrader5 Python: count deals in history

Returns how many deals, the actual fills plus balance operations, are in the account's history between two dates. Returns an integer.

Signature

mt5.history_deals_total(date_from, date_to)

What it does

A deal is one execution: an entry, an exit, or a balance change such as a deposit. history_deals_total() counts them in a date range.

Parameters

ParameterWhat it is
date_fromThe start of the 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.
date_toThe end of the range, in the same form.

What it returns

  • An integer: the number of deals in the range.

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, 11, 1, tzinfo=timezone.utc)
print(mt5.history_deals_total(start, end), "deals in October")
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.
  • The count includes deposits, withdrawals and other non-trade deals.

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's history deals time route returns the deals as a JSON array. Count them, paging with offset when there are more than limit.

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.

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.