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_to, 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)
# 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
|