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) # 表示する列の数
pd.set_option('display.width', 1500) # 表示するテーブルの最大幅
# タイムゾーンを使用するためのpytzモジュールをインポートする
import pytz
# MetaTrader 5ターミナルとの接続を確立する
if not mt5.initialize():
print("initialize() failed, error code =",mt5.last_error())
quit()
# タイムゾーンをUTCに設定する
timezone = pytz.timezone("Etc/UTC")
# ローカルタイムゾーンオフセットの実装を回避するために、UTCタイムゾーンで「datetime」オブジェクトを作成する
utc_from = datetime(2020, 1, 10, tzinfo=timezone)
utc_to = datetime(2020, 1, 11, tzinfo=timezone)
# 11.01.2020 - 11.01.2020からのAUDUSDティックをリクエストする
ticks = mt5.copy_ticks_range("AUDUSD", utc_from, utc_to, mt5.COPY_TICKS_ALL)
print("Ticks received:",len(ticks))
# MetaTrader 5ターミナルへの接続をシャットダウンする
mt5.shutdown()
# 各ティックのデータを新しい行に表示する
print("Display obtained ticks 'as is'")
count = 0
for tick in ticks:
count+=1
print(tick)
if count >= 10:
break
# 取得したデータからDataFrameを作成する
ticks_frame = pd.DataFrame(ticks)
# 秒での時間をdatetime形式に変換する
ticks_frame['time']=pd.to_datetime(ticks_frame['time'], unit='s')
# データを表示する
print("\nDisplay dataframe with ticks")
print(ticks_frame.head(10))
結果:
MetaTrader5 package author: MetaQuotes Software Corp.
MetaTrader5 package version: 5.0.29
Ticks received: 37008
Display obtained ticks 'as is'
(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.)
Display dataframe with ticks
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
|