DataFrame¶
pdmt5.dataframe ¶
MetaTrader5 data client with pandas DataFrame conversion.
Mt5Config ¶
Mt5DataClient ¶
Bases: Mt5Client
MetaTrader5 data client with pandas DataFrame and dictionary conversion.
This class provides a pandas-friendly interface to MetaTrader5 functions, converting native MetaTrader5 data structures to pandas DataFrames with pydantic validation.
config
class-attribute
instance-attribute
¶
config: Mt5Config = Field(
default_factory=Mt5Config,
description="MetaTrader5 connection configuration",
)
model_config
class-attribute
instance-attribute
¶
retry_count
class-attribute
instance-attribute
¶
retry_count: int = Field(
default=3,
ge=0,
description="Number of retry attempts for connection initialization",
)
__enter__ ¶
Context manager entry using config-aware initialization.
Returns:
| Type | Description |
|---|---|
Self
|
The initialized client instance. |
account_info_as_df ¶
Get info on the current account as a data frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index_keys
|
str | None
|
Column name to set as index if provided. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with account information. |
Source code in pdmt5/dataframe.py
account_info_as_dict ¶
Get info on the current account as a dictionary.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with account information. |
copy_rates_from_as_df ¶
copy_rates_from_as_df(
symbol: str,
timeframe: int,
date_from: datetime,
count: int,
skip_to_datetime: bool = False,
index_keys: str | None = None,
) -> DataFrame
Get bars for a specified date range as a data frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
Symbol name. |
required |
timeframe
|
int
|
Timeframe constant. |
required |
date_from
|
datetime
|
Start date in trade-server time (not UTC). |
required |
count
|
int
|
Number of rates to retrieve. |
required |
skip_to_datetime
|
bool
|
Whether to skip converting time to datetime. |
False
|
index_keys
|
str | None
|
Column name to set as index if provided. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with OHLCV data. |
Source code in pdmt5/dataframe.py
copy_rates_from_as_dicts ¶
copy_rates_from_as_dicts(
symbol: str,
timeframe: int,
date_from: datetime,
count: int,
skip_to_datetime: bool = False,
) -> list[dict[str, Any]]
Get bars for a specified date range as a list of dictionaries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
Symbol name. |
required |
timeframe
|
int
|
Timeframe constant. |
required |
date_from
|
datetime
|
Start date in trade-server time (not UTC). |
required |
count
|
int
|
Number of rates to retrieve. |
required |
skip_to_datetime
|
bool
|
Whether to skip converting time to datetime. |
False
|
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
List of dictionaries with OHLCV data. |
Source code in pdmt5/dataframe.py
copy_rates_from_pos_as_df ¶
copy_rates_from_pos_as_df(
symbol: str,
timeframe: int,
start_pos: int,
count: int,
skip_to_datetime: bool = False,
index_keys: str | None = None,
) -> DataFrame
Get bars from a specified position as a data frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
Symbol name. |
required |
timeframe
|
int
|
Timeframe constant. |
required |
start_pos
|
int
|
Start position. |
required |
count
|
int
|
Number of rates to retrieve. |
required |
skip_to_datetime
|
bool
|
Whether to skip converting time to datetime. |
False
|
index_keys
|
str | None
|
Column name to set as index if provided. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with OHLCV data. |
Source code in pdmt5/dataframe.py
copy_rates_from_pos_as_dicts ¶
copy_rates_from_pos_as_dicts(
symbol: str,
timeframe: int,
start_pos: int,
count: int,
skip_to_datetime: bool = False,
) -> list[dict[str, Any]]
Get bars from a specified position as a list of dictionaries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
Symbol name. |
required |
timeframe
|
int
|
Timeframe constant. |
required |
start_pos
|
int
|
Start position. |
required |
count
|
int
|
Number of rates to retrieve. |
required |
skip_to_datetime
|
bool
|
Whether to skip converting time to datetime. |
False
|
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
List of dictionaries with OHLCV data. |
Source code in pdmt5/dataframe.py
copy_rates_range_as_df ¶
copy_rates_range_as_df(
symbol: str,
timeframe: int,
date_from: datetime,
date_to: datetime,
skip_to_datetime: bool = False,
index_keys: str | None = None,
) -> DataFrame
Get bars for a specified date range as a data frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
Symbol name. |
required |
timeframe
|
int
|
Timeframe constant. |
required |
date_from
|
datetime
|
Start date in trade-server time (not UTC). |
required |
date_to
|
datetime
|
End date in trade-server time (not UTC). |
required |
skip_to_datetime
|
bool
|
Whether to skip converting time to datetime. |
False
|
index_keys
|
str | None
|
Column name to set as index if provided. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with OHLCV data. |
Source code in pdmt5/dataframe.py
copy_rates_range_as_dicts ¶
copy_rates_range_as_dicts(
symbol: str,
timeframe: int,
date_from: datetime,
date_to: datetime,
skip_to_datetime: bool = False,
) -> list[dict[str, Any]]
Get bars for a specified date range as a list of dictionaries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
Symbol name. |
required |
timeframe
|
int
|
Timeframe constant. |
required |
date_from
|
datetime
|
Start date in trade-server time (not UTC). |
required |
date_to
|
datetime
|
End date in trade-server time (not UTC). |
required |
skip_to_datetime
|
bool
|
Whether to skip converting time to datetime. |
False
|
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
List of dictionaries with OHLCV data. |
Source code in pdmt5/dataframe.py
copy_ticks_from_as_df ¶
copy_ticks_from_as_df(
symbol: str,
date_from: datetime,
count: int,
flags: int,
skip_to_datetime: bool = False,
index_keys: str | None = None,
) -> DataFrame
Get ticks from a specified date as a data frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
Symbol name. |
required |
date_from
|
datetime
|
Start date in trade-server time (not UTC). |
required |
count
|
int
|
Number of ticks to retrieve. |
required |
flags
|
int
|
Tick flags (use constants from MetaTrader5). |
required |
skip_to_datetime
|
bool
|
Whether to skip converting time to datetime. |
False
|
index_keys
|
str | None
|
Column name to set as index if provided. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with tick data. |
Source code in pdmt5/dataframe.py
copy_ticks_from_as_dicts ¶
copy_ticks_from_as_dicts(
symbol: str,
date_from: datetime,
count: int,
flags: int,
skip_to_datetime: bool = False,
) -> list[dict[str, Any]]
Get ticks from a specified date as a list of dictionaries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
Symbol name. |
required |
date_from
|
datetime
|
Start date in trade-server time (not UTC). |
required |
count
|
int
|
Number of ticks to retrieve. |
required |
flags
|
int
|
Tick flags (use constants from MetaTrader5). |
required |
skip_to_datetime
|
bool
|
Whether to skip converting time to datetime. |
False
|
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
List of dictionaries with tick data. |
Source code in pdmt5/dataframe.py
copy_ticks_range_as_df ¶
copy_ticks_range_as_df(
symbol: str,
date_from: datetime,
date_to: datetime,
flags: int,
skip_to_datetime: bool = False,
index_keys: str | None = None,
) -> DataFrame
Get ticks for a specified date range as a data frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
Symbol name. |
required |
date_from
|
datetime
|
Start date in trade-server time (not UTC). |
required |
date_to
|
datetime
|
End date in trade-server time (not UTC). |
required |
flags
|
int
|
Tick flags (use constants from MetaTrader5). |
required |
skip_to_datetime
|
bool
|
Whether to skip converting time to datetime. |
False
|
index_keys
|
str | None
|
Column name to set as index if provided. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with tick data. |
Source code in pdmt5/dataframe.py
copy_ticks_range_as_dicts ¶
copy_ticks_range_as_dicts(
symbol: str,
date_from: datetime,
date_to: datetime,
flags: int,
skip_to_datetime: bool = False,
) -> list[dict[str, Any]]
Get ticks for a specified date range as a list of dictionaries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
Symbol name. |
required |
date_from
|
datetime
|
Start date in trade-server time (not UTC). |
required |
date_to
|
datetime
|
End date in trade-server time (not UTC). |
required |
flags
|
int
|
Tick flags (use constants from MetaTrader5). |
required |
skip_to_datetime
|
bool
|
Whether to skip converting time to datetime. |
False
|
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
List of dictionaries with tick data. |
Source code in pdmt5/dataframe.py
history_deals_get_as_df ¶
history_deals_get_as_df(
date_from: datetime | None = None,
date_to: datetime | None = None,
group: str | None = None,
symbol: str | None = None,
ticket: int | None = None,
position: int | None = None,
skip_to_datetime: bool = False,
index_keys: str | None = None,
) -> DataFrame
Get historical deals with optional filters as a data frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
date_from
|
datetime | None
|
Start date in trade-server time, not UTC (required if not using ticket/position). |
None
|
date_to
|
datetime | None
|
End date in trade-server time, not UTC (required if not using ticket/position). |
None
|
group
|
str | None
|
Optional group filter. Mutually exclusive with symbol. |
None
|
symbol
|
str | None
|
Optional symbol filter matching the symbol name exactly. Mutually exclusive with group. |
None
|
ticket
|
int | None
|
Get deals by order ticket. |
None
|
position
|
int | None
|
Get deals by position ticket. |
None
|
skip_to_datetime
|
bool
|
Whether to skip converting time to datetime. |
False
|
index_keys
|
str | None
|
Column name to set as index if provided. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with historical deal information. |
Source code in pdmt5/dataframe.py
history_deals_get_as_dicts ¶
history_deals_get_as_dicts(
date_from: datetime | None = None,
date_to: datetime | None = None,
group: str | None = None,
symbol: str | None = None,
ticket: int | None = None,
position: int | None = None,
skip_to_datetime: bool = False,
) -> list[dict[str, Any]]
Get historical deals with optional filters as a list of dictionaries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
date_from
|
datetime | None
|
Start date in trade-server time, not UTC (required if not using ticket/position). |
None
|
date_to
|
datetime | None
|
End date in trade-server time, not UTC (required if not using ticket/position). |
None
|
group
|
str | None
|
Optional group filter. Mutually exclusive with symbol. |
None
|
symbol
|
str | None
|
Optional symbol filter matching the symbol name exactly. Mutually exclusive with group. |
None
|
ticket
|
int | None
|
Get deals by order ticket. |
None
|
position
|
int | None
|
Get deals by position ticket. |
None
|
skip_to_datetime
|
bool
|
Whether to skip converting time to datetime. |
False
|
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
List of dictionaries with historical deal information. |
Source code in pdmt5/dataframe.py
history_orders_get_as_df ¶
history_orders_get_as_df(
date_from: datetime | None = None,
date_to: datetime | None = None,
group: str | None = None,
symbol: str | None = None,
ticket: int | None = None,
position: int | None = None,
skip_to_datetime: bool = False,
index_keys: str | None = None,
) -> DataFrame
Get historical orders with optional filters as a data frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
date_from
|
datetime | None
|
Start date in trade-server time, not UTC (required if not using ticket/position). |
None
|
date_to
|
datetime | None
|
End date in trade-server time, not UTC (required if not using ticket/position). |
None
|
group
|
str | None
|
Optional group filter. Mutually exclusive with symbol. |
None
|
symbol
|
str | None
|
Optional symbol filter matching the symbol name exactly. Mutually exclusive with group. |
None
|
ticket
|
int | None
|
Get orders by ticket. |
None
|
position
|
int | None
|
Get orders by position. |
None
|
skip_to_datetime
|
bool
|
Whether to skip converting time to datetime. |
False
|
index_keys
|
str | None
|
Column name to set as index if provided. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with historical order information. |
Source code in pdmt5/dataframe.py
history_orders_get_as_dicts ¶
history_orders_get_as_dicts(
date_from: datetime | None = None,
date_to: datetime | None = None,
group: str | None = None,
symbol: str | None = None,
ticket: int | None = None,
position: int | None = None,
skip_to_datetime: bool = False,
) -> list[dict[str, Any]]
Get historical orders with optional filters as a list of dictionaries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
date_from
|
datetime | None
|
Start date in trade-server time, not UTC (required if not using ticket/position). |
None
|
date_to
|
datetime | None
|
End date in trade-server time, not UTC (required if not using ticket/position). |
None
|
group
|
str | None
|
Optional group filter. Mutually exclusive with symbol. |
None
|
symbol
|
str | None
|
Optional symbol filter matching the symbol name exactly. Mutually exclusive with group. |
None
|
ticket
|
int | None
|
Get orders by ticket. |
None
|
position
|
int | None
|
Get orders by position. |
None
|
skip_to_datetime
|
bool
|
Whether to skip converting time to datetime. |
False
|
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
List of dictionaries with historical order information. |
Source code in pdmt5/dataframe.py
initialize_and_login_mt5 ¶
initialize_and_login_mt5(
path: str | None = None,
login: int | None = None,
password: str | SecretStr | None = None,
server: str | None = None,
timeout: int | None = None,
) -> None
Initialize MT5 and ensure the requested account is active.
MetaTrader5.initialize() accepts account credentials and normally
connects directly to the requested account. After initialization, this
method verifies the active account and only calls login() when the
terminal reports a different account, avoiding redundant authentication
while preserving account-switch fallback behavior.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | None
|
Path to terminal EXE file (overrides config). |
None
|
login
|
int | None
|
Account login (overrides config). |
None
|
password
|
str | SecretStr | None
|
Account password (overrides config). |
None
|
server
|
str | None
|
Server name (overrides config). |
None
|
timeout
|
int | None
|
Connection timeout (overrides config). |
None
|
Raises:
| Type | Description |
|---|---|
Mt5RuntimeError
|
If initialization or account selection fails after retries. |
Source code in pdmt5/dataframe.py
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | |
last_error_as_df ¶
Get the last error information as a data frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index_keys
|
str | None
|
Column name to set as index if provided. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with last error information. |
Source code in pdmt5/dataframe.py
last_error_as_dict ¶
Get the last error information as a dictionary.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with last error information. |
Source code in pdmt5/dataframe.py
market_book_get_as_df ¶
market_book_get_as_df(
symbol: str,
skip_to_datetime: bool = False,
index_keys: str | None = None,
) -> DataFrame
Get market depth for a specified symbol as a data frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
Symbol name. |
required |
skip_to_datetime
|
bool
|
Whether to skip converting time to datetime. |
False
|
index_keys
|
str | None
|
Column name to set as index if provided. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with market depth data. |
Source code in pdmt5/dataframe.py
market_book_get_as_dicts ¶
Get market depth for a specified symbol as a list of dictionaries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
Symbol name. |
required |
skip_to_datetime
|
bool
|
Whether to skip converting time to datetime. |
False
|
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
List of dictionaries with market depth data. |
Source code in pdmt5/dataframe.py
order_check_as_df ¶
Check funds sufficiency for performing a requested trading operation as a data frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
dict[str, Any]
|
Order request parameters. |
required |
index_keys
|
str | None
|
Column name to set as index if provided. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with order check results. |
Source code in pdmt5/dataframe.py
order_check_as_dict ¶
Check funds sufficiency for performing a requested trading operation as a dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
dict[str, Any]
|
Order request parameters. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with order check results. |
Source code in pdmt5/dataframe.py
order_send_as_df ¶
Send a request to perform a trading operation from the terminal to the trade server as a data frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
dict[str, Any]
|
Order request parameters. |
required |
index_keys
|
str | None
|
Column name to set as index if provided. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with order send results. |
Source code in pdmt5/dataframe.py
order_send_as_dict ¶
Send a request to perform a trading operation from the terminal to the trade server as a dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
dict[str, Any]
|
Order request parameters. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with order send results. |
Source code in pdmt5/dataframe.py
orders_get_as_df ¶
orders_get_as_df(
symbol: str | None = None,
group: str | None = None,
ticket: int | None = None,
skip_to_datetime: bool = False,
index_keys: str | None = None,
) -> DataFrame
Get active orders with optional filters as a data frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str | None
|
Optional symbol filter. Mutually exclusive with group and ticket. |
None
|
group
|
str | None
|
Optional group filter. Mutually exclusive with symbol and ticket. |
None
|
ticket
|
int | None
|
Optional order ticket filter. Mutually exclusive with symbol and group. |
None
|
skip_to_datetime
|
bool
|
Whether to skip converting time to datetime. |
False
|
index_keys
|
str | None
|
Column name to set as index if provided. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with order information or empty DataFrame if no orders. |
Source code in pdmt5/dataframe.py
orders_get_as_dicts ¶
orders_get_as_dicts(
symbol: str | None = None,
group: str | None = None,
ticket: int | None = None,
skip_to_datetime: bool = False,
) -> list[dict[str, Any]]
Get active orders with optional filters as a list of dictionaries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str | None
|
Optional symbol filter. Mutually exclusive with group and ticket. |
None
|
group
|
str | None
|
Optional group filter. Mutually exclusive with symbol and ticket. |
None
|
ticket
|
int | None
|
Optional order ticket filter. Mutually exclusive with symbol and group. |
None
|
skip_to_datetime
|
bool
|
Whether to skip converting time to datetime. |
False
|
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
List of dictionaries with order information or empty list if no orders. |
Source code in pdmt5/dataframe.py
positions_get_as_df ¶
positions_get_as_df(
symbol: str | None = None,
group: str | None = None,
ticket: int | None = None,
skip_to_datetime: bool = False,
index_keys: str | None = None,
) -> DataFrame
Get open positions with optional filters as a data frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str | None
|
Optional symbol filter. Mutually exclusive with group and ticket. |
None
|
group
|
str | None
|
Optional group filter. Mutually exclusive with symbol and ticket. |
None
|
ticket
|
int | None
|
Optional position ticket filter. Mutually exclusive with symbol and group. |
None
|
skip_to_datetime
|
bool
|
Whether to skip converting time to datetime. |
False
|
index_keys
|
str | None
|
Column name to set as index if provided. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with position information or empty DataFrame if no positions. |
Source code in pdmt5/dataframe.py
positions_get_as_dicts ¶
positions_get_as_dicts(
symbol: str | None = None,
group: str | None = None,
ticket: int | None = None,
skip_to_datetime: bool = False,
) -> list[dict[str, Any]]
Get open positions with optional filters as a list of dictionaries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str | None
|
Optional symbol filter. Mutually exclusive with group and ticket. |
None
|
group
|
str | None
|
Optional group filter. Mutually exclusive with symbol and ticket. |
None
|
ticket
|
int | None
|
Optional position ticket filter. Mutually exclusive with symbol and group. |
None
|
skip_to_datetime
|
bool
|
Whether to skip converting time to datetime. |
False
|
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
List of dictionaries with position information or empty list if no |
list[dict[str, Any]]
|
positions. |
Source code in pdmt5/dataframe.py
symbol_info_as_df ¶
symbol_info_as_df(
symbol: str,
skip_to_datetime: bool = False,
index_keys: str | None = None,
) -> DataFrame
Get data on a specific symbol as a data frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
Symbol name. |
required |
skip_to_datetime
|
bool
|
Whether to skip converting time to datetime. |
False
|
index_keys
|
str | None
|
Column name to set as index if provided. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with symbol information. |
Source code in pdmt5/dataframe.py
symbol_info_as_dict ¶
Get data on a specific symbol as a dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
Symbol name. |
required |
skip_to_datetime
|
bool
|
Whether to skip converting time to datetime. |
False
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with symbol information. |
Source code in pdmt5/dataframe.py
symbol_info_tick_as_df ¶
symbol_info_tick_as_df(
symbol: str,
skip_to_datetime: bool = False,
index_keys: str | None = None,
) -> DataFrame
Get the last tick for the specified financial instrument as a data frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
Symbol name. |
required |
skip_to_datetime
|
bool
|
Whether to skip converting time to datetime. |
False
|
index_keys
|
str | None
|
Column name to set as index if provided. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with tick information. |
Source code in pdmt5/dataframe.py
symbol_info_tick_as_dict ¶
Get the last tick for the specified financial instrument as a dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
Symbol name. |
required |
skip_to_datetime
|
bool
|
Whether to skip converting time to datetime. |
False
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with tick information. |
Source code in pdmt5/dataframe.py
symbols_get_as_df ¶
symbols_get_as_df(
group: str | None = None,
skip_to_datetime: bool = False,
index_keys: str | None = None,
) -> DataFrame
Get symbols as a data frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
group
|
str | None
|
Symbol group filter (e.g., "USD", "Forex*"). |
None
|
skip_to_datetime
|
bool
|
Whether to skip converting time to datetime. |
False
|
index_keys
|
str | None
|
Column name to set as index if provided. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with symbol information. |
Source code in pdmt5/dataframe.py
symbols_get_as_dicts ¶
symbols_get_as_dicts(
group: str | None = None, skip_to_datetime: bool = False
) -> list[dict[str, Any]]
Get symbols as a list of dictionaries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
group
|
str | None
|
Symbol group filter (e.g., "USD", "Forex*"). |
None
|
skip_to_datetime
|
bool
|
Whether to skip converting time to datetime. |
False
|
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
List of dictionaries with symbol information. |
Source code in pdmt5/dataframe.py
terminal_info_as_df ¶
Get the connected terminal status and settings as a data frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index_keys
|
str | None
|
Column name to set as index if provided. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with terminal information. |
Source code in pdmt5/dataframe.py
terminal_info_as_dict ¶
Get the connected terminal status and settings as a dictionary.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with terminal information. |
version_as_df ¶
Return MetaTrader5 version information as a data frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index_keys
|
str | None
|
Column name to set as index if provided. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with MetaTrader5 version information. |
Source code in pdmt5/dataframe.py
version_as_dict ¶
Return MetaTrader5 version information as a dictionary.
Returns:
| Type | Description |
|---|---|
dict[str, int | str]
|
Dictionary with MetaTrader5 version information. |
Source code in pdmt5/dataframe.py
Overview¶
The dataframe module extends the base Mt5Client with pandas-friendly functionality for connecting to MetaTrader 5 and retrieving trading data as pandas DataFrames. It includes configuration management, data conversion helpers, and comprehensive validation utilities.
Use the _as_df/_as_dict methods for pandas conversions; base methods return
raw MT5 structures.
Classes¶
Mt5Config¶
pdmt5.dataframe.Mt5Config ¶
options: show_bases: false
Configuration class for MetaTrader 5 connection parameters using pydantic for validation.
Mt5DataClient¶
pdmt5.dataframe.Mt5DataClient ¶
Bases: Mt5Client
MetaTrader5 data client with pandas DataFrame and dictionary conversion.
This class provides a pandas-friendly interface to MetaTrader5 functions, converting native MetaTrader5 data structures to pandas DataFrames with pydantic validation.
config
class-attribute
instance-attribute
¶
config: Mt5Config = Field(
default_factory=Mt5Config,
description="MetaTrader5 connection configuration",
)
model_config
class-attribute
instance-attribute
¶
retry_count
class-attribute
instance-attribute
¶
retry_count: int = Field(
default=3,
ge=0,
description="Number of retry attempts for connection initialization",
)
__enter__ ¶
Context manager entry using config-aware initialization.
Returns:
| Type | Description |
|---|---|
Self
|
The initialized client instance. |
account_info_as_df ¶
Get info on the current account as a data frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index_keys
|
str | None
|
Column name to set as index if provided. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with account information. |
Source code in pdmt5/dataframe.py
account_info_as_dict ¶
Get info on the current account as a dictionary.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with account information. |
copy_rates_from_as_df ¶
copy_rates_from_as_df(
symbol: str,
timeframe: int,
date_from: datetime,
count: int,
skip_to_datetime: bool = False,
index_keys: str | None = None,
) -> DataFrame
Get bars for a specified date range as a data frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
Symbol name. |
required |
timeframe
|
int
|
Timeframe constant. |
required |
date_from
|
datetime
|
Start date in trade-server time (not UTC). |
required |
count
|
int
|
Number of rates to retrieve. |
required |
skip_to_datetime
|
bool
|
Whether to skip converting time to datetime. |
False
|
index_keys
|
str | None
|
Column name to set as index if provided. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with OHLCV data. |
Source code in pdmt5/dataframe.py
copy_rates_from_as_dicts ¶
copy_rates_from_as_dicts(
symbol: str,
timeframe: int,
date_from: datetime,
count: int,
skip_to_datetime: bool = False,
) -> list[dict[str, Any]]
Get bars for a specified date range as a list of dictionaries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
Symbol name. |
required |
timeframe
|
int
|
Timeframe constant. |
required |
date_from
|
datetime
|
Start date in trade-server time (not UTC). |
required |
count
|
int
|
Number of rates to retrieve. |
required |
skip_to_datetime
|
bool
|
Whether to skip converting time to datetime. |
False
|
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
List of dictionaries with OHLCV data. |
Source code in pdmt5/dataframe.py
copy_rates_from_pos_as_df ¶
copy_rates_from_pos_as_df(
symbol: str,
timeframe: int,
start_pos: int,
count: int,
skip_to_datetime: bool = False,
index_keys: str | None = None,
) -> DataFrame
Get bars from a specified position as a data frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
Symbol name. |
required |
timeframe
|
int
|
Timeframe constant. |
required |
start_pos
|
int
|
Start position. |
required |
count
|
int
|
Number of rates to retrieve. |
required |
skip_to_datetime
|
bool
|
Whether to skip converting time to datetime. |
False
|
index_keys
|
str | None
|
Column name to set as index if provided. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with OHLCV data. |
Source code in pdmt5/dataframe.py
copy_rates_from_pos_as_dicts ¶
copy_rates_from_pos_as_dicts(
symbol: str,
timeframe: int,
start_pos: int,
count: int,
skip_to_datetime: bool = False,
) -> list[dict[str, Any]]
Get bars from a specified position as a list of dictionaries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
Symbol name. |
required |
timeframe
|
int
|
Timeframe constant. |
required |
start_pos
|
int
|
Start position. |
required |
count
|
int
|
Number of rates to retrieve. |
required |
skip_to_datetime
|
bool
|
Whether to skip converting time to datetime. |
False
|
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
List of dictionaries with OHLCV data. |
Source code in pdmt5/dataframe.py
copy_rates_range_as_df ¶
copy_rates_range_as_df(
symbol: str,
timeframe: int,
date_from: datetime,
date_to: datetime,
skip_to_datetime: bool = False,
index_keys: str | None = None,
) -> DataFrame
Get bars for a specified date range as a data frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
Symbol name. |
required |
timeframe
|
int
|
Timeframe constant. |
required |
date_from
|
datetime
|
Start date in trade-server time (not UTC). |
required |
date_to
|
datetime
|
End date in trade-server time (not UTC). |
required |
skip_to_datetime
|
bool
|
Whether to skip converting time to datetime. |
False
|
index_keys
|
str | None
|
Column name to set as index if provided. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with OHLCV data. |
Source code in pdmt5/dataframe.py
copy_rates_range_as_dicts ¶
copy_rates_range_as_dicts(
symbol: str,
timeframe: int,
date_from: datetime,
date_to: datetime,
skip_to_datetime: bool = False,
) -> list[dict[str, Any]]
Get bars for a specified date range as a list of dictionaries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
Symbol name. |
required |
timeframe
|
int
|
Timeframe constant. |
required |
date_from
|
datetime
|
Start date in trade-server time (not UTC). |
required |
date_to
|
datetime
|
End date in trade-server time (not UTC). |
required |
skip_to_datetime
|
bool
|
Whether to skip converting time to datetime. |
False
|
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
List of dictionaries with OHLCV data. |
Source code in pdmt5/dataframe.py
copy_ticks_from_as_df ¶
copy_ticks_from_as_df(
symbol: str,
date_from: datetime,
count: int,
flags: int,
skip_to_datetime: bool = False,
index_keys: str | None = None,
) -> DataFrame
Get ticks from a specified date as a data frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
Symbol name. |
required |
date_from
|
datetime
|
Start date in trade-server time (not UTC). |
required |
count
|
int
|
Number of ticks to retrieve. |
required |
flags
|
int
|
Tick flags (use constants from MetaTrader5). |
required |
skip_to_datetime
|
bool
|
Whether to skip converting time to datetime. |
False
|
index_keys
|
str | None
|
Column name to set as index if provided. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with tick data. |
Source code in pdmt5/dataframe.py
copy_ticks_from_as_dicts ¶
copy_ticks_from_as_dicts(
symbol: str,
date_from: datetime,
count: int,
flags: int,
skip_to_datetime: bool = False,
) -> list[dict[str, Any]]
Get ticks from a specified date as a list of dictionaries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
Symbol name. |
required |
date_from
|
datetime
|
Start date in trade-server time (not UTC). |
required |
count
|
int
|
Number of ticks to retrieve. |
required |
flags
|
int
|
Tick flags (use constants from MetaTrader5). |
required |
skip_to_datetime
|
bool
|
Whether to skip converting time to datetime. |
False
|
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
List of dictionaries with tick data. |
Source code in pdmt5/dataframe.py
copy_ticks_range_as_df ¶
copy_ticks_range_as_df(
symbol: str,
date_from: datetime,
date_to: datetime,
flags: int,
skip_to_datetime: bool = False,
index_keys: str | None = None,
) -> DataFrame
Get ticks for a specified date range as a data frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
Symbol name. |
required |
date_from
|
datetime
|
Start date in trade-server time (not UTC). |
required |
date_to
|
datetime
|
End date in trade-server time (not UTC). |
required |
flags
|
int
|
Tick flags (use constants from MetaTrader5). |
required |
skip_to_datetime
|
bool
|
Whether to skip converting time to datetime. |
False
|
index_keys
|
str | None
|
Column name to set as index if provided. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with tick data. |
Source code in pdmt5/dataframe.py
copy_ticks_range_as_dicts ¶
copy_ticks_range_as_dicts(
symbol: str,
date_from: datetime,
date_to: datetime,
flags: int,
skip_to_datetime: bool = False,
) -> list[dict[str, Any]]
Get ticks for a specified date range as a list of dictionaries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
Symbol name. |
required |
date_from
|
datetime
|
Start date in trade-server time (not UTC). |
required |
date_to
|
datetime
|
End date in trade-server time (not UTC). |
required |
flags
|
int
|
Tick flags (use constants from MetaTrader5). |
required |
skip_to_datetime
|
bool
|
Whether to skip converting time to datetime. |
False
|
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
List of dictionaries with tick data. |
Source code in pdmt5/dataframe.py
history_deals_get_as_df ¶
history_deals_get_as_df(
date_from: datetime | None = None,
date_to: datetime | None = None,
group: str | None = None,
symbol: str | None = None,
ticket: int | None = None,
position: int | None = None,
skip_to_datetime: bool = False,
index_keys: str | None = None,
) -> DataFrame
Get historical deals with optional filters as a data frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
date_from
|
datetime | None
|
Start date in trade-server time, not UTC (required if not using ticket/position). |
None
|
date_to
|
datetime | None
|
End date in trade-server time, not UTC (required if not using ticket/position). |
None
|
group
|
str | None
|
Optional group filter. Mutually exclusive with symbol. |
None
|
symbol
|
str | None
|
Optional symbol filter matching the symbol name exactly. Mutually exclusive with group. |
None
|
ticket
|
int | None
|
Get deals by order ticket. |
None
|
position
|
int | None
|
Get deals by position ticket. |
None
|
skip_to_datetime
|
bool
|
Whether to skip converting time to datetime. |
False
|
index_keys
|
str | None
|
Column name to set as index if provided. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with historical deal information. |
Source code in pdmt5/dataframe.py
history_deals_get_as_dicts ¶
history_deals_get_as_dicts(
date_from: datetime | None = None,
date_to: datetime | None = None,
group: str | None = None,
symbol: str | None = None,
ticket: int | None = None,
position: int | None = None,
skip_to_datetime: bool = False,
) -> list[dict[str, Any]]
Get historical deals with optional filters as a list of dictionaries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
date_from
|
datetime | None
|
Start date in trade-server time, not UTC (required if not using ticket/position). |
None
|
date_to
|
datetime | None
|
End date in trade-server time, not UTC (required if not using ticket/position). |
None
|
group
|
str | None
|
Optional group filter. Mutually exclusive with symbol. |
None
|
symbol
|
str | None
|
Optional symbol filter matching the symbol name exactly. Mutually exclusive with group. |
None
|
ticket
|
int | None
|
Get deals by order ticket. |
None
|
position
|
int | None
|
Get deals by position ticket. |
None
|
skip_to_datetime
|
bool
|
Whether to skip converting time to datetime. |
False
|
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
List of dictionaries with historical deal information. |
Source code in pdmt5/dataframe.py
history_orders_get_as_df ¶
history_orders_get_as_df(
date_from: datetime | None = None,
date_to: datetime | None = None,
group: str | None = None,
symbol: str | None = None,
ticket: int | None = None,
position: int | None = None,
skip_to_datetime: bool = False,
index_keys: str | None = None,
) -> DataFrame
Get historical orders with optional filters as a data frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
date_from
|
datetime | None
|
Start date in trade-server time, not UTC (required if not using ticket/position). |
None
|
date_to
|
datetime | None
|
End date in trade-server time, not UTC (required if not using ticket/position). |
None
|
group
|
str | None
|
Optional group filter. Mutually exclusive with symbol. |
None
|
symbol
|
str | None
|
Optional symbol filter matching the symbol name exactly. Mutually exclusive with group. |
None
|
ticket
|
int | None
|
Get orders by ticket. |
None
|
position
|
int | None
|
Get orders by position. |
None
|
skip_to_datetime
|
bool
|
Whether to skip converting time to datetime. |
False
|
index_keys
|
str | None
|
Column name to set as index if provided. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with historical order information. |
Source code in pdmt5/dataframe.py
history_orders_get_as_dicts ¶
history_orders_get_as_dicts(
date_from: datetime | None = None,
date_to: datetime | None = None,
group: str | None = None,
symbol: str | None = None,
ticket: int | None = None,
position: int | None = None,
skip_to_datetime: bool = False,
) -> list[dict[str, Any]]
Get historical orders with optional filters as a list of dictionaries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
date_from
|
datetime | None
|
Start date in trade-server time, not UTC (required if not using ticket/position). |
None
|
date_to
|
datetime | None
|
End date in trade-server time, not UTC (required if not using ticket/position). |
None
|
group
|
str | None
|
Optional group filter. Mutually exclusive with symbol. |
None
|
symbol
|
str | None
|
Optional symbol filter matching the symbol name exactly. Mutually exclusive with group. |
None
|
ticket
|
int | None
|
Get orders by ticket. |
None
|
position
|
int | None
|
Get orders by position. |
None
|
skip_to_datetime
|
bool
|
Whether to skip converting time to datetime. |
False
|
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
List of dictionaries with historical order information. |
Source code in pdmt5/dataframe.py
initialize_and_login_mt5 ¶
initialize_and_login_mt5(
path: str | None = None,
login: int | None = None,
password: str | SecretStr | None = None,
server: str | None = None,
timeout: int | None = None,
) -> None
Initialize MT5 and ensure the requested account is active.
MetaTrader5.initialize() accepts account credentials and normally
connects directly to the requested account. After initialization, this
method verifies the active account and only calls login() when the
terminal reports a different account, avoiding redundant authentication
while preserving account-switch fallback behavior.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | None
|
Path to terminal EXE file (overrides config). |
None
|
login
|
int | None
|
Account login (overrides config). |
None
|
password
|
str | SecretStr | None
|
Account password (overrides config). |
None
|
server
|
str | None
|
Server name (overrides config). |
None
|
timeout
|
int | None
|
Connection timeout (overrides config). |
None
|
Raises:
| Type | Description |
|---|---|
Mt5RuntimeError
|
If initialization or account selection fails after retries. |
Source code in pdmt5/dataframe.py
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | |
last_error_as_df ¶
Get the last error information as a data frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index_keys
|
str | None
|
Column name to set as index if provided. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with last error information. |
Source code in pdmt5/dataframe.py
last_error_as_dict ¶
Get the last error information as a dictionary.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with last error information. |
Source code in pdmt5/dataframe.py
market_book_get_as_df ¶
market_book_get_as_df(
symbol: str,
skip_to_datetime: bool = False,
index_keys: str | None = None,
) -> DataFrame
Get market depth for a specified symbol as a data frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
Symbol name. |
required |
skip_to_datetime
|
bool
|
Whether to skip converting time to datetime. |
False
|
index_keys
|
str | None
|
Column name to set as index if provided. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with market depth data. |
Source code in pdmt5/dataframe.py
market_book_get_as_dicts ¶
Get market depth for a specified symbol as a list of dictionaries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
Symbol name. |
required |
skip_to_datetime
|
bool
|
Whether to skip converting time to datetime. |
False
|
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
List of dictionaries with market depth data. |
Source code in pdmt5/dataframe.py
order_check_as_df ¶
Check funds sufficiency for performing a requested trading operation as a data frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
dict[str, Any]
|
Order request parameters. |
required |
index_keys
|
str | None
|
Column name to set as index if provided. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with order check results. |
Source code in pdmt5/dataframe.py
order_check_as_dict ¶
Check funds sufficiency for performing a requested trading operation as a dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
dict[str, Any]
|
Order request parameters. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with order check results. |
Source code in pdmt5/dataframe.py
order_send_as_df ¶
Send a request to perform a trading operation from the terminal to the trade server as a data frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
dict[str, Any]
|
Order request parameters. |
required |
index_keys
|
str | None
|
Column name to set as index if provided. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with order send results. |
Source code in pdmt5/dataframe.py
order_send_as_dict ¶
Send a request to perform a trading operation from the terminal to the trade server as a dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
dict[str, Any]
|
Order request parameters. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with order send results. |
Source code in pdmt5/dataframe.py
orders_get_as_df ¶
orders_get_as_df(
symbol: str | None = None,
group: str | None = None,
ticket: int | None = None,
skip_to_datetime: bool = False,
index_keys: str | None = None,
) -> DataFrame
Get active orders with optional filters as a data frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str | None
|
Optional symbol filter. Mutually exclusive with group and ticket. |
None
|
group
|
str | None
|
Optional group filter. Mutually exclusive with symbol and ticket. |
None
|
ticket
|
int | None
|
Optional order ticket filter. Mutually exclusive with symbol and group. |
None
|
skip_to_datetime
|
bool
|
Whether to skip converting time to datetime. |
False
|
index_keys
|
str | None
|
Column name to set as index if provided. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with order information or empty DataFrame if no orders. |
Source code in pdmt5/dataframe.py
orders_get_as_dicts ¶
orders_get_as_dicts(
symbol: str | None = None,
group: str | None = None,
ticket: int | None = None,
skip_to_datetime: bool = False,
) -> list[dict[str, Any]]
Get active orders with optional filters as a list of dictionaries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str | None
|
Optional symbol filter. Mutually exclusive with group and ticket. |
None
|
group
|
str | None
|
Optional group filter. Mutually exclusive with symbol and ticket. |
None
|
ticket
|
int | None
|
Optional order ticket filter. Mutually exclusive with symbol and group. |
None
|
skip_to_datetime
|
bool
|
Whether to skip converting time to datetime. |
False
|
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
List of dictionaries with order information or empty list if no orders. |
Source code in pdmt5/dataframe.py
positions_get_as_df ¶
positions_get_as_df(
symbol: str | None = None,
group: str | None = None,
ticket: int | None = None,
skip_to_datetime: bool = False,
index_keys: str | None = None,
) -> DataFrame
Get open positions with optional filters as a data frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str | None
|
Optional symbol filter. Mutually exclusive with group and ticket. |
None
|
group
|
str | None
|
Optional group filter. Mutually exclusive with symbol and ticket. |
None
|
ticket
|
int | None
|
Optional position ticket filter. Mutually exclusive with symbol and group. |
None
|
skip_to_datetime
|
bool
|
Whether to skip converting time to datetime. |
False
|
index_keys
|
str | None
|
Column name to set as index if provided. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with position information or empty DataFrame if no positions. |
Source code in pdmt5/dataframe.py
positions_get_as_dicts ¶
positions_get_as_dicts(
symbol: str | None = None,
group: str | None = None,
ticket: int | None = None,
skip_to_datetime: bool = False,
) -> list[dict[str, Any]]
Get open positions with optional filters as a list of dictionaries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str | None
|
Optional symbol filter. Mutually exclusive with group and ticket. |
None
|
group
|
str | None
|
Optional group filter. Mutually exclusive with symbol and ticket. |
None
|
ticket
|
int | None
|
Optional position ticket filter. Mutually exclusive with symbol and group. |
None
|
skip_to_datetime
|
bool
|
Whether to skip converting time to datetime. |
False
|
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
List of dictionaries with position information or empty list if no |
list[dict[str, Any]]
|
positions. |
Source code in pdmt5/dataframe.py
symbol_info_as_df ¶
symbol_info_as_df(
symbol: str,
skip_to_datetime: bool = False,
index_keys: str | None = None,
) -> DataFrame
Get data on a specific symbol as a data frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
Symbol name. |
required |
skip_to_datetime
|
bool
|
Whether to skip converting time to datetime. |
False
|
index_keys
|
str | None
|
Column name to set as index if provided. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with symbol information. |
Source code in pdmt5/dataframe.py
symbol_info_as_dict ¶
Get data on a specific symbol as a dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
Symbol name. |
required |
skip_to_datetime
|
bool
|
Whether to skip converting time to datetime. |
False
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with symbol information. |
Source code in pdmt5/dataframe.py
symbol_info_tick_as_df ¶
symbol_info_tick_as_df(
symbol: str,
skip_to_datetime: bool = False,
index_keys: str | None = None,
) -> DataFrame
Get the last tick for the specified financial instrument as a data frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
Symbol name. |
required |
skip_to_datetime
|
bool
|
Whether to skip converting time to datetime. |
False
|
index_keys
|
str | None
|
Column name to set as index if provided. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with tick information. |
Source code in pdmt5/dataframe.py
symbol_info_tick_as_dict ¶
Get the last tick for the specified financial instrument as a dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
Symbol name. |
required |
skip_to_datetime
|
bool
|
Whether to skip converting time to datetime. |
False
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with tick information. |
Source code in pdmt5/dataframe.py
symbols_get_as_df ¶
symbols_get_as_df(
group: str | None = None,
skip_to_datetime: bool = False,
index_keys: str | None = None,
) -> DataFrame
Get symbols as a data frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
group
|
str | None
|
Symbol group filter (e.g., "USD", "Forex*"). |
None
|
skip_to_datetime
|
bool
|
Whether to skip converting time to datetime. |
False
|
index_keys
|
str | None
|
Column name to set as index if provided. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with symbol information. |
Source code in pdmt5/dataframe.py
symbols_get_as_dicts ¶
symbols_get_as_dicts(
group: str | None = None, skip_to_datetime: bool = False
) -> list[dict[str, Any]]
Get symbols as a list of dictionaries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
group
|
str | None
|
Symbol group filter (e.g., "USD", "Forex*"). |
None
|
skip_to_datetime
|
bool
|
Whether to skip converting time to datetime. |
False
|
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
List of dictionaries with symbol information. |
Source code in pdmt5/dataframe.py
terminal_info_as_df ¶
Get the connected terminal status and settings as a data frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index_keys
|
str | None
|
Column name to set as index if provided. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with terminal information. |
Source code in pdmt5/dataframe.py
terminal_info_as_dict ¶
Get the connected terminal status and settings as a dictionary.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with terminal information. |
version_as_df ¶
Return MetaTrader5 version information as a data frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index_keys
|
str | None
|
Column name to set as index if provided. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with MetaTrader5 version information. |
Source code in pdmt5/dataframe.py
version_as_dict ¶
Return MetaTrader5 version information as a dictionary.
Returns:
| Type | Description |
|---|---|
dict[str, int | str]
|
Dictionary with MetaTrader5 version information. |
Source code in pdmt5/dataframe.py
options: show_bases: false
Extended client class that inherits from Mt5Client and provides a pandas-friendly interface to MetaTrader 5 functions with automatic DataFrame conversion.
Usage Examples¶
Basic Connection¶
import MetaTrader5 as mt5
from pdmt5.dataframe import Mt5DataClient, Mt5Config
# Create configuration
config = Mt5Config(login=123456, password="your_password", server="broker_server")
# Create client
client = Mt5DataClient(mt5=mt5, config=config)
# Use as context manager: entering the block initializes the connection and
# logs in with the config credentials automatically
with client:
# Get account information
account_df = client.account_info_as_df()
print(account_df)
Retrieving Market Data¶
from datetime import datetime
from pdmt5 import parse_copy_ticks, parse_timeframe
with client:
# Get OHLCV data
rates_df = client.copy_rates_from_as_df(
symbol="EURUSD",
timeframe=parse_timeframe("H1"),
date_from=datetime(2024, 1, 1),
count=1000,
)
# Get tick data
ticks_df = client.copy_ticks_from_as_df(
symbol="EURUSD",
date_from=datetime(2024, 1, 1),
count=1000,
flags=parse_copy_ticks("ALL"),
)
Symbol Information¶
with client:
# Get all symbols
symbols_df = client.symbols_get_as_df()
# Get specific symbol info
symbol_info_df = client.symbol_info_as_df("EURUSD")
# Get current tick
tick_df = client.symbol_info_tick_as_df("EURUSD")
Trading History¶
from datetime import datetime
with client:
# Get historical orders
orders_df = client.history_orders_get_as_df(
date_from=datetime(2024, 1, 1), date_to=datetime(2024, 1, 31), symbol="EURUSD"
)
# Get historical deals
deals_df = client.history_deals_get_as_df(
date_from=datetime(2024, 1, 1), date_to=datetime(2024, 1, 31)
)
Current Positions and Orders¶
with client:
# Get current positions
positions_df = client.positions_get_as_df()
# Get current orders
orders_df = client.orders_get_as_df(symbol="EURUSD")
Data Conversion Features¶
The Mt5DataClient automatically handles:
- Time Conversion: Converts Unix timestamps to pandas datetime objects
- Index Setting: Sets appropriate datetime indexes for time-series data
- DataFrame Creation: Converts MetaTrader 5 named tuples to pandas DataFrames
- Error Handling: Provides meaningful error messages for failed operations
- Empty Data: Returns empty DataFrames when no data is available
Error Handling¶
All methods raise Mt5RuntimeError exceptions with detailed error information when operations fail:
from pdmt5.mt5 import Mt5RuntimeError
try:
rates_df = client.copy_rates_from_as_df(
"INVALID", mt5.TIMEFRAME_H1, datetime.now(), 100
)
except Mt5RuntimeError as e:
print(f"MetaTrader 5 error: {e}")
Connection Management¶
The client supports both explicit and context manager usage:
# Explicit initialization
client.initialize()
try:
# Your trading operations
pass
finally:
client.shutdown()
# Context manager (recommended)
with client:
# Your trading operations
pass
Type Safety¶
All methods include comprehensive type hints and use pydantic for configuration validation, ensuring type safety throughout the codebase.