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) # 表示する表の最高幅
# MetaTrader 5ターミナルとの接続を確立する
if not mt5.initialize():
print("initialize() failed, error code =",mt5.last_error())
quit()
# 今日の10のGBPUSD D1バーを取得する
rates = mt5.copy_rates_from_pos("GBPUSD", mt5.TIMEFRAME_D1, 0, 10)
# MetaTrader 5ターミナルへの接続をシャットダウンする
mt5.shutdown()
# 取得したデータの各要素を新しい行に表示する
print("Display obtained data 'as is'")
for rate in rates:
print(rate)
# 取得したデータからDataFrameを作成する
rates_frame = pd.DataFrame(rates)
# 秒での時間をdatetime形式に変換する
rates_frame['time']=pd.to_datetime(rates_frame['time'], unit='s')
# データを表示する
print("\nDisplay dataframe with data")
print(rates_frame)
結果:
MetaTrader5 package author: MetaQuotes Software Corp.
MetaTrader5 package version: 5.0.29
Display obtained data 'as is'
(1581552000, 1.29568, 1.30692, 1.29441, 1.30412, 68228, 0, 0)
(1581638400, 1.30385, 1.30631, 1.3001, 1.30471, 56498, 0, 0)
(1581897600, 1.30324, 1.30536, 1.29975, 1.30039, 49400, 0, 0)
(1581984000, 1.30039, 1.30486, 1.29705, 1.29952, 62288, 0, 0)
(1582070400, 1.29952, 1.3023, 1.29075, 1.29187, 57909, 0, 0)
(1582156800, 1.29186, 1.29281, 1.28489, 1.28792, 61033, 0, 0)
(1582243200, 1.28802, 1.29805, 1.28746, 1.29566, 66386, 0, 0)
(1582502400, 1.29426, 1.29547, 1.28865, 1.29283, 66933, 0, 0)
(1582588800, 1.2929, 1.30178, 1.29142, 1.30037, 80121, 0, 0)
(1582675200, 1.30036, 1.30078, 1.29136, 1.29374, 49286, 0, 0)
Display dataframe with data
time open high low close tick_volume spread real_volume
0 2020-02-13 1.29568 1.30692 1.29441 1.30412 68228 0 0
1 2020-02-14 1.30385 1.30631 1.30010 1.30471 56498 0 0
2 2020-02-17 1.30324 1.30536 1.29975 1.30039 49400 0 0
3 2020-02-18 1.30039 1.30486 1.29705 1.29952 62288 0 0
4 2020-02-19 1.29952 1.30230 1.29075 1.29187 57909 0 0
5 2020-02-20 1.29186 1.29281 1.28489 1.28792 61033 0 0
6 2020-02-21 1.28802 1.29805 1.28746 1.29566 66386 0 0
7 2020-02-24 1.29426 1.29547 1.28865 1.29283 66933 0 0
8 2020-02-25 1.29290 1.30178 1.29142 1.30037 80121 0 0
9 2020-02-26 1.30036 1.30078 1.29136 1.29374 49286 0 0
|