Utils Module¶
mt5cli.utils ¶
Utility constants, types, and functions for the mt5cli package.
TICK_FLAG_MAP
module-attribute
¶
TIMEFRAME_MAP
module-attribute
¶
TIMEFRAME_MAP: dict[str, int] = {
"M1": 1,
"M2": 2,
"M3": 3,
"M4": 4,
"M5": 5,
"M6": 6,
"M10": 10,
"M12": 12,
"M15": 15,
"M20": 20,
"M30": 30,
"H1": 16385,
"H2": 16386,
"H3": 16387,
"H4": 16388,
"H6": 16390,
"H8": 16392,
"H12": 16396,
"D1": 16408,
"W1": 32769,
"MN1": 49153,
}
Dataset ¶
Bases: StrEnum
Datasets supported by the collect-history command.
IfExists ¶
LogLevel ¶
OutputFormat ¶
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
|
str
|
Tick flag name (ALL, INFO, TRADE) or integer value. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Integer tick flag value. |
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
|
str
|
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. |