Utils Module¶
mt5cli.utils ¶
Utility constants, types, and functions for the mt5cli package.
TIMEFRAME_NAMES
module-attribute
¶
TIMEFRAME_NAMES: tuple[str, ...] = tuple(
name
for name in TIMEFRAME_MAP
if not startswith("TIMEFRAME_")
)
Dataset ¶
Bases: StrEnum
Datasets supported by the collect-history command.
IfExists ¶
LogLevel ¶
OutputFormat ¶
coerce_login ¶
Coerce a login value to int, treating empty strings as unset.
Returns:
| Type | Description |
|---|---|
int | None
|
Integer login, or None when unset or an empty string. |
Source code in mt5cli/utils.py
detect_format ¶
Detect the output format from a file extension or explicit format string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output_path
|
Path
|
Path to the output file. |
required |
explicit_format
|
str | None
|
Explicitly specified format, if any. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
The detected format string. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the format cannot be determined. |
Source code in mt5cli/utils.py
export_dataframe ¶
export_dataframe(
df: DataFrame,
output_path: Path,
output_format: str,
table_name: str = "data",
) -> None
Export a pandas DataFrame to the specified file format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
DataFrame to export. |
required |
output_path
|
Path
|
Path to the output file. |
required |
output_format
|
str
|
Output format (csv, json, parquet, or sqlite3). |
required |
table_name
|
str
|
Table name for SQLite3 output. |
'data'
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the output format is not supported. |
Source code in mt5cli/utils.py
export_dataframe_to_sqlite ¶
export_dataframe_to_sqlite(
df: DataFrame,
output_path: Path,
table_name: str = "data",
*,
if_exists: IfExists = APPEND,
index: bool = False,
index_label: str | None = None,
deduplicate_on: Sequence[str] | None = None,
) -> None
Write a DataFrame to SQLite with configurable append and deduplication.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
DataFrame to export. |
required |
output_path
|
Path
|
SQLite database path. |
required |
table_name
|
str
|
Target table name. |
'data'
|
if_exists
|
IfExists
|
Conflict behavior when the table already exists. |
APPEND
|
index
|
bool
|
Whether to write the DataFrame index as a column. |
False
|
index_label
|
str | None
|
Column name for the index when |
None
|
deduplicate_on
|
Sequence[str] | None
|
Optional key columns to deduplicate after writing,
keeping the latest |
None
|
Source code in mt5cli/utils.py
parse_datetime ¶
Parse an ISO 8601 datetime string to a timezone-aware datetime.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
ISO 8601 datetime string (e.g., '2024-01-01' or '2024-01-01T12:00:00+00:00'). |
required |
Returns:
| Type | Description |
|---|---|
datetime
|
Parsed datetime with UTC timezone if no timezone is specified. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the string cannot be parsed. |
Source code in mt5cli/utils.py
parse_request ¶
Parse a JSON-formatted order request string or file reference.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
JSON object string, or '@path' to read JSON from a file. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Parsed request dictionary. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the request file cannot be read or the value is not a JSON object. |
Source code in mt5cli/utils.py
parse_tick_flags ¶
Parse tick flags string or integer value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
object
|
Tick flag name (ALL, INFO, TRADE, COPY_TICKS_*) or integer value. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Integer tick flag value compatible with MetaTrader 5 |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the flag is invalid. |
Source code in mt5cli/utils.py
parse_timeframe ¶
Parse a timeframe string or integer value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
object
|
Timeframe name (e.g., 'M1', 'H1', 'D1') or integer value. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Integer timeframe value. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the timeframe is invalid. |