-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathreal_time_streaming.py
More file actions
141 lines (124 loc) · 5.83 KB
/
Copy pathreal_time_streaming.py
File metadata and controls
141 lines (124 loc) · 5.83 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import os
import asyncio
from metaapi_cloud_sdk import MetaApi
from datetime import datetime, timedelta
# Note: for information on how to use this example code please read https://metaapi.cloud/docs/client/usingCodeExamples/
token = os.getenv('TOKEN') or '<put in your token here>'
login = os.getenv('LOGIN') or '<put in your MT login here>'
password = os.getenv('PASSWORD') or '<put in your MT password here>'
server_name = os.getenv('SERVER') or '<put in your MT server name here>'
async def meta_api_synchronization():
api = MetaApi(token)
try:
# Add test MetaTrader account
accounts = await api.metatrader_account_api.get_accounts_with_infinite_scroll_pagination()
account = None
for item in accounts:
if item.login == login and item.type.startswith('cloud'):
account = item
break
if not account:
print('Adding MT5 account to MetaApi')
account = await api.metatrader_account_api.create_account(
{
'name': 'Test account',
'type': 'cloud',
'login': login,
'password': password,
'server': server_name,
'platform': 'mt5',
'application': 'MetaApi',
'magic': 1000,
}
)
else:
print('MT5 account already added to MetaApi')
# wait until account is deployed and connected to broker
print('Deploying account')
await account.deploy()
print('Waiting for API server to connect to broker (may take couple of minutes)')
await account.wait_connected()
# connect to MetaApi API
connection = account.get_streaming_connection()
await connection.connect()
# wait until terminal state synchronized to the local state
print('Waiting for SDK to synchronize to terminal state (may take some time depending on your history size)')
await connection.wait_synchronized({'timeoutInSeconds': 600})
# access local copy of terminal state
print('Testing terminal state access')
terminal_state = connection.terminal_state
print('connected:', terminal_state.connected)
print('connected to broker:', terminal_state.connected_to_broker)
print('account information:', terminal_state.account_information)
print('positions:', terminal_state.positions)
print('orders:', terminal_state.orders)
print('specifications:', terminal_state.specifications)
print('EURUSD specification:', terminal_state.specification('EURUSD'))
# access history storage
history_storage = connection.history_storage
print('deals:', history_storage.deals[-5:])
print('deals with id=1:', history_storage.get_deals_by_ticket('1'))
print('deals with positionId=1:', history_storage.get_deals_by_position('1'))
print(
'deals for the last day:',
history_storage.get_deals_by_time_range(
datetime.fromtimestamp(datetime.now().timestamp() - 24 * 60 * 60), datetime.now()
),
)
print('history orders:', history_storage.history_orders[-5:])
print('history orders with id=1:', history_storage.get_history_orders_by_ticket('1'))
print('history orders with positionId=1:', history_storage.get_history_orders_by_position('1'))
print(
'history orders for the last day:',
history_storage.get_history_orders_by_time_range(
datetime.fromtimestamp(datetime.now().timestamp() - 24 * 60 * 60), datetime.now()
),
)
await connection.subscribe_to_market_data('EURUSD')
print('EURUSD price:', terminal_state.price('EURUSD'))
# calculate margin required for trade
print(
'margin required for trade',
await connection.calculate_margin(
{'symbol': 'GBPUSD', 'type': 'ORDER_TYPE_BUY', 'volume': 0.1, 'openPrice': 1.1}
),
)
# trade
print('Submitting pending order')
try:
result = await connection.create_limit_buy_order(
'GBPUSD', 0.07, 1.0, 0.9, 2.0, {
'comment': 'comm',
'clientId': 'TE_GBPUSD_7hyINWqAlE',
'expiration': {
'type': 'ORDER_TIME_SPECIFIED',
'time': datetime.now() + timedelta(days=1)
}
}
)
print('Trade successful, result code is ' + result['stringCode'])
except Exception as err:
print('Trade failed with error:')
print(api.format_error(err))
# finally, undeploy account after the test
print('Undeploying MT5 account so that it does not consume any unwanted resources')
await connection.close()
await account.undeploy()
except Exception as err:
# process errors
if hasattr(err, 'details'):
# returned if the server file for the specified server name has not been found
# recommended to check the server name or create the account using a provisioning profile
if err.details == 'E_SRV_NOT_FOUND':
print(err)
# returned if the server has failed to connect to the broker using your credentials
# recommended to check your login and password
elif err.details == 'E_AUTH':
print(err)
# returned if the server has failed to detect the broker settings
# recommended to try again later or create the account using a provisioning profile
elif err.details == 'E_SERVER_TIMEZONE':
print(err)
print(api.format_error(err))
exit()
asyncio.run(meta_api_synchronization())