copy_ticks_range

从MetaTrader 5程序端获取指定日期范围内的报价。

copy_ticks_range(
   symbol,       // 交易品种名称
   date_from,    // 请求报价的日期
   date_to,      // 请求报价的结束日期
   flags         // 定义请求报价类型的标识组合
   )

参数

symbol

[in]  交易品种名称,例如"EURUSD"。所需的未命名参数。

date_from

[in]  请求报价的开始日期。通过'datetime'对象或根据1970.01.01以来过去的秒数设置。所需的未命名参数。

date_to

[in]  请求报价的结束日期。通过'datetime'对象或根据1970.01.01以来过去的秒数设置。所需的未命名参数。

flags

[in]  确定请求报价类型的标记。COPY_TICKS_INFO – 卖价和/或买价变化的报价, COPY_TICKS_TRADE – 最后价和交易量变化的报价, COPY_TICKS_ALL – 全部报价。标识值在COPY_TICKS枚举中描述。所需的未命名参数。

返回值

返回numpy数据报价(包含以下指定列:时间、卖价、买价、最后价、标识)。'flags'值可以是TICK_FLAG枚举中的标识组合。错误情况下返回None。可使用last_error()获取错误信息。

注意

参见CopyTicks函数获取更多信息。

当创建'datetime'对象时,Python使用本地时区,而MetaTrader 5以UTC时区保存报价和柱形图开盘时间(没有时移)。 因此,'datetime'应在UTC时间内创建,用于执行使用时间的函数。从MetaTrader 5获得的数据有UTC时间,但Python在尝试打印时再次应用本地时移。 因此,所获得的数据也应进行校正,以便直观表示。

例如:

from datetime import datetime
import MetaTrader5 as mt5
# 显示有关MetaTrader 5程序包的数据
print("MetaTrader5 package author: ",mt5.__author__)
print("MetaTrader5 package version: ",mt5.__version__)
 
# 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
# import pytz module for working with time zone
import pytz
 
# establish connection to MetaTrader 5 terminal
if not mt5.initialize():
    print("initialize() failed, error code =",mt5.last_error())
    quit()
 
# 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(2020, 1, 10, tzinfo=timezone)
utc_to = datetime(2020, 1, 11, tzinfo=timezone)
# request AUDUSD ticks within 11.01.2020 - 11.01.2020
ticks = mt5.copy_ticks_range("AUDUSD"utc_from, utc_tomt5.COPY_TICKS_ALL)
print("Ticks received:",len(ticks))
 
# shut down connection to the MetaTrader 5 terminal
mt5.shutdown()
 
# display data on each tick on a new line
print("Display obtained ticks 'as is'")
count = 0
for tick in ticks:
    count+=1
    print(tick)
    if count >= 10:
        break
 
# create DataFrame out of the obtained data
ticks_frame = pd.DataFrame(ticks)
# convert time in seconds into the datetime format
ticks_frame['time']=pd.to_datetime(ticks_frame['time'], unit='s')
 
# display data
print("\nDisplay dataframe with ticks")
print(ticks_frame.head(10)
 
结果:
MetaTrader5程序包作者:MetaQuotes Software Corp.
MetaTrader5程序包版本:5.0.29
 
已接收报价:37008
将获得的报价显示为“保持原来状态”
(1578614400, 0.68577, 0.68594, 0., 0, 1578614400820, 134, 0.)
(1578614401, 0.68578, 0.68594, 0., 0, 1578614401128, 130, 0.)
(1578614401, 0.68575, 0.68594, 0., 0, 1578614401128, 130, 0.)
(1578614411, 0.68576, 0.68594, 0., 0, 1578614411388, 130, 0.)
(1578614411, 0.68575, 0.68594, 0., 0, 1578614411560, 130, 0.)
(1578614414, 0.68576, 0.68595, 0., 0, 1578614414973, 134, 0.)
(1578614430, 0.68576, 0.68594, 0., 0, 1578614430188, 4, 0.)
(1578614450, 0.68576, 0.68595, 0., 0, 1578614450408, 4, 0.)
(1578614450, 0.68576, 0.68594, 0., 0, 1578614450519, 4, 0.)
(1578614456, 0.68575, 0.68594, 0., 0, 1578614456363, 130, 0.)
 
显示带有报价的数据框
                 time      bid      ask  last  volume       time_msc  flags  volume_real
0 2020-01-10 00:00:00  0.68577  0.68594   0.0       0  1578614400820    134          0.0
1 2020-01-10 00:00:01  0.68578  0.68594   0.0       0  1578614401128    130          0.0
2 2020-01-10 00:00:01  0.68575  0.68594   0.0       0  1578614401128    130          0.0
3 2020-01-10 00:00:11  0.68576  0.68594   0.0       0  1578614411388    130          0.0
4 2020-01-10 00:00:11  0.68575  0.68594   0.0       0  1578614411560    130          0.0
5 2020-01-10 00:00:14  0.68576  0.68595   0.0       0  1578614414973    134          0.0
6 2020-01-10 00:00:30  0.68576  0.68594   0.0       0  1578614430188      4          0.0
7 2020-01-10 00:00:50  0.68576  0.68595   0.0       0  1578614450408      4          0.0
8 2020-01-10 00:00:50  0.68576  0.68594   0.0       0  1578614450519      4          0.0
9 2020-01-10 00:00:56  0.68575  0.68594   0.0       0  1578614456363    130          0.0

另见

CopyRatescopy_rates_from_poscopy_rates_rangecopy_ticks_fromcopy_ticks_range