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",
)
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. |
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. |
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. |
required |
date_to
|
datetime
|
End date. |
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. |
required |
date_to
|
datetime
|
End date. |
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. |
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. |
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. |
required |
date_to
|
datetime
|
End date. |
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. |
required |
date_to
|
datetime
|
End date. |
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 (required if not using ticket/position). |
None
|
date_to
|
datetime | None
|
End date (required if not using ticket/position). |
None
|
group
|
str | None
|
Optional group filter. |
None
|
symbol
|
str | None
|
Optional symbol filter. |
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 (required if not using ticket/position). |
None
|
date_to
|
datetime | None
|
End date (required if not using ticket/position). |
None
|
group
|
str | None
|
Optional group filter. |
None
|
symbol
|
str | None
|
Optional symbol filter. |
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 (required if not using ticket/position). |
None
|
date_to
|
datetime | None
|
End date (required if not using ticket/position). |
None
|
group
|
str | None
|
Optional group filter. |
None
|
symbol
|
str | None
|
Optional symbol filter. |
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 (required if not using ticket/position). |
None
|
date_to
|
datetime | None
|
End date (required if not using ticket/position). |
None
|
group
|
str | None
|
Optional group filter. |
None
|
symbol
|
str | None
|
Optional symbol filter. |
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 | None = None,
server: str | None = None,
timeout: int | None = None,
) -> None
Initialize MetaTrader5 connection with retry logic.
This method overrides the base class to add retry logic and use config values.
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 | 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 fails after retries. |
Source code in pdmt5/dataframe.py
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. |
None
|
group
|
str | None
|
Optional group filter. |
None
|
ticket
|
int | None
|
Optional order ticket filter. |
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. |
None
|
group
|
str | None
|
Optional group filter. |
None
|
ticket
|
int | None
|
Optional order ticket filter. |
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. |
None
|
group
|
str | None
|
Optional group filter. |
None
|
ticket
|
int | None
|
Optional position ticket filter. |
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. |
None
|
group
|
str | None
|
Optional group filter. |
None
|
ticket
|
int | None
|
Optional position ticket filter. |
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, automatic data conversion, and comprehensive validation utilities.
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",
)
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. |
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. |
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. |
required |
date_to
|
datetime
|
End date. |
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. |
required |
date_to
|
datetime
|
End date. |
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. |
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. |
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. |
required |
date_to
|
datetime
|
End date. |
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. |
required |
date_to
|
datetime
|
End date. |
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 (required if not using ticket/position). |
None
|
date_to
|
datetime | None
|
End date (required if not using ticket/position). |
None
|
group
|
str | None
|
Optional group filter. |
None
|
symbol
|
str | None
|
Optional symbol filter. |
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 (required if not using ticket/position). |
None
|
date_to
|
datetime | None
|
End date (required if not using ticket/position). |
None
|
group
|
str | None
|
Optional group filter. |
None
|
symbol
|
str | None
|
Optional symbol filter. |
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 (required if not using ticket/position). |
None
|
date_to
|
datetime | None
|
End date (required if not using ticket/position). |
None
|
group
|
str | None
|
Optional group filter. |
None
|
symbol
|
str | None
|
Optional symbol filter. |
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 (required if not using ticket/position). |
None
|
date_to
|
datetime | None
|
End date (required if not using ticket/position). |
None
|
group
|
str | None
|
Optional group filter. |
None
|
symbol
|
str | None
|
Optional symbol filter. |
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 | None = None,
server: str | None = None,
timeout: int | None = None,
) -> None
Initialize MetaTrader5 connection with retry logic.
This method overrides the base class to add retry logic and use config values.
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 | 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 fails after retries. |
Source code in pdmt5/dataframe.py
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. |
None
|
group
|
str | None
|
Optional group filter. |
None
|
ticket
|
int | None
|
Optional order ticket filter. |
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. |
None
|
group
|
str | None
|
Optional group filter. |
None
|
ticket
|
int | None
|
Optional order ticket filter. |
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. |
None
|
group
|
str | None
|
Optional group filter. |
None
|
ticket
|
int | None
|
Optional position ticket filter. |
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. |
None
|
group
|
str | None
|
Optional group filter. |
None
|
ticket
|
int | None
|
Optional position ticket filter. |
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
with client:
# Get account information
account_df = client.account_info()
print(account_df)
Retrieving Market Data¶
from datetime import datetime
import MetaTrader5 as mt5
with client:
# Get OHLCV data
rates_df = client.copy_rates_from(
symbol="EURUSD",
timeframe=mt5.TIMEFRAME_H1,
date_from=datetime(2024, 1, 1),
count=1000
)
# Get tick data
ticks_df = client.copy_ticks_from(
symbol="EURUSD",
date_from=datetime(2024, 1, 1),
count=1000,
flags=mt5.COPY_TICKS_ALL
)
Symbol Information¶
with client:
# Get all symbols
symbols_df = client.symbols_get()
# Get specific symbol info
symbol_info_df = client.symbol_info("EURUSD")
# Get current tick
tick_df = client.symbol_info_tick("EURUSD")
Trading History¶
from datetime import datetime
with client:
# Get historical orders
orders_df = client.history_orders_get(
date_from=datetime(2024, 1, 1),
date_to=datetime(2024, 1, 31),
symbol="EURUSD"
)
# Get historical deals
deals_df = client.history_deals_get(
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()
# Get current orders
orders_df = client.orders_get(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("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.