MT5CopyRatesFrom

Get bars from the MetaTrader 5 terminal starting from the specified date.

MT5CopyRatesFrom(
   symbol,       // symbol name
   timeframe,    // timeframe
   from,         // initial bar open date
   count         // number of bars
   )

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.

from

[in]  Date of opening of the first bar from the requested sample. Set by the 'datetime' object or as a number of seconds elapsed since 1970.01.01.

count

[in]  Number of bars to receive.

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.  

MT5_TIMEFRAME is an enumeration with possible chart period values

ID

Description

MT5_TIMEFRAME_M1

1 minute

MT5_TIMEFRAME_M2

2 minutes

MT5_TIMEFRAME_M3

3 minutes

MT5_TIMEFRAME_M4

4 minutes

MT5_TIMEFRAME_M5

5 minutes

MT5_TIMEFRAME_M6

6 minutes

MT5_TIMEFRAME_M10

10 minutes

MT5_TIMEFRAME_M12

12 minutes

MT5_TIMEFRAME_M12

15 minutes

MT5_TIMEFRAME_M20

20 minutes

MT5_TIMEFRAME_M30

30 minutes

MT5_TIMEFRAME_H1

1 hour

MT5_TIMEFRAME_H2

2 hours

MT5_TIMEFRAME_H3

3 hours

MT5_TIMEFRAME_H4

4 hours

MT5_TIMEFRAME_H6

6 hours

MT5_TIMEFRAME_H8

8 hours

MT5_TIMEFRAME_H12

12 hours

MT5_TIMEFRAME_D1

1 day

MT5_TIMEFRAME_W1

1 week

MT5_TIMEFRAME_MON1

1 month

 

Example:

from datetime import datetime
from MetaTrader5 import *
# import the pandas module for displaying obtained data 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
# import pytz module for working with time zone
import pytz
 
# connect to MetaTrader 5
MT5Initialize()
# wait till MetaTrader 5 connects to the trade server
MT5WaitForTerminal()
 
# set time zone to UTC
timezone = pytz.timezone("Etc/UTC")
# create 'datetime' object in UTC time zone to avoid the implementation of a local time zone offset
utc_from = datetime(201945tzinfo=timezone)
# get 10 EURUSD H4 bars starting from 01.04.2019 in UTC time zone
rates = MT5CopyRatesFrom("EURUSD"MT5_TIMEFRAME_H4utc_from10)
 
# 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 bars open time in the local time zone with an offset
 
# get a UTC time 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 H4 candles open time is now multiple of 4
print("\nDisplay the dataframe after adjusting the time")
print(rates_frame)
 
Result:
Display obtained data 'as is'
MT5Rate(time=datetime.datetime(2019, 4, 3, 15, 0), open=1.12431, low=1.12543, high=1.12335, close=1.12341, tick_volume=11735, spread=8, real_volume=0)
MT5Rate(time=datetime.datetime(2019, 4, 3, 19, 0), open=1.12339, low=1.12487, high=1.12247, close=1.12439, tick_volume=15241, spread=8, real_volume=0)
MT5Rate(time=datetime.datetime(2019, 4, 3, 23, 0), open=1.12439, low=1.12478, high=1.12311, close=1.12312, tick_volume=4973, spread=8, real_volume=0)
MT5Rate(time=datetime.datetime(2019, 4, 4, 3, 0), open=1.12324, low=1.12472, high=1.12318, close=1.1244, tick_volume=3099, spread=4, real_volume=0)
MT5Rate(time=datetime.datetime(2019, 4, 4, 7, 0), open=1.1244, low=1.12451, high=1.12364, close=1.12406, tick_volume=3304, spread=4, real_volume=0)
MT5Rate(time=datetime.datetime(2019, 4, 4, 11, 0), open=1.12406, low=1.12472, high=1.12302, close=1.12313, tick_volume=10119, spread=8, real_volume=0)
MT5Rate(time=datetime.datetime(2019, 4, 4, 15, 0), open=1.12313, low=1.12359, high=1.12122, close=1.12171, tick_volume=15098, spread=8, real_volume=0)
MT5Rate(time=datetime.datetime(2019, 4, 4, 19, 0), open=1.12171, low=1.12246, high=1.12056, close=1.12188, tick_volume=15369, spread=8, real_volume=0)
MT5Rate(time=datetime.datetime(2019, 4, 4, 23, 0), open=1.12187, low=1.12265, high=1.1213, close=1.12191, tick_volume=5156, spread=8, real_volume=0)
MT5Rate(time=datetime.datetime(2019, 4, 5, 3, 0), open=1.12199, low=1.12286, high=1.1217, close=1.1222, tick_volume=3903, spread=4, real_volume=0)
 
Display dataframe with data
                 time     open      low     high    close  tick_volume  spread  real_volume
0 2019-04-03 15:00:00  1.12431  1.12543  1.12335  1.12341        11735       8            0
1 2019-04-03 19:00:00  1.12339  1.12487  1.12247  1.12439        15241       8            0
2 2019-04-03 23:00:00  1.12439  1.12478  1.12311  1.12312         4973       8            0
3 2019-04-04 03:00:00  1.12324  1.12472  1.12318  1.12440         3099       4            0
4 2019-04-04 07:00:00  1.12440  1.12451  1.12364  1.12406         3304       4            0
5 2019-04-04 11:00:00  1.12406  1.12472  1.12302  1.12313        10119       8            0
6 2019-04-04 15:00:00  1.12313  1.12359  1.12122  1.12171        15098       8            0
7 2019-04-04 19:00:00  1.12171  1.12246  1.12056  1.12188        15369       8            0
8 2019-04-04 23:00:00  1.12187  1.12265  1.12130  1.12191         5156       8            0
9 2019-04-05 03:00:00  1.12200  1.12286  1.12170  1.12220         3903       4            0
 
Display dataframe after adjusting the candle open time
                 time     open      low     high    close  tick_volume  spread  real_volume
0 2019-04-03 12:00:00  1.12431  1.12543  1.12335  1.12341        11735       8            0
1 2019-04-03 16:00:00  1.12339  1.12487  1.12247  1.12439        15241       8            0
2 2019-04-03 20:00:00  1.12439  1.12478  1.12311  1.12312         4973       8            0
3 2019-04-04 00:00:00  1.12324  1.12472  1.12318  1.12440         3099       4            0
4 2019-04-04 04:00:00  1.12440  1.12451  1.12364  1.12406         3304       4            0
5 2019-04-04 08:00:00  1.12406  1.12472  1.12302  1.12313        10119       8            0
6 2019-04-04 12:00:00  1.12313  1.12359  1.12122  1.12171        15098       8            0
7 2019-04-04 16:00:00  1.12171  1.12246  1.12056  1.12188        15369       8            0
8 2019-04-04 20:00:00  1.12187  1.12265  1.12130  1.12191         5156       8            0
9 2019-04-05 00:00:00  1.12200  1.12286  1.12170  1.12220         3903       4            0

See also

CopyRates, MT5CopyRatesFromPos, MT5CopyRatesRange, MT5CopyTicksFrom, MT5CopyTicksRange