MT5CopyRatesRange

Get bars in the specified date range from the MetaTrader 5 terminal.

MT5CopyRatesRange(
   symbol,       // symbol name
   timeframe,    // timeframe
   date_from,    // date the bars are requested from
   date_to       // date, up to which the bars are requested
   )

Parameters

symbol

[in]  Financial instrument name, for example, "EURUSD".

timeframe

[in]  Timeframe the bars are requested for. Set by a value from the MT5_TIMEFRAME enumeration.

date_from

[in]  Date the bars are requested from. Set by the 'datetime' object or as a number of seconds elapsed since 1970.01.01. Bars with the open time >= date_from are returned.

date_to

[in]  Date, up to which the bars are requested. Set by the 'datetime' object or as a number of seconds elapsed since 1970.01.01. Bars with the open time <= date_to are returned.

Return Value

Returns bars as tuples (time, open, high, low, close, tick_volume, spread, real_volume).

Note

See the CopyRates() function for more information.

When creating the 'datetime' object, Python uses the local time zone, while MetaTrader 5 stores tick and bar open time in UTC time zone (without the shift). Therefore, 'datetime' should be created in UTC time for executing functions that use time. The data obtained from MetaTrader 5 have UTC time, but Python applies the local time shift again when trying to print them. Thus, the obtained data should also be corrected for visual presentation.

Example:

