Converters¶
mt5cli.converters ¶
Shared conversion helpers for MT5 symbols, timeframes, and date ranges.
__all__
module-attribute
¶
__all__ = [
"ensure_utc",
"granularity_name",
"normalize_symbol",
"normalize_symbols",
"parse_date_range",
"parse_datetime",
"parse_tick_flags",
"parse_timeframe",
"recent_window",
]
ensure_utc ¶
Return a timezone-aware UTC datetime.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
datetime | str
|
Datetime instance or ISO 8601 string. |
required |
Returns:
| Type | Description |
|---|---|
datetime
|
UTC-aware datetime. |
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 UTC date 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 UTC-aware |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
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 UTC window ending at date_to or now.
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
|
Window end. Defaults to current UTC time. |
None
|
Returns:
| Type | Description |
|---|---|
tuple[datetime, datetime]
|
Tuple of UTC-aware |
Raises:
| Type | Description |
|---|---|
ValueError
|
If neither or both window lengths are provided, or if a length is not positive. |