mt5.history_orders_get() in MetaTrader5 Python: read past orders
Returns orders from the account's history as a tuple of named tuples: by date range, with an optional symbol filter, or by order or position ticket.
Signature
mt5.history_orders_get(date_from, date_to)
mt5.history_orders_get(date_from, date_to, group="...")
mt5.history_orders_get(ticket=...)
mt5.history_orders_get(position=...) What it does
history_orders_get() reads orders that are done: filled, canceled, rejected or expired. Each one has fields such as ticket, symbol, type, state, volume_initial, price_open, time_setup, time_done and position_id.
Use the date form for a report. Use ticket to look up one order, or position for every order that touched one position.
Parameters
| Parameter | What it is |
|---|---|
date_from, date_to | 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. |
group | Only orders whose symbol matches a filter such as "*USD*". Optional, with the date form. |
ticket | Only the order with this ticket. |
position | Only the orders of the position with this ticket. |
What it returns
- A tuple of order records. An empty tuple means none matched.
Noneon failure. Checkmt5.last_error().
Example
import MetaTrader5 as mt5
if not mt5.initialize():
raise SystemExit(mt5.last_error())
orders = mt5.history_orders_get(position=81234567)
if orders is None:
raise SystemExit(mt5.last_error())
for o in orders:
print(o.ticket, o.type, o.state, o.volume_initial, o.price_open)
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 order is not a fill. For prices, profit and commission, read the deals with
history_deals_get().
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 order routes: by time range, by order ticket and by position. Each returns a JSON array with id, symbol, type, state, volume, openPrice and positionId.
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.
GET /users/current/accounts/{account_id}/history-orders/time/{start_time}/{end_time}GET /users/current/accounts/{account_id}/history-orders/ticket/{ticket}GET /users/current/accounts/{account_id}/history-orders/position/{position_id}
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']}"
for o in requests.get(f"{base}/history-orders/position/81234567", headers=h).json():
print(o["id"], o["type"], o["state"], o["volume"]) Related functions
- mt5.history_orders_total() in MetaTrader5 Python: count orders in history
- mt5.history_deals_get() in MetaTrader5 Python: read past deals and fills
- mt5.orders_get() in MetaTrader5 Python: read 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.