SDK Module¶
mt5cli.sdk ¶
Programmatic SDK for MetaTrader 5 data collection.
__all__
module-attribute
¶
__all__ = [
"AccountSpec",
"Mt5CliClient",
"account_info",
"build_config",
"collect_history",
"collect_latest_rates",
"collect_latest_rates_for_accounts",
"copy_rates_from",
"copy_rates_from_pos",
"copy_rates_range",
"copy_ticks_from",
"copy_ticks_range",
"history_deals",
"history_orders",
"last_error",
"latest_rates",
"market_book",
"minimum_margins",
"mt5_session",
"mt5_summary",
"mt5_summary_as_df",
"orders",
"positions",
"recent_history_deals",
"recent_ticks",
"symbol_info",
"symbol_info_tick",
"symbols",
"terminal_info",
"update_history",
"update_history_with_config",
"version",
]
AccountSpec
dataclass
¶
AccountSpec(
symbols: Sequence[str],
login: int | str | None = None,
password: str | None = None,
server: str | None = None,
path: str | None = None,
timeout: int | None = None,
)
Connection parameters and symbols for one MT5 account group.
Attributes:
| Name | Type | Description |
|---|---|---|
symbols |
Sequence[str]
|
Symbols to load latest rates for under this account. |
login |
int | str | None
|
Trading account login. String values are coerced to int when non-empty. |
password |
str | None
|
Trading account password. |
server |
str | None
|
Trading server name. |
path |
str | None
|
Path to the MetaTrader5 terminal EXE file. |
timeout |
int | None
|
Connection timeout in milliseconds. |
password
class-attribute
instance-attribute
¶
Mt5CliClient ¶
Mt5CliClient(
*,
path: str | None = None,
login: int | None = None,
password: str | None = None,
server: str | None = None,
timeout: int | None = None,
config: Mt5Config | None = None,
client: Mt5DataClient | None = None,
)
Programmatic client for read-only MetaTrader 5 data access.
Initialize the SDK client.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | None
|
Path to MetaTrader5 terminal EXE file. |
None
|
login
|
int | None
|
Trading account login. |
None
|
password
|
str | None
|
Trading account password. |
None
|
server
|
str | None
|
Trading server name. |
None
|
timeout
|
int | None
|
Connection timeout in milliseconds. |
None
|
config
|
Mt5Config | None
|
Optional pre-built |
None
|
client
|
Mt5DataClient | None
|
Optional already-connected |
None
|
Source code in mt5cli/sdk.py
__enter__ ¶
Open a persistent MT5 connection for multiple calls.
Returns:
| Type | Description |
|---|---|
Self
|
This client instance. |
Source code in mt5cli/sdk.py
__exit__ ¶
Shut down the persistent MT5 connection.
Source code in mt5cli/sdk.py
account_info ¶
collect_latest_rates ¶
collect_latest_rates(
symbols: Sequence[str],
timeframes: Sequence[int | str],
*,
count: int,
start_pos: int = 0,
) -> dict[tuple[str, int], DataFrame]
Return latest rates for each symbol/timeframe pair.
Returns:
| Type | Description |
|---|---|
dict[tuple[str, int], DataFrame]
|
Mapping keyed by |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in mt5cli/sdk.py
copy_rates_from ¶
copy_rates_from(
symbol: str,
timeframe: int | str,
date_from: datetime | str,
count: int,
) -> DataFrame
Return rates starting from a date.
Source code in mt5cli/sdk.py
copy_rates_from_pos ¶
Return rates starting from a bar position.
Source code in mt5cli/sdk.py
copy_rates_range ¶
copy_rates_range(
symbol: str,
timeframe: int | str,
date_from: datetime | str,
date_to: datetime | str,
) -> DataFrame
Return rates for a date range.
Source code in mt5cli/sdk.py
copy_ticks_from ¶
copy_ticks_from(
symbol: str,
date_from: datetime | str,
count: int,
flags: int | str,
) -> DataFrame
Return ticks starting from a date.
Source code in mt5cli/sdk.py
copy_ticks_range ¶
copy_ticks_range(
symbol: str,
date_from: datetime | str,
date_to: datetime | str,
flags: int | str,
) -> DataFrame
Return ticks for a date range.
Source code in mt5cli/sdk.py
from_connected_client
classmethod
¶
Bind to an already-connected Mt5DataClient without owning it.
The returned Mt5CliClient never initializes or shuts down the
injected client, including when used as a context manager.
Returns:
| Type | Description |
|---|---|
Self
|
Client wrapper bound to the injected connection. |
Source code in mt5cli/sdk.py
history_deals ¶
history_deals(
date_from: datetime | str | None = None,
date_to: datetime | str | None = None,
group: str | None = None,
symbol: str | None = None,
ticket: int | None = None,
position: int | None = None,
) -> DataFrame
Return historical deals.
Source code in mt5cli/sdk.py
history_orders ¶
history_orders(
date_from: datetime | str | None = None,
date_to: datetime | str | None = None,
group: str | None = None,
symbol: str | None = None,
ticket: int | None = None,
position: int | None = None,
) -> DataFrame
Return historical orders.
Source code in mt5cli/sdk.py
last_error ¶
latest_rates ¶
Return the latest rates from a bar position.
Source code in mt5cli/sdk.py
market_book ¶
minimum_margins ¶
Return minimum-volume buy and sell margin requirements.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
Symbol name. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
One-row DataFrame with columns |
DataFrame
|
|
Source code in mt5cli/sdk.py
mt5_summary ¶
Return a compact terminal/account status summary.
Source code in mt5cli/sdk.py
mt5_summary_as_df ¶
Return an export-safe one-row terminal/account summary DataFrame.
Source code in mt5cli/sdk.py
orders ¶
orders(
symbol: str | None = None,
group: str | None = None,
ticket: int | None = None,
) -> DataFrame
Return active orders.
Source code in mt5cli/sdk.py
positions ¶
positions(
symbol: str | None = None,
group: str | None = None,
ticket: int | None = None,
) -> DataFrame
Return open positions.
Source code in mt5cli/sdk.py
recent_history_deals ¶
recent_history_deals(
hours: float,
date_to: datetime | str | None = None,
group: str | None = None,
symbol: str | None = None,
) -> DataFrame
Return historical deals from a recent trailing window.
Source code in mt5cli/sdk.py
recent_ticks ¶
recent_ticks(
symbol: str,
seconds: float,
*,
date_to: datetime | str | None = None,
count: int = 10000,
flags: int | str = "ALL",
) -> DataFrame
Return ticks from a recent time window.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
Symbol name. |
required |
seconds
|
float
|
Lookback window in seconds ending at |
required |
date_to
|
datetime | str | None
|
Window end time. When |
None
|
count
|
int
|
Maximum ticks to return. Values |
10000
|
flags
|
int | str
|
Tick flags as |
'ALL'
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
Tick DataFrame with MT5 tick columns such as |
DataFrame
|
|
Source code in mt5cli/sdk.py
symbol_info ¶
symbol_info_tick ¶
symbols ¶
terminal_info ¶
account_info ¶
build_config ¶
build_config(
*,
path: str | None = None,
login: int | None = None,
password: str | None = None,
server: str | None = None,
timeout: int | None = None,
) -> Mt5Config
Build an Mt5Config from optional connection parameters.
Returns:
| Type | Description |
|---|---|
Mt5Config
|
Configured |
Source code in mt5cli/sdk.py
collect_history ¶
collect_history(
output: Path,
symbols: list[str],
date_from: datetime | str,
date_to: datetime | str,
*,
datasets: set[Dataset] | None = None,
timeframe: int | str = 1,
flags: int | str = 1,
if_exists: IfExists = FAIL,
with_views: bool = False,
config: Mt5Config | None = None,
) -> None
Collect historical datasets into a single SQLite database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output
|
Path
|
SQLite database path. |
required |
symbols
|
list[str]
|
Symbols to collect. |
required |
date_from
|
datetime | str
|
Start date. |
required |
date_to
|
datetime | str
|
End date. |
required |
datasets
|
set[Dataset] | None
|
Datasets to include (defaults to all). |
None
|
timeframe
|
int | str
|
Rates timeframe as integer or name (e.g. |
1
|
flags
|
int | str
|
Tick copy flags as integer or name (e.g. |
1
|
if_exists
|
IfExists
|
Behavior when a target table already exists. |
FAIL
|
with_views
|
bool
|
Create |
False
|
config
|
Mt5Config | None
|
MT5 connection configuration. |
None
|
Source code in mt5cli/sdk.py
collect_latest_rates ¶
collect_latest_rates(
symbols: Sequence[str],
timeframes: Sequence[int | str],
*,
count: int,
start_pos: int = 0,
config: Mt5Config | None = None,
) -> dict[tuple[str, int], DataFrame]
Return latest rates for each symbol/timeframe pair.
Source code in mt5cli/sdk.py
collect_latest_rates_for_accounts ¶
collect_latest_rates_for_accounts(
accounts: Sequence[AccountSpec],
timeframes: Sequence[int | str],
count: int,
*,
start_pos: int = 0,
base_config: Mt5Config | None = None,
) -> dict[tuple[str, int], DataFrame]
Collect latest rates across multiple MT5 account groups.
Each account is connected in turn, its symbols are read for every timeframe, and the resulting frames are merged into a single mapping.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
accounts
|
Sequence[AccountSpec]
|
Account groups to read. Each must define at least one symbol. |
required |
timeframes
|
Sequence[int | str]
|
MT5 timeframes as integers or names (for example |
required |
count
|
int
|
Number of most recent bars to read per symbol/timeframe. |
required |
start_pos
|
int
|
Initial bar position offset. |
0
|
base_config
|
Mt5Config | None
|
Optional base configuration whose fields fill any value not set on an individual account. |
None
|
Returns:
| Type | Description |
|---|---|
dict[tuple[str, int], DataFrame]
|
Mapping keyed by |
dict[tuple[str, int], DataFrame]
|
symbol/timeframe pair, the last account processed wins. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in mt5cli/sdk.py
copy_rates_from ¶
copy_rates_from(
symbol: str,
timeframe: int | str,
date_from: datetime | str,
count: int,
*,
config: Mt5Config | None = None,
) -> DataFrame
Return rates starting from a date.
Source code in mt5cli/sdk.py
copy_rates_from_pos ¶
copy_rates_from_pos(
symbol: str,
timeframe: int | str,
start_pos: int,
count: int,
*,
config: Mt5Config | None = None,
) -> DataFrame
Return rates starting from a bar position.
Source code in mt5cli/sdk.py
copy_rates_range ¶
copy_rates_range(
symbol: str,
timeframe: int | str,
date_from: datetime | str,
date_to: datetime | str,
*,
config: Mt5Config | None = None,
) -> DataFrame
Return rates for a date range.
Source code in mt5cli/sdk.py
copy_ticks_from ¶
copy_ticks_from(
symbol: str,
date_from: datetime | str,
count: int,
flags: int | str,
*,
config: Mt5Config | None = None,
) -> DataFrame
Return ticks starting from a date.
Source code in mt5cli/sdk.py
copy_ticks_range ¶
copy_ticks_range(
symbol: str,
date_from: datetime | str,
date_to: datetime | str,
flags: int | str,
*,
config: Mt5Config | None = None,
) -> DataFrame
Return ticks for a date range.
Source code in mt5cli/sdk.py
history_deals ¶
history_deals(
date_from: datetime | str | None = None,
date_to: datetime | str | None = None,
group: str | None = None,
symbol: str | None = None,
ticket: int | None = None,
position: int | None = None,
*,
config: Mt5Config | None = None,
) -> DataFrame
Return historical deals.
Source code in mt5cli/sdk.py
history_orders ¶
history_orders(
date_from: datetime | str | None = None,
date_to: datetime | str | None = None,
group: str | None = None,
symbol: str | None = None,
ticket: int | None = None,
position: int | None = None,
*,
config: Mt5Config | None = None,
) -> DataFrame
Return historical orders.
Source code in mt5cli/sdk.py
last_error ¶
latest_rates ¶
latest_rates(
symbol: str,
timeframe: int | str,
count: int,
start_pos: int = 0,
*,
config: Mt5Config | None = None,
) -> DataFrame
Return the latest rates from a bar position.
Source code in mt5cli/sdk.py
market_book ¶
minimum_margins ¶
Return minimum-volume buy and sell margin requirements.
See Mt5CliClient.minimum_margins for return details.
Source code in mt5cli/sdk.py
mt5_session ¶
mt5_session(
config: Mt5Config | None = None,
) -> Iterator[Mt5CliClient]
Open an MT5 terminal session and yield a connected client.
Launches the MetaTrader 5 terminal using Mt5Config.path (when set),
logs in, yields a connected :class:Mt5CliClient, and always shuts the
terminal down on exit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
Mt5Config | None
|
MT5 connection configuration. Defaults to an empty config that attaches to a running terminal. |
None
|
Yields:
| Type | Description |
|---|---|
Mt5CliClient
|
Connected |
Source code in mt5cli/sdk.py
mt5_summary ¶
mt5_summary_as_df ¶
Return an export-safe terminal/account status summary DataFrame.
orders ¶
orders(
symbol: str | None = None,
group: str | None = None,
ticket: int | None = None,
*,
config: Mt5Config | None = None,
) -> DataFrame
Return active orders.
Source code in mt5cli/sdk.py
positions ¶
positions(
symbol: str | None = None,
group: str | None = None,
ticket: int | None = None,
*,
config: Mt5Config | None = None,
) -> DataFrame
Return open positions.
Source code in mt5cli/sdk.py
recent_history_deals ¶
recent_history_deals(
hours: float,
date_to: datetime | str | None = None,
group: str | None = None,
symbol: str | None = None,
*,
config: Mt5Config | None = None,
) -> DataFrame
Return historical deals from a recent trailing window.
Source code in mt5cli/sdk.py
recent_ticks ¶
recent_ticks(
symbol: str,
seconds: float,
*,
date_to: datetime | str | None = None,
count: int = 10000,
flags: int | str = "ALL",
config: Mt5Config | None = None,
) -> DataFrame
Return ticks from a recent time window ending at date_to or now.
See Mt5CliClient.recent_ticks for parameter and return details.
Source code in mt5cli/sdk.py
symbol_info ¶
symbol_info_tick ¶
symbols ¶
terminal_info ¶
update_history ¶
update_history(
*,
client: Mt5DataClient,
output: Path | str,
symbols: Sequence[str],
datasets: set[Dataset] | None = None,
timeframes: Sequence[int | str] | None = None,
flags: int | str = "ALL",
lookback_hours: float = 24.0,
date_to: datetime | str | None = None,
deduplicate: bool = True,
create_rate_views: bool = True,
with_views: bool = False,
include_account_events: bool = True,
) -> None
Incrementally append MT5 history into a SQLite database.
Uses an already-connected Mt5DataClient and does not create or close
the MT5 connection. For first-time tables, data is fetched from
date_to - lookback_hours. Subsequent runs resume from existing
MAX(time) per symbol (and timeframe for rates); when
include_account_events=True, account-level deals use a separate cursor
over type NOT IN (0, 1) / empty-symbol rows.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
Mt5DataClient
|
Connected MT5 data client. |
required |
output
|
Path | str
|
SQLite database path. |
required |
symbols
|
Sequence[str]
|
Symbols to update. |
required |
datasets
|
set[Dataset] | None
|
Datasets to include (defaults to all). |
None
|
timeframes
|
Sequence[int | str] | None
|
Rate timeframes to update (defaults to all fixed MT5 timeframes when None). |
None
|
flags
|
int | str
|
Tick copy flags as integer or name (e.g. |
'ALL'
|
lookback_hours
|
float
|
First-run lookback when a table has no prior rows. |
24.0
|
date_to
|
datetime | str | None
|
Optional update end datetime. Defaults to now (UTC). |
None
|
deduplicate
|
bool
|
Remove duplicate rows after append, keeping latest ROWID. |
True
|
create_rate_views
|
bool
|
Create |
True
|
with_views
|
bool
|
Create |
False
|
include_account_events
|
bool
|
Include account-level cash events in
|
True
|
Source code in mt5cli/sdk.py
update_history_with_config ¶
update_history_with_config(
*,
output: Path | str,
symbols: Sequence[str],
config: Mt5Config | None = None,
datasets: set[Dataset] | None = None,
timeframes: Sequence[int | str] | None = None,
flags: int | str = "ALL",
lookback_hours: float = 24.0,
date_to: datetime | str | None = None,
deduplicate: bool = True,
create_rate_views: bool = True,
with_views: bool = False,
include_account_events: bool = True,
) -> None
Incrementally append MT5 history, opening and closing the MT5 connection.
Convenience wrapper around :func:update_history for standalone use.