MT5 Python copy_rates_range()
Returns the bars for one instrument and timeframe that open between two dates.
Risk warning: CFDs and spread bets are complex instruments and come with a high risk of losing money rapidly due to leverage. You can lose money on these products, and each provider publishes its own loss figure under FCA rules. You should consider whether you understand how they work and whether you can afford to take the high risk of losing your money. Automated and API trading can amplify losses through bugs, connectivity failures and runaway orders.
This is a Python function, not an HTTP endpoint. It has no method, no path and no address to send a request to: the package talks to a MetaTrader 5 terminal running on the same computer.
The page says the terminal supplies bars only as far back as its charts hold, a limit set by the terminal's Max. bars in chart setting. The page warns that Python builds a datetime in local time while the terminal stores times in UTC, so dates passed to the package should be created in UTC.
Signature
The form of the call on the vendor's page, with the vendor's comments taken out.
copy_rates_range( symbol, timeframe, date_from, date_to )
Arguments
| Name | Type | Description |
|---|---|---|
| symbolrequired | Not stated | The instrument's name. The page's example value is "EURUSD". |
| timeframerequired | Not stated | The bar size, as a value of the package's TIMEFRAME enumeration. |
| date_fromrequired | datetime or int | The start of the range. The page says it is given as a datetime object or as a number of seconds since 1 January 1970. Bars that open at or after this time are returned. |
| date_torequired | datetime or int | The end of the range, given the same way. Bars that open at or before this time are returned. |
Return value
- Type
- numpy array or None
- What comes back
- One row per bar with the named columns time, open, high, low, close, tick_volume, spread and real_volume. On failure the page says it returns None, and that last_error() gives the reason.
Sample
Written by this site from the signature and arguments recorded on this page. It has not been run against a terminal, a server or an account, and the values in it are placeholders. No credential is typed into it: where one is needed it is read from an environment variable on your own machine. Nothing is entered on this site.
from datetime import datetime, timezone
import MetaTrader5 as mt5
if not mt5.initialize():
raise SystemExit(f"initialize() failed: {mt5.last_error()}")
start = datetime(2026, 1, 5, tzinfo=timezone.utc)
end = datetime(2026, 1, 9, tzinfo=timezone.utc)
rates = mt5.copy_rates_range("EURUSD", mt5.TIMEFRAME_M5, start, end)
print(0 if rates is None else len(rates))
mt5.shutdown()