History¶
Historical orders and deals retrieval.
Models¶
Deal¶
Deal
¶
Bases: BaseModel
A completed transaction in the MT5 trade history.
Wraps the raw TradeDeal struct returned by mt5.history_deals_get().
A deal is created whenever a position is opened, modified, or closed.
Non-trade operations (balance adjustments, commissions, swaps) also
appear as deals with non-BUY/SELL type values.
| ATTRIBUTE | DESCRIPTION |
|---|---|
ticket |
Unique deal identifier.
TYPE:
|
order |
Ticket of the order that generated this deal.
TYPE:
|
time |
Deal execution time as a Unix timestamp (seconds).
TYPE:
|
time_msc |
Deal execution time in milliseconds.
TYPE:
|
type |
Nature of the transaction (BUY, SELL, BALANCE, COMMISSION, etc.).
TYPE:
|
entry |
How this deal relates to the affected position (IN, OUT, INOUT, OUT_BY).
TYPE:
|
magic |
Expert Advisor identifier; 0 for manual trades.
TYPE:
|
position_id |
Ticket of the position this deal belongs to.
TYPE:
|
reason |
What triggered the deal.
TYPE:
|
volume |
Executed volume in lots.
TYPE:
|
price |
Execution price.
TYPE:
|
commission |
Commission charged for this deal.
TYPE:
|
swap |
Swap charged at the time of this deal.
TYPE:
|
profit |
Realised profit/loss in account currency.
TYPE:
|
fee |
Additional fee charged for this deal.
TYPE:
|
symbol |
Trading instrument name.
TYPE:
|
comment |
Arbitrary comment attached to the deal.
TYPE:
|
external_id |
Deal identifier in an external system.
TYPE:
|
net_profit
property
¶
Realised profit after deducting commission, swap, and fees.
| RETURNS | DESCRIPTION |
|---|---|
float
|
Net profit in account currency. |
is_reversal
property
¶
Whether this deal reversed the direction of a position (DEAL_ENTRY_INOUT).
is_close_by
property
¶
Whether this deal closed a position by an opposite position (DEAL_ENTRY_OUT_BY).
Service¶
HistoryService¶
HistoryService
¶
Retrieves historical orders and deals from the MT5 trade history.
Wraps mt5.history_orders_total(), mt5.history_orders_get(),
mt5.history_deals_total(), and mt5.history_deals_get() via
call_mt5, parses raw structs into HistoricalOrder and Deal
models, and returns Result[T].
Usage¶
from datetime import datetime, timezone
date_from = datetime(2024, 1, 1, tzinfo=timezone.utc)
date_to = datetime(2024, 1, 31, tzinfo=timezone.utc)
# Get historical orders
res = mt5.history_orders_get(date_from, date_to)
# Get historical deals
res = mt5.history_deals_get(date_from, date_to)
# Filter by position
res = mt5.history_deals_get(position=123456)