Converters¶
mt5cli.converters ¶
Shared conversion helpers for MT5 symbols, timeframes, and date ranges.
__all__
module-attribute
¶
__all__ = [
"ensure_trade_server_time",
"ensure_utc",
"granularity_name",
"normalize_symbol",
"normalize_symbols",
"parse_date_range",
"parse_datetime",
"parse_tick_flags",
"parse_timeframe",
"recent_window",
]
ensure_trade_server_time ¶
Return a timezone-naive MT5 trade-server wall-clock datetime.
ISO 8601 strings without an offset are parsed as naive wall-clock values. Datetime objects and strings carrying timezone information are rejected; mt5cli has no broker timezone with which to convert them safely.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
datetime | str
|
Naive datetime or offset-free ISO 8601 string. |
required |
Returns:
| Type | Description |
|---|---|
datetime
|
The timezone-naive trade-server wall-clock datetime. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the value is invalid or timezone-aware. |
Source code in mt5cli/converters.py
ensure_utc ¶
Return a timezone-aware UTC datetime without guessing a timezone.
ISO-8601 strings continue to use :func:parse_datetime, whose documented
input contract treats a missing offset as UTC for CLI/user input. Datetime
objects are stricter: a naive object has no timezone information and may be
a pdmt5 trade-server wall-clock value, so attaching UTC would silently
change its meaning.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
datetime | str
|
Datetime instance or ISO 8601 string. |
required |
Returns:
| Type | Description |
|---|---|
datetime
|
UTC-aware datetime. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If a datetime object is timezone-naive. |
Source code in mt5cli/converters.py
granularity_name ¶
Return a short granularity label for a timeframe integer or name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timeframe
|
int | str
|
MT5 timeframe as integer or name (for example |
required |
Returns:
| Type | Description |
|---|---|
str
|
Short name such as |
Source code in mt5cli/converters.py
normalize_symbol ¶
Normalize a broker symbol name for MT5 API calls.
Strips surrounding whitespace while preserving broker-specific casing and
suffixes (for example XAUUSDm, US500.cash, or EURUSD.r).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
Raw symbol name. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Normalized symbol string. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the symbol is empty after normalization. |
Source code in mt5cli/converters.py
normalize_symbols ¶
Normalize a sequence of broker symbol names.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbols
|
Sequence[str]
|
Raw symbol names. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
List of normalized, de-duplicated symbols preserving first-seen order. |
Source code in mt5cli/converters.py
parse_date_range ¶
Parse and validate an inclusive trade-server wall-clock range.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
date_from
|
datetime | str
|
Range start as datetime or ISO 8601 string. |
required |
date_to
|
datetime | str
|
Range end as datetime or ISO 8601 string. |
required |
Returns:
| Type | Description |
|---|---|
tuple[datetime, datetime]
|
Tuple of timezone-naive |
Raises:
| Type | Description |
|---|---|
ValueError
|
If either bound is timezone-aware or |
Source code in mt5cli/converters.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_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. |
Source code in mt5cli/utils.py
recent_window ¶
recent_window(
*,
hours: float | None = None,
seconds: float | None = None,
date_to: datetime | str | None = None,
) -> tuple[datetime, datetime]
Build a trailing trade-server wall-clock window.
Exactly one of hours or seconds must be provided.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hours
|
float | None
|
Trailing window length in hours. |
None
|
seconds
|
float | None
|
Trailing window length in seconds. |
None
|
date_to
|
datetime | str | None
|
Required naive MT5 trade-server window end. |
None
|
Returns:
| Type | Description |
|---|---|
tuple[datetime, datetime]
|
Tuple of timezone-naive |
Raises:
| Type | Description |
|---|---|
ValueError
|
If neither or both window lengths are provided, a length is
not positive, |