Skip to content

Rates

Module for retrieving and managing historical price data from MetaTrader 5.

Provides the Rates class for accessing historical price information.

Rates #

Rates(symbol, time_frame, start_position, count)

Represents historical price data for a financial instrument.

Parameters:

Name Type Description Default
symbol str

The financial instrument symbol.

required
time_frame int

The time frame for the rates.

required
start_position int

The starting position for the rates.

required
count int

The number of rates to retrieve.

required

Returns:

Type Description
None

None

Source code in mqpy\rates.py
def __init__(self, symbol: str, time_frame: int, start_position: int, count: int) -> None:
    """Initializes a Rates object.

    Args:
        symbol (str): The financial instrument symbol.
        time_frame (int): The time frame for the rates.
        start_position (int): The starting position for the rates.
        count (int): The number of rates to retrieve.

    Returns:
        None
    """

    def _raise_value_error(msg: str) -> None:
        raise ValueError(msg)

    try:
        rates = Mt5.copy_rates_from_pos(symbol, time_frame, start_position, count)
        if rates is None:
            _raise_value_error(f"Failed to retrieve rates for {symbol}")

        self._time = [rate[0] for rate in rates]
        self._open = [rate[1] for rate in rates]
        self._high = [rate[2] for rate in rates]
        self._low = [rate[3] for rate in rates]
        self._close = [rate[4] for rate in rates]
        self._tick_volume = [rate[5] for rate in rates]
        self._spread = [rate[6] for rate in rates]
        self._real_volume = [rate[7] for rate in rates]
    except Mt5.Error as e:
        raise ValueError(f"Failed to create Rates object for symbol {symbol}") from e

time property #

time

List of timestamps.

open property #

open

List of open prices.

high property #

high

List of high prices.

low property #

low

List of low prices.

close property #

close

List of close prices.

tick_volume property #

tick_volume

List of tick volumes.

spread property #

spread

List of spreads.

real_volume property #

real_volume

List of real volumes.