Client¶
mt5cli.client ¶
Sole connection-lifecycle owner and stable public client abstraction.
This module owns the MT5 connection lifecycle end to end: building
Mt5Config, initializing and logging in to the terminal, and shutting it
down exactly once on exit. mt5_session() is the only public session
factory; every other mt5cli module that needs a connection imports the
private primitives defined here instead of duplicating lifecycle behavior.
__all__
module-attribute
¶
__all__ = [
"MT5Client",
"build_config",
"mt5_session",
"substitute_env_placeholders",
"substitute_mapping_values",
]
MT5Client ¶
MT5Client(
*,
path: str | None = None,
login: int | None = None,
password: str | None = None,
server: str | None = None,
timeout: int | None = None,
retry_count: int = 3,
config: Mt5Config | None = None,
)
Bases: _BaseMT5Client
The single public connected MT5 client.
Extends the read-only data access above with optional order check/send
helpers and exposes the same connection lifecycle as :func:mt5_session.
mt5cli intentionally exposes minimal execution primitives only. Trading decisions, signals, strategies, backtests, and optimization remain the responsibility of downstream applications.
Configure a client; use it as a context manager to connect.
Source code in mt5cli/client.py
mt5
property
¶
MT5 constants required by operational helpers.
This intentionally exposes constants only, never the pdmt5 client.
account_info_as_dict ¶
Return the current account snapshot as a plain mapping.
from_connected_client
classmethod
¶
Bind the public facade to an externally owned connection.
Returns:
| Type | Description |
|---|---|
Self
|
Public facade that never initializes or shuts down |
Source code in mt5cli/client.py
order_calc_margin ¶
Calculate broker margin for an order candidate.
Returns:
| Type | Description |
|---|---|
object
|
Broker-calculated margin value. |
Source code in mt5cli/client.py
order_check ¶
Check funds sufficiency for a trade request.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
dict[str, Any]
|
MT5 order request dictionary. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
One-row DataFrame with the order-check result. |
Source code in mt5cli/client.py
order_send ¶
Send a live trade request to the MT5 trade server.
Warning
This is a live execution primitive. A successful call can place, modify, or close real trades on the connected account. Downstream applications must gate usage explicitly (for example behind manual confirmation or application-specific risk controls). mt5cli does not implement strategy logic, signal generation, or trade sizing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
dict[str, Any]
|
MT5 order request dictionary. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
One-row DataFrame with the order-send result. |
Source code in mt5cli/client.py
positions_get_as_df ¶
symbol_info_as_dict ¶
Return one symbol snapshot as a plain mapping.
symbol_select ¶
Select or deselect a symbol in Market Watch.
Returns:
| Type | Description |
|---|---|
bool
|
Whether MT5 accepted the selection operation. |
Source code in mt5cli/client.py
build_config ¶
build_config(
*,
path: str | None = None,
login: int | str | None = None,
password: str | None = None,
server: str | None = None,
timeout: int | None = None,
allow_whole_dollar_env: bool = False,
) -> Mt5Config
Build an Mt5Config from optional connection parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | None
|
Optional terminal executable path. |
None
|
login
|
int | str | None
|
Optional trading account login. Integers are preserved. String
values are coerced: empty or whitespace-only strings become
|
None
|
password
|
str | None
|
Optional trading account password. |
None
|
server
|
str | None
|
Optional trading server name. |
None
|
timeout
|
int | None
|
Optional connection timeout in milliseconds. |
None
|
allow_whole_dollar_env
|
bool
|
When |
False
|
Returns:
| Type | Description |
|---|---|
Mt5Config
|
Configured |
Source code in mt5cli/client.py
mt5_session ¶
mt5_session(
config: Mt5Config | None = None,
*,
client: MT5Client | None = None,
) -> Generator[MT5Client, None, None]
Open an MT5 terminal session and yield a connected :class:MT5Client.
This is the sole public MT5 session factory. It is the only supported way
to obtain a connected :class:MT5Client for market data, history,
observability, and execution workflows.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
Mt5Config | None
|
MT5 connection configuration. Defaults to an empty config that attaches to a running terminal. |
None
|
client
|
MT5Client | None
|
A caller-owned connected public client. It is yielded as-is and is never initialized or shut down by this context manager. |
None
|
Yields:
| Name | Type | Description |
|---|---|---|
Connected |
MT5Client
|
class: |
Source code in mt5cli/client.py
substitute_env_placeholders ¶
Replace ${ENV_VAR} placeholders in a string with environment values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
String that may contain one or more |
required |
allow_whole_dollar_env
|
bool
|
When |
False
|
Returns:
| Type | Description |
|---|---|
str
|
The string with every placeholder replaced by its environment value. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If a referenced environment variable is not set. |
Source code in mt5cli/client.py
substitute_mapping_values ¶
substitute_mapping_values(
data: object,
*,
keys: Collection[str],
allow_whole_dollar_env: bool = False,
blank_string_keys_as_none: Collection[str] = (),
) -> object
Recursively substitute environment placeholders for selected mapping keys.
Traverses nested dicts and lists, expanding ${ENV_VAR} (and
$ENV_NAME when allow_whole_dollar_env=True) in string values
whose immediate parent dict key is in keys. Fields whose key is
not in keys are preserved exactly, including literal dollar signs.
Strings that are direct elements of a list are never substituted;
substitution only applies to strings that are immediate dict values.
This is a generic downstream config utility. Key names such as
mt5_login or mt5_password must be supplied by the caller;
mt5cli does not hard-code any application-specific key names.
Callers are responsible for ensuring data has bounded nesting depth;
deeply nested or self-referential structures will hit Python's recursion
limit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
object
|
Arbitrarily nested dict/list/scalar value to process. |
required |
keys
|
Collection[str]
|
Mapping keys whose string values receive placeholder substitution. |
required |
allow_whole_dollar_env
|
bool
|
When |
False
|
blank_string_keys_as_none
|
Collection[str]
|
Mapping keys for which blank strings
(after any substitution) are normalised to |
()
|
Returns:
| Type | Description |
|---|---|
object
|
The processed value. Dicts and lists are rebuilt into new |
object
|
containers with selected string values substituted and |
object
|
blank-normalised. Scalar inputs (non-dict, non-list) are |
object
|
returned as-is. |