Schemas¶
mt5cli.schemas ¶
Canonical DataFrame schemas for MT5 market and account datasets.
DEDUP_KEYS
module-attribute
¶
DEDUP_KEYS: dict[DataKind, tuple[tuple[str, ...], ...]] = {
DataKind.rates: (
("symbol", "timeframe", "time"),
("symbol", "time"),
),
DataKind.ticks: (
("symbol", "time_msc"),
("symbol", "time"),
),
DataKind.history_orders: (
("ticket",),
("symbol", "time", "type"),
),
DataKind.history_deals: (
("ticket",),
("symbol", "time", "type", "entry"),
),
DataKind.symbols: (("symbol", "time"),),
}
KNOWN_MT5_TIME_COLUMNS
module-attribute
¶
KNOWN_MT5_TIME_COLUMNS: Final[frozenset[str]] = frozenset({
"time",
"time_setup",
"time_setup_msc",
"time_done",
"time_done_msc",
"time_msc",
})
REQUIRED_COLUMNS
module-attribute
¶
REQUIRED_COLUMNS: dict[DataKind, frozenset[str]] = {
DataKind.rates: frozenset({
"time",
"open",
"high",
"low",
"close",
"tick_volume",
"spread",
"real_volume",
}),
DataKind.ticks: frozenset({
"time",
"bid",
"ask",
"last",
"volume",
"time_msc",
"flags",
"volume_real",
}),
DataKind.orders: frozenset({
"ticket",
"time_setup",
"type",
"state",
"symbol",
"volume_current",
"price_open",
}),
DataKind.positions: frozenset({
"ticket",
"time",
"type",
"symbol",
"volume",
"price_open",
"price_current",
"profit",
}),
DataKind.history_orders: frozenset({
"ticket",
"time_setup",
"type",
"state",
"symbol",
"volume_initial",
"price_open",
}),
DataKind.history_deals: frozenset({
"ticket",
"order",
"time",
"type",
"entry",
"symbol",
"volume",
"price",
"profit",
}),
DataKind.symbols: frozenset({
"symbol",
"time",
"point",
"digits",
"trade_contract_size",
"volume_min",
"volume_max",
"volume_step",
"trade_tick_size",
"trade_tick_value",
"currency_profit",
}),
}
TIME_COLUMNS
module-attribute
¶
TIME_COLUMNS: dict[DataKind, frozenset[str]] = {
kind: REQUIRED_COLUMNS[kind] & _TIME_COLUMN_NAMES
| _OPTIONAL_TIME_COLUMNS_BY_KIND.get(kind, frozenset())
for kind in DataKind
}
__all__
module-attribute
¶
__all__ = [
"DEDUP_KEYS",
"KNOWN_MT5_TIME_COLUMNS",
"REQUIRED_COLUMNS",
"TIME_COLUMNS",
"DataKind",
"normalize_dataframe",
"normalize_time_columns",
"schema_columns",
"validate_schema",
]
DataKind ¶
Bases: StrEnum
Supported MT5 dataset kinds with canonical column contracts.
ensure_utc_columns ¶
Return a copy with selected datetime columns normalized safely.
Known MT5 time columns preserve the pdmt5 server-wall-clock contract rather than being relabeled as UTC. Other columns are interpreted as generic datetimes and coerced to UTC.
Source code in mt5cli/schemas.py
normalize_dataframe ¶
normalize_dataframe(
frame: DataFrame,
kind: DataKind,
*,
symbol: str | None = None,
timeframe: int | str | None = None,
sort: bool = True,
) -> DataFrame
Normalize MT5 DataFrame columns, timestamps, and storage metadata.
Preserves timezone-naive trade-server wall-clock timestamps from pdmt5,
normalizes explicitly aware timestamps to UTC, optionally injects
symbol / timeframe metadata, and sorts chronologically.
Returns:
| Type | Description |
|---|---|
DataFrame
|
Normalized DataFrame with canonical columns and ordering. |
Source code in mt5cli/schemas.py
normalize_time_columns ¶
normalize_time_columns(
frame: DataFrame, kind: DataKind
) -> DataFrame
Normalize dataset time columns while preserving MT5 server-time semantics.
Any column in :data:KNOWN_MT5_TIME_COLUMNS that is present in frame
is parsed. Timezone-naive values remain timezone-naive MT5 trade-server
wall-clock labels. Explicitly aware values are normalized to UTC. Numeric
fields use seconds for time, time_setup, and time_done, and
milliseconds for *_msc columns, producing naive values consistent with
pdmt5's conversion contract.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame
|
DataFrame
|
Source DataFrame from MT5 or pdmt5. |
required |
kind
|
DataKind
|
Dataset kind (retained for API compatibility). |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame copy with normalized time columns. |
Source code in mt5cli/schemas.py
validate_schema ¶
validate_schema(
frame: DataFrame,
kind: DataKind,
*,
extra_required: Iterable[str] | None = None,
) -> None
Validate that a DataFrame includes required columns for a dataset kind.
Raises:
| Type | Description |
|---|---|
Mt5SchemaError
|
If required columns are missing. |