Schemas¶
mt5cli.schemas ¶
Canonical DataFrame schemas for MT5 market and account datasets.
DEDUP_KEYS
module-attribute
¶
DEDUP_KEYS: dict[DataKind, tuple[tuple[str, ...], ...]] = {
rates: (
("symbol", "timeframe", "time"),
("symbol", "time"),
),
ticks: (("symbol", "time_msc"), ("symbol", "time")),
history_orders: (
("ticket",),
("symbol", "time", "type"),
),
history_deals: (
("ticket",),
("symbol", "time", "type", "entry"),
),
}
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]] = {
rates: frozenset({
"time",
"open",
"high",
"low",
"close",
"tick_volume",
"spread",
"real_volume",
}),
ticks: frozenset({
"time",
"bid",
"ask",
"last",
"volume",
"time_msc",
"flags",
"volume_real",
}),
orders: frozenset({
"ticket",
"time_setup",
"type",
"state",
"symbol",
"volume_current",
"price_open",
}),
positions: frozenset({
"ticket",
"time",
"type",
"symbol",
"volume",
"price_open",
"price_current",
"profit",
}),
history_orders: frozenset({
"ticket",
"time_setup",
"type",
"state",
"symbol",
"volume_initial",
"price_open",
}),
history_deals: frozenset({
"ticket",
"order",
"time",
"type",
"entry",
"symbol",
"volume",
"price",
"profit",
}),
}
TIME_COLUMNS
module-attribute
¶
TIME_COLUMNS: dict[DataKind, frozenset[str]] = {
kind: (
REQUIRED_COLUMNS[kind] & _TIME_COLUMN_NAMES
| 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 columns coerced to UTC datetimes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame
|
DataFrame
|
Source DataFrame. |
required |
columns
|
Iterable[str]
|
Column names to coerce. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame copy with UTC-aware datetime columns. |
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.
Ensures UTC timestamps, optionally injects symbol / timeframe for
storage-oriented datasets, and sorts chronologically when a time column
exists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame
|
DataFrame
|
Source DataFrame from MT5 or pdmt5. |
required |
kind
|
DataKind
|
Dataset kind guiding normalization rules. |
required |
symbol
|
str | None
|
Optional symbol to inject when missing. |
None
|
timeframe
|
int | str | None
|
Optional timeframe integer or name to inject for rates. |
None
|
sort
|
bool
|
Whether to sort by |
True
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
Normalized DataFrame copy. |
Source code in mt5cli/schemas.py
normalize_time_columns ¶
normalize_time_columns(
frame: DataFrame, kind: DataKind
) -> DataFrame
Coerce dataset time columns to UTC-aware datetimes when present.
Any column in :data:KNOWN_MT5_TIME_COLUMNS that is present in frame
is normalized. Numeric MT5 epoch values use seconds for time,
time_setup, and time_done, and milliseconds for *_msc columns.
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
schema_columns ¶
schema_columns(kind: DataKind) -> frozenset[str]
Return required column names for a dataset kind.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kind
|
DataKind
|
Dataset kind. |
required |
Returns:
| Type | Description |
|---|---|
frozenset[str]
|
Required column names for |
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.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame
|
DataFrame
|
DataFrame to validate. |
required |
kind
|
DataKind
|
Expected dataset kind. |
required |
extra_required
|
Iterable[str] | None
|
Additional columns that must be present (for example
|
None
|
Raises:
| Type | Description |
|---|---|
Mt5SchemaError
|
If required columns are missing. |