copy_rates_range

从MetaTrader 5程序端获取指定日期范围内的柱形图。

copy_rates_range(

   symbol,       // 交易品种名称

   timeframe,    // 时间周期

   date_from,    // 请求柱形图的开始日期

   date_to       // 请求柱形图的结束日期

   )

参数

symbol

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

timeframe

[in]  请求柱形图的时间周期。根据TIMEFRAME枚举的值设置。所需的未命名参数。

date_from

[in]  请求柱形图的开始日期。通过'datetime'对象或根据1970.01.01以来过去的秒数设置。包括开盘时间的柱形图 >= 返回date_from。所需的未命名参数。

date_to

[in]  请求柱形图的结束日期。通过'datetime'对象或根据1970.01.01以来过去的秒数设置。包括开盘时间的柱形图 <= 返回date_to。所需的未命名参数。

返回值

返回numpy数据柱形图(包含以下指定列:时间、开盘价、最高价、最低价、收盘价、tick_volume、点差和real_volume)。错误情况下返回None。可使用last_error()获取错误信息。

注意

参见CopyRates()函数获取更多信息。

MetaTrader 5程序端仅在用户可用的图表历史记录中提供柱形图。可供用户使用的柱形图数量设置在“图表中最大数量(Max.)柱形图”参数中。

当创建'datetime'对象时,Python使用本地时区,而MetaTrader 5以UTC时区保存报价和柱形图开盘时间(没有时移)。 因此,'datetime'应在UTC时间内创建,用于执行使用时间的函数。从MetaTrader 5程序端接收到的数据使用UTC时间。

例如:

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



# 建立与MetaTrader 5程序端的连接

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, hour = 13, tzinfo=timezone)

# get bars from USDJPY M5 within the interval of 2020.01.10 00:00 - 2020.01.11 13:00 in UTC time zone

rates = mt5.copy_rates_range("USDJPY", mt5.TIMEFRAME_M5, utc_from, utc_to)



# shut down connection to the MetaTrader 5 terminal

mt5.shutdown()



# display each element of obtained data in a new line

print("Display obtained data 'as is'")

counter=0

for rate in rates:

    counter+=1

    if counter<=10:

        print(rate)



# create DataFrame out of the obtained data

rates_frame = pd.DataFrame(rates)

# convert time in seconds into the 'datetime' format

rates_frame['time']=pd.to_datetime(rates_frame['time'], unit='s')



# display data

print("\nDisplay dataframe with data")

print(rates_frame.head(10))



结果:

MetaTrader5程序包作者:MetaQuotes Software Corp.

MetaTrader5程序包版本:5.0.29



将获得的数据显示为“保持原来状态”

(1578614400, 109.513, 109.527, 109.505, 109.521, 43, 2, 0)

(1578614700, 109.521, 109.549, 109.518, 109.543, 215, 8, 0)

(1578615000, 109.543, 109.543, 109.466, 109.505, 98, 10, 0)

(1578615300, 109.504, 109.534, 109.502, 109.517, 155, 8, 0)

(1578615600, 109.517, 109.539, 109.513, 109.527, 71, 4, 0)

(1578615900, 109.526, 109.537, 109.484, 109.52, 106, 9, 0)

(1578616200, 109.52, 109.524, 109.508, 109.51, 205, 7, 0)

(1578616500, 109.51, 109.51, 109.491, 109.496, 44, 8, 0)

(1578616800, 109.496, 109.509, 109.487, 109.5, 85, 5, 0)

(1578617100, 109.5, 109.504, 109.487, 109.489, 82, 7, 0)



显示带有数据的数据框

                 time     open     high      low    close  tick_volume  spread  real_volume

0 2020-01-10 00:00:00  109.513  109.527  109.505  109.521           43       2            0

1 2020-01-10 00:05:00  109.521  109.549  109.518  109.543          215       8            0

2 2020-01-10 00:10:00  109.543  109.543  109.466  109.505           98      10            0

3 2020-01-10 00:15:00  109.504  109.534  109.502  109.517          155       8            0

4 2020-01-10 00:20:00  109.517  109.539  109.513  109.527           71       4            0

5 2020-01-10 00:25:00  109.526  109.537  109.484  109.520          106       9            0

6 2020-01-10 00:30:00  109.520  109.524  109.508  109.510          205       7            0

7 2020-01-10 00:35:00  109.510  109.510  109.491  109.496           44       8            0

8 2020-01-10 00:40:00  109.496  109.509  109.487  109.500           85       5            0

9 2020-01-10 00:45:00  109.500  109.504  109.487  109.489           82       7            0

另见

CopyRatescopy_rates_fromcopy_rates_rangecopy_ticks_fromcopy_ticks_range