Skip to content

Results and errors

Expected failures are values, not exceptions (ADR 0002). What to do about each ErrorKind is in the user guide.

Result module-attribute

Result = Union[Ok[T], Err[E]]

Either Ok(value) or Err(error); every trading call returns one.

Ok dataclass

Ok(value: T)

Bases: Generic[T]

A call that succeeded, carrying its value.

value instance-attribute

value: T

is_ok

is_ok() -> bool

Always True; prefer match or isinstance to narrow the type.

is_err

is_err() -> bool

Always False.

Err dataclass

Err(error: E)

Bases: Generic[E]

A call that failed in an expected way, carrying its error.

error instance-attribute

error: E

is_ok

is_ok() -> bool

Always False.

is_err

is_err() -> bool

Always True; prefer match or isinstance to narrow the type.

TradeError dataclass

TradeError(kind: ErrorKind, message: str, retcode: int | None = None, receipt: TradeReceipt | None = None, ticket: int | None = None)

An expected failure: its ErrorKind, a message and the broker's answer if any.

receipt is the broker's raw answer when there was one; ticket names the position or order when the failure concerns one item of a batch.

kind instance-attribute

kind: ErrorKind

message instance-attribute

message: str

retcode class-attribute instance-attribute

retcode: int | None = None

receipt class-attribute instance-attribute

receipt: TradeReceipt | None = None

ticket class-attribute instance-attribute

ticket: int | None = None

ErrorKind

Bases: Enum

Why a call failed. What to do about each kind is in the user guide.

INVALID_ARGUMENT class-attribute instance-attribute

INVALID_ARGUMENT = 'invalid_argument'

An argument MT5 would misread or refuse: checked before sending, or refused by the server.

INVALID_VOLUME class-attribute instance-attribute

INVALID_VOLUME = 'invalid_volume'

The volume is outside the symbol's min/max, off its step, or above the position's.

INVALID_PRICE class-attribute instance-attribute

INVALID_PRICE = 'invalid_price'

The price is invalid, e.g. a limit on the wrong side of the market.

INVALID_STOPS class-attribute instance-attribute

INVALID_STOPS = 'invalid_stops'

A stop-loss or take-profit is zero, negative, not finite or refused by the server.

INVALID_FILLING class-attribute instance-attribute

INVALID_FILLING = 'invalid_filling'

The server refused the filling mode chosen from the symbol's flags (10030).

NO_MONEY class-attribute instance-attribute

NO_MONEY = 'no_money'

Not enough margin.

MARKET_CLOSED class-attribute instance-attribute

MARKET_CLOSED = 'market_closed'

The market for the symbol is closed.

TRADE_DISABLED class-attribute instance-attribute

TRADE_DISABLED = 'trade_disabled'

Trading is not allowed now: on the symbol or account, or in this direction.

ALGO_TRADING_DISABLED class-attribute instance-attribute

ALGO_TRADING_DISABLED = 'algo_trading_disabled'

Algo Trading is off in the terminal (10027) or forbidden by the server (10026).

LIMIT_REACHED class-attribute instance-attribute

LIMIT_REACHED = 'limit_reached'

A broker limit on orders, positions or volume is reached.

PARTIAL_FILL class-attribute instance-attribute

PARTIAL_FILL = 'partial_fill'

Only part of the volume was executed; the rest is still open or unfilled.

OUTCOME_UNKNOWN class-attribute instance-attribute

OUTCOME_UNKNOWN = 'outcome_unknown'

The request may or may not have reached the server: check positions, never auto-retried.

RETRIES_EXHAUSTED class-attribute instance-attribute

RETRIES_EXHAUSTED = 'retries_exhausted'

Requotes, price changes, no quotes or too many requests outlasted the RetryPolicy.

REJECTED class-attribute instance-attribute

REJECTED = 'rejected'

Any other refusal by the server, unknown retcodes included.

SYMBOL_NOT_FOUND class-attribute instance-attribute

SYMBOL_NOT_FOUND = 'symbol_not_found'

The terminal does not know the symbol.

NO_PRICE class-attribute instance-attribute

NO_PRICE = 'no_price'

The symbol has no quote.

TERMINAL_ERROR class-attribute instance-attribute

TERMINAL_ERROR = 'terminal_error'

A terminal call failed before anything reached the trade server.

BROKEN_SYMBOL_DATA class-attribute instance-attribute

BROKEN_SYMBOL_DATA = 'broken_symbol_data'

The terminal's symbol data cannot price an order (point 0, NaN tick size, step 0).

Unlike TERMINAL_ERROR it does not clear on a resend, so it has its own kind: a bot must escalate, not retry.

CONNECTION_FAILED class-attribute instance-attribute

CONNECTION_FAILED = 'connection_failed'

The terminal did not start or the login failed.

BatchFailure dataclass

BatchFailure(succeeded: tuple[TradeReceipt, ...], errors: tuple[TradeError, ...])

Some items of a batch (close all / cancel all) failed; others may have succeeded.

succeeded instance-attribute

succeeded: tuple[TradeReceipt, ...]

errors instance-attribute

errors: tuple[TradeError, ...]