from datetime import datetime
from MetaTrader5 import *
# import the pandas module for displaying data obtained in the tabular form
import pandas as pd
pd.set_option('display.max_columns', 500# number of columns to be displayed
pd.set_option('display.width', 1500)      # max table width to display
 
# connect to MetaTrader 5
MT5Initialize()
# wait till MetaTrader 5 connects to trade server
MT5WaitForTerminal()
 
# set time zone to UTC
timezone = pytz.timezone("Etc/UTC")
# create 'datetime' objects in UTC time zone to avoid the implementation of a local time zone
utc_from = datetime(201945tzinfo=timezone)
utc_to = datetime(2019, 4, 5, hour = 13, tzinfo=timezone)
# get 10 EURPLN M5 bars starting from 01.04.2019 in UTC time zone
rates = MT5CopyRatesFrom("EURPLN"MT5_TIMEFRAME_M5utc_from, utc_to)
 
# shut down connection to MetaTrader 5
MT5Shutdown()
# display each element of obtained data in a new line
print("Display obtained data 'as is'")
for rate in rates:
    print(rate)
 
# create DataFrame out of the obtained data
rates_frame = pd.DataFrame(list(rates),
                           columns=['time', 'open', 'low', 'high', 'close', 'tick_volume', 'spread', 'real_volume'])
# display data
print("\nDisplay dataframe with data")
print(rates_frame)  # we can see that Python provides the bar open time in a local time zone with an offset
 
# get UTC offset for the local PC
UTC_OFFSET_TIMEDELTA = datetime.utcnow() - datetime.now()
 
# create a simple function correcting the offset head-on
def local_to_utc(dt):
    return dt + UTC_OFFSET_TIMEDELTA
 
# apply the offset for the 'time' column in the rates_frame dataframe
rates_frame['time'] = rates_frame.apply(lambda ratelocal_to_utc(rate['time']), axis=1)
 
# display the data once again and make sure the open time has changed
print("\nDisplay dataframe after correcting time")
print(rates_frame)
 
Result:
Display obtained data 'as is'
MT5Rate(time=datetime.datetime(2019, 4, 1, 3, 30), open=4.2988, low=4.2993, high=4.2986, close=4.2989, tick_volume=853, spread=540, real_volume=0)
MT5Rate(time=datetime.datetime(2019, 4, 1, 3, 35), open=4.2989, low=4.2989, high=4.2989, close=4.2989, tick_volume=1, spread=630, real_volume=0)
MT5Rate(time=datetime.datetime(2019, 4, 1, 3, 40), open=4.2989, low=4.2991, high=4.2987, close=4.2989, tick_volume=10, spread=560, real_volume=0)
MT5Rate(time=datetime.datetime(2019, 4, 1, 3, 45), open=4.2989, low=4.2989, high=4.2989, close=4.2989, tick_volume=2, spread=600, real_volume=0)
MT5Rate(time=datetime.datetime(2019, 4, 1, 3, 50), open=4.2989, low=4.2989, high=4.2989, close=4.2989, tick_volume=4, spread=640, real_volume=0)
MT5Rate(time=datetime.datetime(2019, 4, 1, 3, 55), open=4.2989, low=4.2989, high=4.2989, close=4.2989, tick_volume=4, spread=640, real_volume=0)
MT5Rate(time=datetime.datetime(2019, 4, 1, 4, 0), open=4.2989, low=4.2995, high=4.29872, close=4.2994, tick_volume=40, spread=500, real_volume=0)
MT5Rate(time=datetime.datetime(2019, 4, 1, 4, 5), open=4.2995, low=4.2998, high=4.299, close=4.2998, tick_volume=38, spread=440, real_volume=0)
MT5Rate(time=datetime.datetime(2019, 4, 1, 4, 10), open=4.2998, low=4.2998, high=4.2991, close=4.2992, tick_volume=35, spread=440, real_volume=0)
MT5Rate(time=datetime.datetime(2019, 4, 1, 4, 15), open=4.2992, low=4.2995, high=4.2989, close=4.2993, tick_volume=379, spread=500, real_volume=0)
 
Display the first 10 values from the dataframe
                 time    open     low     high   close  tick_volume  spread  real_volume
0 2019-04-01 03:30:00  4.2988  4.2993  4.29860  4.2989          853     540            0
1 2019-04-01 03:35:00  4.2989  4.2989  4.29890  4.2989            1     630            0
2 2019-04-01 03:40:00  4.2989  4.2991  4.29870  4.2989           10     560            0
3 2019-04-01 03:45:00  4.2989  4.2989  4.29890  4.2989            2     600            0
4 2019-04-01 03:50:00  4.2989  4.2989  4.29890  4.2989            4     640            0
5 2019-04-01 03:55:00  4.2989  4.2989  4.29890  4.2989            4     640            0
6 2019-04-01 04:00:00  4.2989  4.2995  4.29872  4.2994           40     500            0
7 2019-04-01 04:05:00  4.2995  4.2998  4.29900  4.2998           38     440            0
8 2019-04-01 04:10:00  4.2998  4.2998  4.29910  4.2992           35     440            0
9 2019-04-01 04:15:00  4.2992  4.2995  4.29890  4.2993          379     500            0
 
Display the first 10 values from the dataframe after adjusting the time
                 time    open     low     high   close  tick_volume  spread  real_volume
0 2019-04-01 00:30:00  4.2988  4.2993  4.29860  4.2989          853     540            0
1 2019-04-01 00:35:00  4.2989  4.2989  4.29890  4.2989            1     630            0
2 2019-04-01 00:40:00  4.2989  4.2991  4.29870  4.2989           10     560            0
3 2019-04-01 00:45:00  4.2989  4.2989  4.29890  4.2989            2     600            0
4 2019-04-01 00:50:00  4.2989  4.2989  4.29890  4.2989            4     640            0
5 2019-04-01 00:55:00  4.2989  4.2989  4.29890  4.2989            4     640            0
6 2019-04-01 01:00:00  4.2989  4.2995  4.29872  4.2994           40     500            0
7 2019-04-01 01:05:00  4.2995  4.2998  4.29900  4.2998           38     440            0
8 2019-04-01 01:10:00  4.2998  4.2998  4.29910  4.2992           35     440            0
9 2019-04-01 01:15:00  4.2992  4.2995  4.29890  4.2993          379     500            0

See also

CopyRates, MT5CopyRatesFrom, MT5CopyRatesRange, MT5CopyTicksFrom, MT5CopyTicksRange