Skip to content

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: int

time_setup

Time the order was placed (Unix seconds).

TYPE: int

time_setup_msc

Time the order was placed (milliseconds).

TYPE: int

time_done

Time the order was executed or cancelled (Unix seconds).

TYPE: int

time_done_msc

Execution/cancellation time in milliseconds.

TYPE: int

time_expiration

Expiry time for ORDER_TIME_SPECIFIED orders; 0 otherwise.

TYPE: int

type

Order type (BUY, SELL, BUY_LIMIT, etc.).

TYPE: OrderType

type_time

Expiry policy (GTC, DAY, SPECIFIED, etc.).

TYPE: OrderTime

type_filling

Fill policy (FOK, IOC, RETURN, BOC).

TYPE: OrderFilling

state

Current lifecycle state of the order.

TYPE: OrderState

magic

Expert Advisor identifier; 0 for manual orders.

TYPE: int

position_id

Ticket of the position this order is linked to.

TYPE: int

position_by_id

Ticket of the opposite position for CLOSE_BY orders.

TYPE: int

reason

What triggered the order placement.

TYPE: OrderReason

volume_initial

Original order volume in lots.

TYPE: float

volume_current

Remaining unfilled volume in lots.

TYPE: float

price_open

Requested execution price.

TYPE: float

sl

Stop loss price; 0.0 if not set.

TYPE: float

tp

Take profit price; 0.0 if not set.

TYPE: float

price_current

Current market price for the order's symbol.

TYPE: float

price_stoplimit

Stop-limit price for BUY_STOP_LIMIT / SELL_STOP_LIMIT orders.

TYPE: float

symbol

Trading instrument name.

TYPE: str

comment

Arbitrary comment attached to the order.

TYPE: str

external_id

Order identifier in an external system.

TYPE: str

is_pending property

is_pending: bool

Whether the order is waiting to be filled (placed or partially filled).

is_filled property

is_filled: bool

Whether the order has been fully executed.

is_cancelled property

is_cancelled: bool

Whether the order was cancelled before execution.

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: int

time_setup

Time the order was placed (Unix seconds).

TYPE: int

time_setup_msc

Placement time in milliseconds.

TYPE: int

time_done

Time the order reached its terminal state (Unix seconds).

TYPE: int

time_done_msc

Terminal state time in milliseconds.

TYPE: int

time_expiration

Expiry time for time-limited orders; 0 otherwise.

TYPE: int

type

Order type at the time of placement.

TYPE: OrderType

type_time

Expiry policy used.

TYPE: OrderTime

type_filling

Fill policy used.

TYPE: OrderFilling

state

Final state of the order.

TYPE: OrderState

magic

Expert Advisor identifier; 0 for manual orders.

TYPE: int

position_id

Ticket of the position this order created or affected.

TYPE: int

position_by_id

Ticket of the opposite position for CLOSE_BY orders.

TYPE: int

reason

What triggered the order.

TYPE: OrderReason

volume_initial

Original order volume in lots.

TYPE: float

volume_current

Remaining unfilled volume (0.0 for fully filled orders).

TYPE: float

price_open

Requested execution price.

TYPE: float

sl

Stop loss price at the time of the order.

TYPE: float

tp

Take profit price at the time of the order.

TYPE: float

price_current

Price at the time the order reached its terminal state.

TYPE: float

price_stoplimit

Stop-limit activation price, if applicable.

TYPE: float

symbol

Trading instrument name.

TYPE: str

comment

Arbitrary comment attached to the order.

TYPE: str

external_id

Order identifier in an external system.

TYPE: str

duration_seconds property

duration_seconds: int

Time between order placement and completion, in seconds.

was_filled property

was_filled: bool

Whether the order was fully executed before reaching a terminal state.

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: TradeAction

magic

Expert Advisor identifier; 0 for manual requests.

TYPE: int

order

Ticket of the order to modify or remove (for MODIFY/REMOVE actions).

TYPE: int

symbol

Trading instrument name.

TYPE: str

volume

Trade volume in lots.

TYPE: float

price

Requested execution price (0.0 for market orders).

TYPE: float

stoplimit

Stop-limit activation price for stop-limit orders.

TYPE: float

sl

Stop loss price; 0.0 to leave unset.

TYPE: float

tp

Take profit price; 0.0 to leave unset.

TYPE: float

deviation

Maximum allowed price deviation in points.

TYPE: int

type

Order type (BUY, SELL, BUY_LIMIT, etc.).

TYPE: OrderType

type_filling

Fill policy (FOK, IOC, RETURN, BOC).

TYPE: OrderFilling

type_time

Expiry policy (GTC, DAY, SPECIFIED, etc.).

TYPE: OrderTime

expiration

Expiry time for ORDER_TIME_SPECIFIED orders (Unix seconds).

TYPE: int

comment

Arbitrary comment to attach to the order.

TYPE: str

position

Ticket of the position to modify (for SLTP action).

TYPE: int

position_by

Ticket of the opposite position (for CLOSE_BY action).

TYPE: int

model_dump

model_dump(**kwargs: object) -> dict[str, object]

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 mt5.order_send()

dict[str, object]

or mt5.order_check().

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: int

deal

Ticket of the deal created by this request; 0 if none.

TYPE: int

order

Ticket of the order created by this request; 0 if none.

TYPE: int

volume

Volume actually executed in lots.

TYPE: float

price

Price at which the trade was executed.

TYPE: float

bid

Current bid price at the time of execution.

TYPE: float

ask

Current ask price at the time of execution.

TYPE: float

comment

Broker comment on the result.

TYPE: str

request_id

Identifier assigned to the request by the client terminal.

TYPE: int

retcode_external

Return code from an external trading system, if any.

TYPE: int

request

Echo of the original TradeRequest dict, if available.

TYPE: dict[str, object] | None

is_successful property

is_successful: bool

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

is_rejected: bool

Whether the trade was explicitly rejected by the broker (TRADE_RETCODE_REJECT).

requires_requote property

requires_requote: bool

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}")