Orders¶
Order placement, validation, and management.
Models¶
Order¶
Order
¶
Bases: BaseModel
An active (pending) order in MetaTrader 5.
Wraps the raw TradeOrder struct returned by mt5.orders_get().
Only orders that have not yet been executed, cancelled, or expired
appear here — completed orders are in HistoricalOrder.
| ATTRIBUTE | DESCRIPTION |
|---|---|
ticket |
Unique order identifier.
TYPE:
|
time_setup |
Time the order was placed (Unix seconds).
TYPE:
|
time_setup_msc |
Time the order was placed (milliseconds).
TYPE:
|
time_done |
Time the order was executed or cancelled (Unix seconds).
TYPE:
|
time_done_msc |
Execution/cancellation time in milliseconds.
TYPE:
|
time_expiration |
Expiry time for
TYPE:
|
type |
Order type (BUY, SELL, BUY_LIMIT, etc.).
TYPE:
|
type_time |
Expiry policy (GTC, DAY, SPECIFIED, etc.).
TYPE:
|
type_filling |
Fill policy (FOK, IOC, RETURN, BOC).
TYPE:
|
state |
Current lifecycle state of the order.
TYPE:
|
magic |
Expert Advisor identifier; 0 for manual orders.
TYPE:
|
position_id |
Ticket of the position this order is linked to.
TYPE:
|
position_by_id |
Ticket of the opposite position for CLOSE_BY orders.
TYPE:
|
reason |
What triggered the order placement.
TYPE:
|
volume_initial |
Original order volume in lots.
TYPE:
|
volume_current |
Remaining unfilled volume in lots.
TYPE:
|
price_open |
Requested execution price.
TYPE:
|
sl |
Stop loss price; 0.0 if not set.
TYPE:
|
tp |
Take profit price; 0.0 if not set.
TYPE:
|
price_current |
Current market price for the order's symbol.
TYPE:
|
price_stoplimit |
Stop-limit price for
TYPE:
|
symbol |
Trading instrument name.
TYPE:
|
comment |
Arbitrary comment attached to the order.
TYPE:
|
external_id |
Order identifier in an external system.
TYPE:
|
HistoricalOrder¶
HistoricalOrder
¶
Bases: BaseModel
A completed order retrieved from the MT5 trade history.
Wraps the raw TradeOrder struct returned by
mt5.history_orders_get(). Unlike Order, historical orders
include all terminal states (filled, cancelled, expired, rejected).
| ATTRIBUTE | DESCRIPTION |
|---|---|
ticket |
Unique order identifier.
TYPE:
|
time_setup |
Time the order was placed (Unix seconds).
TYPE:
|
time_setup_msc |
Placement time in milliseconds.
TYPE:
|
time_done |
Time the order reached its terminal state (Unix seconds).
TYPE:
|
time_done_msc |
Terminal state time in milliseconds.
TYPE:
|
time_expiration |
Expiry time for time-limited orders; 0 otherwise.
TYPE:
|
type |
Order type at the time of placement.
TYPE:
|
type_time |
Expiry policy used.
TYPE:
|
type_filling |
Fill policy used.
TYPE:
|
state |
Final state of the order.
TYPE:
|
magic |
Expert Advisor identifier; 0 for manual orders.
TYPE:
|
position_id |
Ticket of the position this order created or affected.
TYPE:
|
position_by_id |
Ticket of the opposite position for CLOSE_BY orders.
TYPE:
|
reason |
What triggered the order.
TYPE:
|
volume_initial |
Original order volume in lots.
TYPE:
|
volume_current |
Remaining unfilled volume (0.0 for fully filled orders).
TYPE:
|
price_open |
Requested execution price.
TYPE:
|
sl |
Stop loss price at the time of the order.
TYPE:
|
tp |
Take profit price at the time of the order.
TYPE:
|
price_current |
Price at the time the order reached its terminal state.
TYPE:
|
price_stoplimit |
Stop-limit activation price, if applicable.
TYPE:
|
symbol |
Trading instrument name.
TYPE:
|
comment |
Arbitrary comment attached to the order.
TYPE:
|
external_id |
Order identifier in an external system.
TYPE:
|
TradeRequest¶
TradeRequest
¶
Bases: BaseModel
Parameters for a trade operation sent to MT5 via order_send or order_check.
All enum-typed fields accept either the typed enum member or a raw integer — Pydantic coerces integers to the matching enum and rejects unknown values at construction time.
model_dump() is overridden to cast enum fields back to plain
int before the dict is passed to the MT5 C extension, which
expects native integers.
| ATTRIBUTE | DESCRIPTION |
|---|---|
action |
Trade action type (DEAL, PENDING, SLTP, MODIFY, REMOVE, CLOSE_BY).
TYPE:
|
magic |
Expert Advisor identifier; 0 for manual requests.
TYPE:
|
order |
Ticket of the order to modify or remove (for MODIFY/REMOVE actions).
TYPE:
|
symbol |
Trading instrument name.
TYPE:
|
volume |
Trade volume in lots.
TYPE:
|
price |
Requested execution price (0.0 for market orders).
TYPE:
|
stoplimit |
Stop-limit activation price for stop-limit orders.
TYPE:
|
sl |
Stop loss price; 0.0 to leave unset.
TYPE:
|
tp |
Take profit price; 0.0 to leave unset.
TYPE:
|
deviation |
Maximum allowed price deviation in points.
TYPE:
|
type |
Order type (BUY, SELL, BUY_LIMIT, etc.).
TYPE:
|
type_filling |
Fill policy (FOK, IOC, RETURN, BOC).
TYPE:
|
type_time |
Expiry policy (GTC, DAY, SPECIFIED, etc.).
TYPE:
|
expiration |
Expiry time for
TYPE:
|
comment |
Arbitrary comment to attach to the order.
TYPE:
|
position |
Ticket of the position to modify (for SLTP action).
TYPE:
|
position_by |
Ticket of the opposite position (for CLOSE_BY action).
TYPE:
|
model_dump
¶
Serialize to a plain dict with integer enum values for the MT5 API.
The MT5 C extension's order_send and order_check functions
expect a dict of plain Python ints. IntEnum members are ints,
but some MT5 builds reject non-int types, so enum fields are
explicitly cast here.
| RETURNS | DESCRIPTION |
|---|---|
dict[str, object]
|
A dict suitable for passing directly to |
dict[str, object]
|
or |
TradeResult¶
TradeResult
¶
Bases: BaseModel
Outcome of a trade request returned by order_send or order_check.
Wraps the raw OrderSendResult struct from the MT5 C extension.
The retcode field is kept as a plain int rather than an enum
because brokers can return custom codes outside the standard MT5 set —
enforcing a strict enum would cause ValidationError on valid
broker-specific responses.
| ATTRIBUTE | DESCRIPTION |
|---|---|
retcode |
MT5 return code (e.g. 10009 = DONE, 10006 = REJECT).
TYPE:
|
deal |
Ticket of the deal created by this request; 0 if none.
TYPE:
|
order |
Ticket of the order created by this request; 0 if none.
TYPE:
|
volume |
Volume actually executed in lots.
TYPE:
|
price |
Price at which the trade was executed.
TYPE:
|
bid |
Current bid price at the time of execution.
TYPE:
|
ask |
Current ask price at the time of execution.
TYPE:
|
comment |
Broker comment on the result.
TYPE:
|
request_id |
Identifier assigned to the request by the client terminal.
TYPE:
|
retcode_external |
Return code from an external trading system, if any.
TYPE:
|
request |
Echo of the original
TYPE:
|
is_successful
property
¶
Whether the trade was accepted by the broker.
Returns True for TRADE_RETCODE_PLACED (10008),
TRADE_RETCODE_DONE (10009), and
TRADE_RETCODE_DONE_PARTIAL (10010).
is_rejected
property
¶
Whether the trade was explicitly rejected by the broker (TRADE_RETCODE_REJECT).
requires_requote
property
¶
Whether the broker requires a new price before the trade can proceed (TRADE_RETCODE_REQUOTE).
Service¶
OrderService¶
OrderService
¶
Manages active orders and trade execution for the MT5 terminal.
Wraps the MT5 order API (orders_total, orders_get,
order_calc_margin, order_calc_profit, order_check,
order_send) via call_mt5, parses raw structs into Order
and TradeResult models, and returns Result[T].
Usage¶
from syntiq_mt5 import TradeRequest, constants
# Create a trade request
request = TradeRequest(
action=constants.TRADE_ACTION_DEAL,
symbol="EURUSD",
volume=0.10,
type=constants.ORDER_TYPE_BUY,
price=1.08500,
sl=1.08000,
tp=1.09000,
deviation=10,
)
# Validate first
check = mt5.order_check(request)
if check.success and check.data.is_successful:
# Send the order
res = mt5.order_send(request)
if res.success and res.data.is_successful:
print(f"Order filled: {res.data.order}")