Client¶
The main entry point for all MetaTrader 5 operations.
MetaTrader5Client¶
MetaTrader5Client
¶
Unified client for all MetaTrader 5 operations.
Composes the individual service classes (connection, positions, orders,
history, market data, etc.) behind a single interface. All methods
return Result[T] β never raise β so callers always handle both the
success and failure paths explicitly.
Lifecycle¶
initialize()β connect to the terminal (required before anything else)login()β authenticate with the broker (required for trading operations)- (use the client)
shutdown()β disconnect cleanly
The client can also be used as a context manager; shutdown() is
called automatically on exit regardless of whether an exception occurred.
| PARAMETER | DESCRIPTION |
|---|---|
debug
|
When
TYPE:
|
Initialise the client and instantiate all service objects.
| PARAMETER | DESCRIPTION |
|---|---|
debug
|
Enable per-call DEBUG logging with timing information.
TYPE:
|
__exit__
¶
Call shutdown() automatically when leaving the context block.
initialize
¶
Connect to the MetaTrader 5 terminal.
Must be called before any other operation. Sets the internal
_initialized flag on success so subsequent calls to other
methods are permitted.
| PARAMETER | DESCRIPTION |
|---|---|
credentials
|
Optional credentials; only
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Result[None]
|
|
login
¶
Authenticate with the broker.
Requires a prior successful initialize() call.
| PARAMETER | DESCRIPTION |
|---|---|
credentials
|
Account number, password, and server name.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Result[None]
|
|
shutdown
¶
Disconnect from the MetaTrader 5 terminal.
Resets _initialized and _logged_in regardless of outcome.
Safe to call multiple times.
| RETURNS | DESCRIPTION |
|---|---|
Result[None]
|
|
version
¶
Return the connected terminal's version information.
| RETURNS | DESCRIPTION |
|---|---|
Result[tuple[int, int, str]]
|
|
positions
¶
positions_total
¶
Return the total number of open positions.
| RETURNS | DESCRIPTION |
|---|---|
Result[int]
|
|
get_candles
¶
Retrieve the most recent OHLCV candles for a symbol.
| PARAMETER | DESCRIPTION |
|---|---|
symbol
|
Trading instrument name (e.g.
TYPE:
|
timeframe
|
MT5 timeframe constant (e.g.
TYPE:
|
count
|
Number of bars to retrieve.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Result[list[Candle]]
|
|
copy_rates_from
¶
copy_rates_from(
symbol: str,
timeframe: int,
date_from: datetime,
count: int,
) -> Result[list[Candle]]
Retrieve candles starting from a specific datetime.
| PARAMETER | DESCRIPTION |
|---|---|
symbol
|
Trading instrument name.
TYPE:
|
timeframe
|
MT5 timeframe constant.
TYPE:
|
date_from
|
Start datetime (inclusive).
TYPE:
|
count
|
Number of bars to retrieve going forward.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Result[list[Candle]]
|
|
copy_rates_range
¶
copy_rates_range(
symbol: str,
timeframe: int,
date_from: datetime,
date_to: datetime,
) -> Result[list[Candle]]
Retrieve all candles within a datetime range.
| PARAMETER | DESCRIPTION |
|---|---|
symbol
|
Trading instrument name.
TYPE:
|
timeframe
|
MT5 timeframe constant.
TYPE:
|
date_from
|
Range start datetime (inclusive).
TYPE:
|
date_to
|
Range end datetime (inclusive).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Result[list[Candle]]
|
|
account_info
¶
Retrieve the current trading account details.
| RETURNS | DESCRIPTION |
|---|---|
Result[AccountInfo]
|
|
Result[AccountInfo]
|
and margin on success. |
terminal_info
¶
Retrieve the connected terminal's configuration and state.
| RETURNS | DESCRIPTION |
|---|---|
Result[TerminalInfo]
|
|
Result[TerminalInfo]
|
status and trading permissions on success. |
symbols_total
¶
Return the total number of symbols available in the terminal.
| RETURNS | DESCRIPTION |
|---|---|
Result[int]
|
|
symbols_get
¶
Retrieve symbol names, optionally filtered by group pattern.
| PARAMETER | DESCRIPTION |
|---|---|
group
|
Wildcard filter pattern (e.g.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Result[list[str]]
|
|
symbol_select
¶
Add or remove a symbol from the Market Watch window.
| PARAMETER | DESCRIPTION |
|---|---|
symbol
|
Trading instrument name.
TYPE:
|
enable
|
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Result[bool]
|
|
symbol_info
¶
Retrieve full specification data for a symbol.
| PARAMETER | DESCRIPTION |
|---|---|
symbol
|
Trading instrument name.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Result[SymbolInfo]
|
|
symbol_info_tick
¶
Retrieve the latest tick for a symbol.
| PARAMETER | DESCRIPTION |
|---|---|
symbol
|
Trading instrument name.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Result[SymbolTick]
|
|
market_book_add
¶
Subscribe to market depth (DOM) updates for a symbol.
Must be called before market_book_get() for the same symbol.
| PARAMETER | DESCRIPTION |
|---|---|
symbol
|
Trading instrument name.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Result[bool]
|
|
market_book_get
¶
market_book_release
¶
Unsubscribe from market depth updates for a symbol.
| PARAMETER | DESCRIPTION |
|---|---|
symbol
|
Trading instrument name.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Result[bool]
|
|
copy_ticks_from
¶
Retrieve ticks starting from a specific datetime.
| PARAMETER | DESCRIPTION |
|---|---|
symbol
|
Trading instrument name.
TYPE:
|
date_from
|
Start datetime (inclusive).
TYPE:
|
count
|
Maximum number of ticks to retrieve.
TYPE:
|
flags
|
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Result[list[Tick]]
|
|
copy_ticks_range
¶
copy_ticks_range(
symbol: str,
date_from: datetime,
date_to: datetime,
flags: int,
) -> Result[list[Tick]]
Retrieve all ticks within a datetime range.
| PARAMETER | DESCRIPTION |
|---|---|
symbol
|
Trading instrument name.
TYPE:
|
date_from
|
Range start datetime (inclusive).
TYPE:
|
date_to
|
Range end datetime (inclusive).
TYPE:
|
flags
|
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Result[list[Tick]]
|
|
orders_total
¶
Return the total number of active pending orders.
| RETURNS | DESCRIPTION |
|---|---|
Result[int]
|
|
orders_get
¶
orders_get(
symbol: str | None = None,
group: str | None = None,
ticket: int | None = None,
) -> Result[list[Order]]
Retrieve active pending orders with optional filtering.
At most one filter argument should be provided. When none are given, all active orders are returned.
| PARAMETER | DESCRIPTION |
|---|---|
symbol
|
Filter by trading instrument name.
TYPE:
|
group
|
Filter by symbol group wildcard pattern.
TYPE:
|
ticket
|
Filter by order ticket number.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Result[list[Order]]
|
|
order_calc_margin
¶
Calculate the margin required for a hypothetical order.
| PARAMETER | DESCRIPTION |
|---|---|
action
|
Trade action type (
TYPE:
|
symbol
|
Trading instrument name.
TYPE:
|
volume
|
Order volume in lots.
TYPE:
|
price
|
Intended execution price.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Result[float]
|
|
order_calc_profit
¶
order_calc_profit(
action: int,
symbol: str,
volume: float,
price_open: float,
price_close: float,
) -> Result[float]
Calculate the profit for a hypothetical trade.
| PARAMETER | DESCRIPTION |
|---|---|
action
|
Trade action type (
TYPE:
|
symbol
|
Trading instrument name.
TYPE:
|
volume
|
Trade volume in lots.
TYPE:
|
price_open
|
Hypothetical entry price.
TYPE:
|
price_close
|
Hypothetical exit price.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Result[float]
|
|
order_check
¶
Validate a trade request without submitting it.
Useful for pre-flight checks before calling order_send().
| PARAMETER | DESCRIPTION |
|---|---|
request
|
The trade request to validate.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Result[TradeResult]
|
|
Result[TradeResult]
|
requirements and any rejection reason on success. |
order_send
¶
Submit a trade request to the broker.
| PARAMETER | DESCRIPTION |
|---|---|
request
|
The trade request to execute. Use
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Result[TradeResult]
|
|
Result[TradeResult]
|
and order tickets on success. Check |
Result[TradeResult]
|
for the broker's acceptance status. |
history_orders_total
¶
Return the number of historical orders in a date range.
| PARAMETER | DESCRIPTION |
|---|---|
date_from
|
Range start (inclusive).
TYPE:
|
date_to
|
Range end (inclusive).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Result[int]
|
|
history_orders_get
¶
history_orders_get(
date_from: datetime | None = None,
date_to: datetime | None = None,
group: str | None = None,
ticket: int | None = None,
position: int | None = None,
) -> Result[list[HistoricalOrder]]
Retrieve historical orders with flexible filtering.
Exactly one filter strategy must be provided: either a date range
(date_from + date_to), a group pattern, a ticket, or a
position ticket.
| PARAMETER | DESCRIPTION |
|---|---|
date_from
|
Range start (used with
TYPE:
|
date_to
|
Range end (used with
TYPE:
|
group
|
Symbol group wildcard pattern.
TYPE:
|
ticket
|
Order ticket number.
TYPE:
|
position
|
Position ticket to retrieve orders for.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Result[list[HistoricalOrder]]
|
|
history_deals_total
¶
Return the number of deals in a date range.
| PARAMETER | DESCRIPTION |
|---|---|
date_from
|
Range start (inclusive).
TYPE:
|
date_to
|
Range end (inclusive).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Result[int]
|
|
history_deals_get
¶
history_deals_get(
date_from: datetime | None = None,
date_to: datetime | None = None,
group: str | None = None,
ticket: int | None = None,
position: int | None = None,
) -> Result[list[Deal]]
Retrieve deals from the trade history with flexible filtering.
Exactly one filter strategy must be provided: either a date range, a group pattern, a deal ticket, or a position ticket.
| PARAMETER | DESCRIPTION |
|---|---|
date_from
|
Range start (used with
TYPE:
|
date_to
|
Range end (used with
TYPE:
|
group
|
Symbol group wildcard pattern.
TYPE:
|
ticket
|
Deal ticket number.
TYPE:
|
position
|
Position ticket to retrieve deals for.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Result[list[Deal]]
|
|
Usage¶
from pydantic import SecretStr
from syntiq_mt5 import LoginCredential, MetaTrader5Client
creds = LoginCredential(
login=12345678,
password=SecretStr("your-password"),
server="Broker-Demo",
)
with MetaTrader5Client(debug=True) as mt5:
# Initialize and login
mt5.initialize(creds)
mt5.login(creds)
# Use any method
res = mt5.positions()
if res.success:
for p in res.data:
print(f"{p.symbol}: {p.pips_profit:+.1f} pips")
The client automatically calls shutdown() when the context manager exits.