mt5.history_orders_total() in MetaTrader5 Python: count orders in history
Returns how many orders in the account's history fall between two dates: filled, canceled and expired orders. Returns an integer.
Signature
mt5.history_orders_total(date_from, date_to) What it does
history_orders_total() counts orders that are no longer pending in a date range. Use it to see how much history_orders_get() will return.
Parameters
| Parameter | What it is |
|---|---|
date_from | The 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_to | The end of the range, in the same form. |
What it returns
- An integer: the number of history orders in the range.
Example
from datetime import datetime, timedelta, timezone
import MetaTrader5 as mt5
if not mt5.initialize():
raise SystemExit(mt5.last_error())
end = datetime.now(timezone.utc) + timedelta(hours=3) # cover the broker's clock
start = end - timedelta(days=7)
print(mt5.history_orders_total(start, end), "orders in the last week")
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.
- An end date of "now" in UTC can miss the last hours of trades, because the broker's clock is usually ahead. Pad the end date.
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 orders time route returns the orders 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.
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-orders/time/2026-10-01T00:00:00Z/2026-10-08T00:00:00Z"
print(len(requests.get(url, params={"limit": 1000}, headers=h).json()), "orders") Related functions
- mt5.history_orders_get() in MetaTrader5 Python: read past orders
- mt5.history_deals_total() in MetaTrader5 Python: count deals in history
- mt5.orders_total() in MetaTrader5 Python: count pending orders
Related error codes
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.