from datetime import datetime
import MetaTrader5 as mt5
# 显示有关MetaTrader 5程序包的数据
print("MetaTrader5 package author: ",mt5.__author__)
print("MetaTrader5 package version: ",mt5.__version__)
# 导入'pandas'模块,用于以表格形式显示获得的数据
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
# 导入用于处理时区的pytz模块
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' object in UTC time zone to avoid the implementation of a local time zone offset
utc_from = datetime(2020, 1, 10, tzinfo=timezone)
# request 100 000 EURUSD ticks starting from 10.01.2019 in UTC time zone
ticks = mt5.copy_ticks_from("EURUSD", utc_from, 100000, mt5.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)
# 将时间(以秒为单位)转换为日期时间格式
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
已接收报价:100000
将获得的报价显示为“保持原来状态”
(1578614400, 1.11051, 1.11069, 0., 0, 1578614400987, 134, 0.)
(1578614402, 1.11049, 1.11067, 0., 0, 1578614402025, 134, 0.)
(1578614404, 1.1105, 1.11066, 0., 0, 1578614404057, 134, 0.)
(1578614404, 1.11049, 1.11067, 0., 0, 1578614404344, 134, 0.)
(1578614412, 1.11052, 1.11064, 0., 0, 1578614412106, 134, 0.)
(1578614418, 1.11039, 1.11051, 0., 0, 1578614418265, 134, 0.)
(1578614418, 1.1104, 1.1105, 0., 0, 1578614418905, 134, 0.)
(1578614419, 1.11039, 1.11051, 0., 0, 1578614419519, 134, 0.)
(1578614456, 1.11037, 1.11065, 0., 0, 1578614456011, 134, 0.)
(1578614456, 1.11039, 1.11051, 0., 0, 1578614456015, 134, 0.)
显示带有报价的数据框
time bid ask last volume time_msc flags volume_real
0 2020-01-10 00:00:00 1.11051 1.11069 0.0 0 1578614400987 134 0.0
1 2020-01-10 00:00:02 1.11049 1.11067 0.0 0 1578614402025 134 0.0
2 2020-01-10 00:00:04 1.11050 1.11066 0.0 0 1578614404057 134 0.0
3 2020-01-10 00:00:04 1.11049 1.11067 0.0 0 1578614404344 134 0.0
4 2020-01-10 00:00:12 1.11052 1.11064 0.0 0 1578614412106 134 0.0
5 2020-01-10 00:00:18 1.11039 1.11051 0.0 0 1578614418265 134 0.0
6 2020-01-10 00:00:18 1.11040 1.11050 0.0 0 1578614418905 134 0.0
7 2020-01-10 00:00:19 1.11039 1.11051 0.0 0 1578614419519 134 0.0
8 2020-01-10 00:00:56 1.11037 1.11065 0.0 0 1578614456011 134 0.0
9 2020-01-10 00:00:56 1.11039 1.11051 0.0 0 1578614456015 134 0.0
|