Repository navigation
Expand file tree
/
Copy pathrpc_example.py
More file actions
94 lines (80 loc) · 3.82 KB
/
Copy pathrpc_example.py
File metadata and controls
94 lines (80 loc) · 3.82 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
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>'
accountId = os.getenv('ACCOUNT_ID') or '<put in your account id here>'
async def test_meta_api_synchronization():
api = MetaApi(token)
try:
account = await api.metatrader_account_api.get_account(accountId)
initial_state = account.state
deployed_states = ['DEPLOYING', 'DEPLOYED']
if initial_state not in deployed_states:
# 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_rpc_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()
# invoke RPC API (replace ticket numbers with actual ticket numbers which exist in your MT account)
print('Testing MetaAPI RPC API')
print('account information:', await connection.get_account_information())
print('positions:', await connection.get_positions())
# print(await connection.get_position('1234567'))
print('open orders:', await connection.get_orders())
# print(await connection.get_order('1234567'))
print('history orders by ticket:', await connection.get_history_orders_by_ticket('1234567'))
print('history orders by position:', await connection.get_history_orders_by_position('1234567'))
print(
'history orders (~last 3 months):',
await connection.get_history_orders_by_time_range(
datetime.utcnow() - timedelta(days=90), datetime.utcnow()
),
)
print('history deals by ticket:', await connection.get_deals_by_ticket('1234567'))
print('history deals by position:', await connection.get_deals_by_position('1234567'))
print(
'history deals (~last 3 months):',
await connection.get_deals_by_time_range(datetime.utcnow() - timedelta(days=90), datetime.utcnow()),
)
print('server time', await connection.get_server_time())
# 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))
if initial_state not in deployed_states:
# undeploy account if it was undeployed
print('Undeploying account')
await connection.close()
await account.undeploy()
except Exception as err:
print(api.format_error(err))
exit()
asyncio.run(test_meta_api_synchronization())