Market¶
Candles, rates, and market data services.
Models¶
Candle¶
Candle
¶
Bases: BaseModel
A single OHLCV bar from the MT5 price history.
Wraps one row of the numpy structured array returned by
mt5.copy_rates_from_pos(), mt5.copy_rates_from(), and
mt5.copy_rates_range().
| ATTRIBUTE | DESCRIPTION |
|---|---|
time |
Bar open time as a Unix timestamp (seconds).
TYPE:
|
open |
Opening price of the bar.
TYPE:
|
high |
Highest price reached during the bar.
TYPE:
|
low |
Lowest price reached during the bar.
TYPE:
|
close |
Closing price of the bar.
TYPE:
|
tick_volume |
Number of price ticks during the bar.
TYPE:
|
spread |
Spread in points at bar open.
TYPE:
|
real_volume |
Traded volume (exchange instruments only; 0 for Forex).
TYPE:
|
Services¶
MarketService¶
MarketService
¶
Retrieves OHLCV rate data from the MT5 terminal.
Wraps the three MT5 rate-copy functions:
- copy_rates_from_pos โ most recent N bars from the current position
- copy_rates_from โ N bars starting from a specific datetime
- copy_rates_range โ all bars within a datetime range
MT5 returns rates as a numpy structured array or a list of dicts
depending on the build. The hasattr / dict-key fallback in each
parser handles both shapes transparently.
get_candles
¶
Retrieve the most recent candles for a symbol.
Calls mt5.copy_rates_from_pos() starting at bar index 0
(the most recent completed bar) and going back count bars.
| PARAMETER | DESCRIPTION |
|---|---|
symbol
|
Trading instrument name (e.g.
TYPE:
|
timeframe
|
MT5 timeframe constant (e.g.
TYPE:
|
count
|
Number of bars to retrieve.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Result[list[Candle]]
|
|
Result[list[Candle]]
|
order on success (may be empty), or a failure result if the |
Result[list[Candle]]
|
MT5 call fails or the payload cannot be parsed. |
copy_rates_from
¶
copy_rates_from(
symbol: str,
timeframe: int,
date_from: datetime,
count: int,
) -> Result[list[Candle]]
Retrieve candles starting from a specific datetime.
| PARAMETER | DESCRIPTION |
|---|---|
symbol
|
Trading instrument name.
TYPE:
|
timeframe
|
MT5 timeframe constant.
TYPE:
|
date_from
|
Start datetime (inclusive).
TYPE:
|
count
|
Number of bars to retrieve going forward from
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Result[list[Candle]]
|
|
Result[list[Candle]]
|
failure result on error. |
copy_rates_range
¶
copy_rates_range(
symbol: str,
timeframe: int,
date_from: datetime,
date_to: datetime,
) -> Result[list[Candle]]
Retrieve all candles within a datetime range.
| PARAMETER | DESCRIPTION |
|---|---|
symbol
|
Trading instrument name.
TYPE:
|
timeframe
|
MT5 timeframe constant.
TYPE:
|
date_from
|
Range start datetime (inclusive).
TYPE:
|
date_to
|
Range end datetime (inclusive).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Result[list[Candle]]
|
|
Result[list[Candle]]
|
failure result on error. |
Usage¶
from syntiq_mt5 import constants
# Get recent candles
res = mt5.get_candles("EURUSD", timeframe=constants.TIMEFRAME_H1, count=100)
# Get candles from a date
from datetime import datetime, timezone
date_from = datetime(2024, 1, 1, tzinfo=timezone.utc)
res = mt5.copy_rates_from("EURUSD", constants.TIMEFRAME_D1, date_from, count=30)
# Get candles in a range
date_to = datetime(2024, 1, 31, tzinfo=timezone.utc)
res = mt5.copy_rates_range("EURUSD", constants.TIMEFRAME_H4, date_from, date_to)