-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch_orders.py
More file actions
126 lines (91 loc) · 2.79 KB
/
fetch_orders.py
File metadata and controls
126 lines (91 loc) · 2.79 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
import asyncio as aio
import json
import time
from types import SimpleNamespace
from webSocket import WebSocket
parsejson = lambda s: json.loads(s, object_hook=lambda d: SimpleNamespace(**d))
def readfile(path):
with open(path) as f:
return parsejson(f.read())
from_ts = None
creds = None
oa = None
pt = None
_ws = None
_closed = None
def construct_msg(payloadType, fields=None, clientMsgId=None):
if fields is None:
fields = {}
msg = {
"payloadType": payloadType,
"payload": {
"ctidTraderAccountId": creds.accountId,
"accessToken": creds.accessToken,
**fields
}
}
if clientMsgId:
msg["clientMsgId"] = clientMsgId
return json.dumps(msg)
async def on_ready(ws):
print("✅ account authenticated – ready")
msg = construct_msg(pt.req.OrderList, {
'fromTimestamp': from_ts,
'toTimestamp': int(time.time() * 1000)
})
await ws.send(msg)
async def on_resp(ws, msg_str):
msg = json.loads(msg_str)
payloadType = msg["payloadType"]
payload = msg["payload"]
if payloadType == pt.res.OrderList:
with open("data/orders.json", "w") as f:
json.dump(payload["order"], f, indent=2)
print("📁 written to orders.json")
await ws.close()
else:
print("unknown payloadType:")
print(msg_str)
async def _on_open():
print("🔌 connected")
msg = {
"payloadType": pt.req.ApplicationAuth,
"payload": {
"clientId": creds.clientId,
"clientSecret": creds.clientSecret
}
}
await _ws.send(json.dumps(msg))
async def _on_message(msg_str):
msg = parsejson(msg_str)
if msg.payloadType == pt.res.ApplicationAuth:
print("🔐 application authenticated")
await _ws.send(construct_msg(pt.req.AccountAuth))
return
if msg.payloadType == pt.res.AccountAuth:
print("🔐 account authenticated")
await on_ready(_ws)
return
if msg.payloadType == pt.common.HeartbeatEvent:
await _ws.send(json.dumps({"payloadType": pt.common.HeartbeatEvent}))
return
await on_resp(_ws, msg_str)
async def _on_close():
print("❌ connection closed")
_closed.set()
async def _on_error(err):
print("⚠️ websocket error:", err)
_closed.set()
async def fetch(from_when):
global creds, oa, pt, _ws, _closed, from_ts
from_ts = from_when
creds = readfile("./credentials.json")
oa = readfile("models/OAModel.custom.json")
pt = readfile("models/payloadTypes.custom.json")
_closed = aio.Event()
_ws = WebSocket("wss://live.ctraderapi.com:5036")
_ws.onopen = _on_open
_ws.onmessage = _on_message
_ws.onclose = _on_close
_ws.onerror = _on_error
await _closed.wait()