MT5CopyTicksRange

Get ticks for the specified date range from the MetaTrader 5 terminal.

MT5CopyTicksRange(
   symbol,       // symbol name
   from,         // date the ticks are requested from
   to,           // date, up to which the ticks are requested
   flags         // combination of flags defining the type of requested ticks
   )

Parameters

symbol

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

from

[in]  Date the ticks are requested from. Set by the 'datetime' object or as a number of seconds elapsed since 1970.01.01.

to

[in]  Date, up to which the ticks are requested. Set by the 'datetime' object or as a number of seconds elapsed since 1970.01.01.

flags

[in]  A flag to define the type of the requested ticks. MT5_COPY_TICKS_INFO – ticks with Bid and/or Ask changes, MT5_COPY_TICKS_TRADE – ticks with changes in Last and Volume, MT5_COPY_TICKS_ALL – all ticks. Flag values are described in the MT5_COPY_TICKS enumeration.

Return Value

Returns ticks in the form of tuples (time, bid, ask, last, flags).  In the tuple, the flags variable can be a combination of flags from the MT5_TICK_FLAG enumeration.

Note

See the CopyTicks 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 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' objects in UTC time zone to avoid the implementation of a local time zone offset
utc_from = datetime(2019, 4, 1, tzinfo=timezone)
utc_to = datetime(2019, 4, 5, tzinfo=timezone)
# request AUDUSD ticks within 01.04.2019 - 05.04.2019
ticks = MT5CopyTicksFrom("EURUSD"utc_from, utc_to, MT5_COPY_TICKS_ALL)
print("Ticks received:",len(ticks))
 
# shut down connection to MetaTrader 5
MT5Shutdown()
# display data on each tick in a new line
print("Display obtained ticks 'as is'")
count = 0
for tick in ticks:
    print(tick)
    if(count >= 10):
        break
 
# create DataFrame out of the obtained data
ticks_frame = pd.DataFrame(list(ticks),
                           columns=['time', 'bid', 'ask', 'last', 'volume', 'flags'])
# display data
print("\nDisplay dataframe with ticks")
print(ticks_frame.head(10))  # we can see that Python provides ticks time in the local time zone with an offset
 
# get a UTC time offset for a 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 ticks_frame dataframe
ticks_frame['time'] = ticks_frame.apply(lambda ticklocal_to_utc(tick['time']), axis=1)
 
# display the data once again and make sure the tick time changed
print("\nDisplay the dataframe with ticks after adjusting the time")
print(ticks_frame.head(10))
 
Result:
Ticks received: 243374
Display obtained ticks 'as is'
MT5Tick(time=datetime.datetime(2019, 4, 1, 3, 2, 2, 759000), bid=0.71155, ask=0.71223, last=0.0, volume=0.0, flags=134)
MT5Tick(time=datetime.datetime(2019, 4, 1, 3, 2, 7, 824000), bid=0.71154, ask=0.71222, last=0.0, volume=0.0, flags=134)
MT5Tick(time=datetime.datetime(2019, 4, 1, 3, 2, 27, 818000), bid=0.71155, ask=0.71223, last=0.0, volume=0.0, flags=134)
MT5Tick(time=datetime.datetime(2019, 4, 1, 3, 2, 43, 794000), bid=0.71159, ask=0.71204, last=0.0, volume=0.0, flags=134)
MT5Tick(time=datetime.datetime(2019, 4, 1, 3, 2, 44, 67000), bid=0.71145, ask=0.71209, last=0.0, volume=0.0, flags=134)
MT5Tick(time=datetime.datetime(2019, 4, 1, 3, 2, 44, 140000), bid=0.71157, ask=0.71223, last=0.0, volume=0.0, flags=134)
MT5Tick(time=datetime.datetime(2019, 4, 1, 3, 2, 44, 432000), bid=0.71157, ask=0.71225, last=0.0, volume=0.0, flags=4)
MT5Tick(time=datetime.datetime(2019, 4, 1, 3, 2, 44, 872000), bid=0.71159, ask=0.71204, last=0.0, volume=0.0, flags=134)
MT5Tick(time=datetime.datetime(2019, 4, 1, 3, 2, 45, 164000), bid=0.71148, ask=0.7121, last=0.0, volume=0.0, flags=134)
MT5Tick(time=datetime.datetime(2019, 4, 1, 3, 2, 45, 244000), bid=0.71158, ask=0.71223, last=0.0, volume=0.0, flags=134)
 
Display dataframe with ticks
                     time      bid      ask  last  volume  flags
0 2019-04-01 03:02:02.759  0.71155  0.71223   0.0     0.0    134
1 2019-04-01 03:02:07.824  0.71154  0.71222   0.0     0.0    134
2 2019-04-01 03:02:27.818  0.71155  0.71223   0.0     0.0    134
3 2019-04-01 03:02:43.794  0.71159  0.71204   0.0     0.0    134
4 2019-04-01 03:02:44.067  0.71145  0.71209   0.0     0.0    134
5 2019-04-01 03:02:44.140  0.71157  0.71223   0.0     0.0    134
6 2019-04-01 03:02:44.432  0.71157  0.71225   0.0     0.0      4
7 2019-04-01 03:02:44.872  0.71159  0.71204   0.0     0.0    134
8 2019-04-01 03:02:45.164  0.71148  0.71210   0.0     0.0    134
9 2019-04-01 03:02:45.244  0.71158  0.71223   0.0     0.0    134
 
Display dataframe with ticks after adjusting the time
                     time      bid      ask  last  volume  flags
0 2019-04-01 00:02:02.759  0.71155  0.71223   0.0     0.0    134
1 2019-04-01 00:02:07.824  0.71154  0.71222   0.0     0.0    134
2 2019-04-01 00:02:27.818  0.71155  0.71223   0.0     0.0    134
3 2019-04-01 00:02:43.794  0.71159  0.71204   0.0     0.0    134
4 2019-04-01 00:02:44.067  0.71145  0.71209   0.0     0.0    134
5 2019-04-01 00:02:44.140  0.71157  0.71223   0.0     0.0    134
6 2019-04-01 00:02:44.432  0.71157  0.71225   0.0     0.0      4
7 2019-04-01 00:02:44.872  0.71159  0.71204   0.0     0.0    134
8 2019-04-01 00:02:45.164  0.71148  0.71210   0.0     0.0    134
9 2019-04-01 00:02:45.244  0.71158  0.71223   0.0     0.0    134

See also

CopyRates, MT5CopyRatesFromPos, MT5CopyRatesRange, MT5CopyTicksFrom, MT5CopyTicksRange