-
Notifications
You must be signed in to change notification settings - Fork 0
/
MetaTraderData.py
76 lines (58 loc) · 2.53 KB
/
MetaTraderData.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from datetime import datetime
import matplotlib.pyplot as plt
import pandas as pd
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()
import MetaTrader5 as mt5
# connect to MetaTrader 5
if not mt5.initialize():
print("initialize() failed")
mt5.shutdown()
# request connection status and parameters
#print(mt5.terminal_info())
# # get data on MetaTrader 5 version
#print(mt5.version())
# # request 1000 ticks from EURAUD
# euraud_ticks = mt5.copy_ticks_from("EURAUD", datetime(2020,1,28,13), 1000, mt5.COPY_TICKS_ALL)
# # request ticks from AUDUSD within 2019.04.01 13:00 - 2019.04.02 13:00
# audusd_ticks = mt5.copy_ticks_range("AUDUSD", datetime(2020,1,27,13), datetime(2020,1,28,13), mt5.COPY_TICKS_ALL)
# # get bars from different symbols in a number of ways
eurusd_rates = mt5.copy_rates_from("EURUSD", mt5.TIMEFRAME_M1, datetime(2020,1,28,13), 1000)
eurgbp_rates = mt5.copy_rates_from_pos("EURGBP", mt5.TIMEFRAME_M1, 0, 1000)
eurcad_rates = mt5.copy_rates_range("EURCAD", mt5.TIMEFRAME_W1, datetime(2020,1,27,13), datetime(2023,1,28,13))
# # shut down connection to MetaTrader 5
# mt5.shutdown()
#DATA
# print('euraud_ticks(', len(euraud_ticks), ')')
# for val in euraud_ticks[:10]: print(val)
# print('audusd_ticks(', len(audusd_ticks), ')')
# for val in audusd_ticks[:10]: print(val)
# print('audusd_ticks(', len(audusd_ticks), ')')
# for val in audusd_ticks[:10]: print(val)
# print('eurusd_rates(', len(eurusd_rates), ')')
# for val in eurusd_rates[:10]: print(val)
# print('eurgbp_rates(', len(eurgbp_rates), ')')
# for val in eurgbp_rates[:10]: print(val)
#sprint('eurcad_rates(', len(eurcad_rates), ')')
# for val in eurcad_rates:
# print(val)
print(eurcad_rates)
# create DataFrame out of the obtained data
eurcad_rates = pd.DataFrame(eurcad_rates)
# convert time in seconds into the datetime format
eurcad_rates['time']=pd.to_datetime(eurcad_rates['time'], unit='s')
print(eurcad_rates)
# #PLOT
# # create DataFrame out of the obtained data
# ticks_frame = pd.DataFrame(euraud_ticks)
# # convert time in seconds into the datetime format
# ticks_frame['time']=pd.to_datetime(ticks_frame['time'], unit='s')
# # display ticks on the chart
# plt.plot(ticks_frame['time'], ticks_frame['ask'], 'r-', label='ask')
# plt.plot(ticks_frame['time'], ticks_frame['bid'], 'b-', label='bid')
# # display the legends
# plt.legend(loc='upper left')
# # add the header
# plt.title('EURAUD ticks')
# # display the chart
# plt.show()