-
Notifications
You must be signed in to change notification settings - Fork 1
/
place_orders.py
522 lines (392 loc) · 16.9 KB
/
place_orders.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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
#!/usr/bin/env python3
'''
Created on August 4, 2024
Author: Rufus Ayeni
Contact: https://github.com/rayeni/python_rithmic_trading_app/discussions
'''
#################################################################
# LIBRARY IMPORTS #
#################################################################
import asyncio
import google.protobuf.message
import pathlib
import ssl
import websockets
from protobuf import base_pb2
from protobuf import request_account_list_pb2
from protobuf import response_account_list_pb2
from protobuf import request_heartbeat_pb2
from protobuf import request_rithmic_system_info_pb2
from protobuf import response_rithmic_system_info_pb2
from protobuf import request_login_pb2
from protobuf import response_login_pb2
from protobuf import request_login_info_pb2
from protobuf import response_login_info_pb2
from protobuf import request_logout_pb2
from protobuf import request_trade_routes_pb2
from protobuf import response_trade_routes_pb2
from protobuf import request_subscribe_for_order_updates_pb2
from protobuf import request_new_order_pb2
from protobuf import exchange_order_notification_pb2
from protobuf import rithmic_order_notification_pb2
class PlaceOrderApp:
def __init__(self, uri, system_name, user_id, password, exchange, symbol):
self.uri = uri
self.system_name = system_name
self.user_id = user_id
self.password = password
self.g_exchange = exchange
self.g_symbol = symbol
self.g_rcvd_account = False
self.g_fcm_id = ""
self.g_ib_id = ""
self.g_account_id = ""
self.g_rcvd_trade_route = False
self.g_trade_route = ""
self.g_order_is_complete = False
self.ssl_context = None
async def rithmic_order_notification_cb(self, msg_buf):
'''
Interpret the msg_buf as a RithmicOrderNotification, 351.
:param msg_buf: bytes
A binary message sent from Rithmic's order plant.
'''
msg = rithmic_order_notification_pb2.RithmicOrderNotification()
msg.ParseFromString(msg_buf[4:])
print(f"")
print(f" RithmicOrderNotification : ")
print(f" status : {msg.status}")
if msg.status == "complete" or \
msg.notify_type == rithmic_order_notification_pb2.RithmicOrderNotification.COMPLETE:
self.g_order_is_complete = True
async def exchange_order_notification_cb(self, msg_buf):
'''
Interpret the msg_buf as a ExchangeOrderNotification, 352.
:param msg_buf: bytes
A binary message sent from Rithmic's order plant.
'''
msg = exchange_order_notification_pb2.ExchangeOrderNotification()
msg.ParseFromString(msg_buf[4:])
print(f"")
print(f" ExchangeOrderNotification : ")
print(f" status : {msg.status}")
async def connect_to_rithmic(self):
'''
Connect to the specified URI and return a
websocket connection object.
'''
# disable ping keep-alive mechanism
ws = await websockets.connect(self.uri, ssl=self.ssl_context, ping_interval=3)
print(f"connected to {self.uri}")
return (ws)
async def send_heartbeat(self, ws):
rq = request_heartbeat_pb2.RequestHeartbeat()
rq.template_id = 18
serialized = rq.SerializeToString()
length = len(serialized)
# length into bytes (4 bytes, big/little, true/false)
buf = bytearray()
buf = length.to_bytes(4, byteorder='big', signed=True)
buf += serialized
await ws.send(buf)
print(f"sent heartbeat request")
async def list_systems(self, ws):
'''
Request the list of available Rithmic systems from the server.
'''
rq = request_rithmic_system_info_pb2.RequestRithmicSystemInfo()
rq.template_id = 16
rq.user_msg.append("hello");
rq.user_msg.append("world");
serialized = rq.SerializeToString()
length = len(serialized)
# length into bytes (4 bytes, big/little, true/false)
buf = bytearray()
buf = length.to_bytes(4, byteorder='big', signed=True)
buf += serialized
await ws.send(buf)
print(f"sent list_systems request")
rp_buf = bytearray()
rp_buf = await ws.recv()
# get length from first four bytes from rp_buf
rp_length = int.from_bytes(rp_buf[0:3], byteorder='big', signed=True)
rp = response_rithmic_system_info_pb2.ResponseRithmicSystemInfo()
rp.ParseFromString(rp_buf[4:])
# an rp code of "0" indicates that the request was completed successfully
if rp.rp_code[0] == "0":
print(f" Available Systems :")
print(f" ===================")
for sys_name in rp.system_name:
print(f"{sys_name}")
else:
print(f" error retrieving system list :")
print(f" template_id : {rp.template_id}")
print(f" user_msg : {rp.user_msg}")
print(f" rp code : {rp.rp_code}")
print(f" system_name : {rp.system_name}")
async def consume(self, ws):
'''
Read data off the wire. Occassionally send heartbeats if
there is no traffic.
'''
# send a heartbeat immediately, just in case
await self.send_heartbeat(ws)
max_num_msgs = 200000
num_msgs = 0
# After 100 messages are read, this routine will exit
while num_msgs < max_num_msgs and self.g_order_is_complete == False:
msg_buf = bytearray()
waiting_for_msg = True
while waiting_for_msg:
try:
#print(f"waiting for msg ...")
msg_buf = await asyncio.wait_for(ws.recv(), timeout=5)
waiting_for_msg = False
except asyncio.TimeoutError:
if ws.open:
print(f"sending heartbeat ...")
await self.send_heartbeat(ws)
else:
print(f"connection appears to be closed. exiting consume()")
return;
num_msgs += 1
# get length from first four bytes from msg_buf
msg_length = int.from_bytes(msg_buf[0:3], byteorder='big', signed=True)
# parse into base class just to get a template id
base = base_pb2.Base()
base.ParseFromString(msg_buf[4:])
# route msg based on template id
if base.template_id == 13:
msg_type = "logout response"
#print(f" consumed msg : {msg_type} ({base.template_id})")
elif base.template_id == 19:
msg_type = "heartbeat response"
#print(f" consumed msg : {msg_type} ({base.template_id})")
elif base.template_id == 309: # response to subscribe_for_order_updates
msg_type = "response_subscribe_for_order_updates"
#print(f" consumed msg : {msg_type} ({base.template_id})")
elif base.template_id == 313: # response to new_order
msg_type = "response_new_order"
#print(f" consumed msg : {msg_type} ({base.template_id})")
elif base.template_id == 351: # rithmic_order_notification
msg_type = "rithmic_order_notification"
#print(f" consumed msg : {msg_type} ({base.template_id})")
await self.rithmic_order_notification_cb(msg_buf)
elif base.template_id == 352: # exchange_order_notification
msg_type = "exchange_order_notification"
#print(f" consumed msg : {msg_type} ({base.template_id})")
await self.exchange_order_notification_cb(msg_buf)
if self.g_order_is_complete == True:
print(f"order is complete ...")
async def rithmic_login(self, ws, infra_type):
'''
Log into the specified Rithmic system using the specified
credentials and wait for the login response.
'''
rq = request_login_pb2.RequestLogin()
rq.template_id = 10;
rq.template_version = "3.9"
rq.user_msg.append("hello")
rq.user = self.user_id
rq.password = self.password
rq.app_name = "CHANGE_ME:place_orders.py"
rq.app_version = "1.0.0"
rq.system_name = self.system_name
rq.infra_type = infra_type
serialized = rq.SerializeToString()
length = len(serialized)
buf = bytearray()
buf = length.to_bytes(4, byteorder = 'big', signed=True)
buf += serialized
await ws.send(buf)
rp_buf = bytearray()
rp_buf = await ws.recv()
# get length from first four bytes from rp_buf
rp_length = int.from_bytes(rp_buf[0:3], byteorder='big', signed=True)
rp = response_login_pb2.ResponseLogin()
rp.ParseFromString(rp_buf[4:])
async def login_info(self, ws):
'''
Retrieve additional info about the currently logged in user.
'''
rq = request_login_info_pb2.RequestLoginInfo()
rq.template_id = 300;
rq.user_msg.append("hello")
serialized = rq.SerializeToString()
length = len(serialized)
buf = bytearray()
buf = length.to_bytes(4, byteorder = 'big', signed=True)
buf += serialized
await ws.send(buf)
rp_buf = bytearray()
rp_buf = await ws.recv()
# get length from first four bytes from rp_buf
rp_length = int.from_bytes(rp_buf[0:3], byteorder='big', signed=True)
rp = response_login_info_pb2.ResponseLoginInfo()
rp.ParseFromString(rp_buf[4:])
if rp.rp_code[0] == '0':
print(f"retrieving account list ...")
await self.list_accounts(ws, rp.fcm_id, rp.ib_id, rp.user_type)
print(f"retrieving trade routes ...")
await self.list_trade_routes(ws)
async def list_accounts(self, ws, fcm_id, ib_id, user_type):
'''
Retrieve the list of accounts that the currently logged in
user has permission to trade on.
'''
rq = request_account_list_pb2.RequestAccountList()
rq.template_id = 302;
rq.user_msg.append("hello")
rq.fcm_id = fcm_id
rq.ib_id = ib_id
rq.user_type = user_type
serialized = rq.SerializeToString()
length = len(serialized)
buf = bytearray()
buf = length.to_bytes(4, byteorder = 'big', signed=True)
buf += serialized
rp_is_done = False
await ws.send(buf)
rp_buf = bytearray()
while rp_is_done == False:
rp_buf = await ws.recv()
# get length from first four bytes from rp_buf
rp_length = int.from_bytes(rp_buf[0:3], byteorder='big', signed=True)
rp = response_account_list_pb2.ResponseAccountList()
rp.ParseFromString(rp_buf[4:])
# store the first acount we get for placing the order
if self.g_rcvd_account == False and \
len(rp.rq_handler_rp_code) > 0 and \
rp.rq_handler_rp_code[0] == "0" and \
len(rp.fcm_id) > 0 and \
len(rp.ib_id) > 0 and \
len(rp.account_id) > 0:
self.g_fcm_id = rp.fcm_id
self.g_ib_id = rp.ib_id
self.g_account_id = rp.account_id
self.g_rcvd_account = True
if len(rp.rp_code) > 0:
rp_is_done = True
async def list_trade_routes(self, ws):
'''
Retrieves the list of trade routes from the order plant.
'''
rq = request_trade_routes_pb2.RequestTradeRoutes()
rq.template_id = 310;
rq.user_msg.append("hello")
rq.subscribe_for_updates = False
serialized = rq.SerializeToString()
length = len(serialized)
buf = bytearray()
buf = length.to_bytes(4, byteorder = 'big', signed=True)
buf += serialized
rp_is_done = False
await ws.send(buf)
rp_buf = bytearray()
while rp_is_done == False:
rp_buf = await ws.recv()
# get length from first four bytes from rp_buf
rp_length = int.from_bytes(rp_buf[0:3], byteorder='big', signed=True)
rp = response_trade_routes_pb2.ResponseTradeRoutes()
rp.ParseFromString(rp_buf[4:])
# store the first applicable trade route we get
if self.g_rcvd_trade_route == False and \
len(rp.rq_handler_rp_code) > 0 and \
rp.rq_handler_rp_code[0] == "0" and \
self.g_fcm_id == rp.fcm_id and \
self.g_ib_id == rp.ib_id and \
self.g_exchange == rp.exchange:
self.g_trade_route = rp.trade_route
self.g_rcvd_trade_route = True
if len(rp.rp_code) > 0:
rp_is_done = True
async def subscribe_for_order_updates(self, ws):
'''
Subscribe to updates on any orders on the specified fcm, ib
and account.
'''
rq = request_subscribe_for_order_updates_pb2.RequestSubscribeForOrderUpdates()
rq.template_id = 308;
rq.user_msg.append("hello")
rq.fcm_id = self.g_fcm_id
rq.ib_id = self.g_ib_id
rq.account_id = self.g_account_id
serialized = rq.SerializeToString()
length = len(serialized)
buf = bytearray()
buf = length.to_bytes(4, byteorder = 'big', signed=True)
buf += serialized
await ws.send(buf)
async def new_order(self, ws, side, qty):
'''
Submit a request for a new order.
'''
rq = request_new_order_pb2.RequestNewOrder()
rq.template_id = 312;
rq.user_msg.append("hello")
rq.fcm_id = self.g_fcm_id
rq.ib_id = self.g_ib_id
rq.account_id = self.g_account_id
rq.exchange = self.g_exchange
rq.symbol = self.g_symbol
rq.quantity = qty
if side == "B" or side == "b":
rq.transaction_type = request_new_order_pb2.RequestNewOrder.TransactionType.BUY
else:
rq.transaction_type = request_new_order_pb2.RequestNewOrder.TransactionType.SELL
rq.duration = request_new_order_pb2.RequestNewOrder.Duration.DAY
rq.price_type = request_new_order_pb2.RequestNewOrder.PriceType.MARKET
rq.manual_or_auto = request_new_order_pb2.RequestNewOrder.OrderPlacement.AUTO
rq.trade_route = self.g_trade_route
serialized = rq.SerializeToString()
length = len(serialized)
buf = bytearray()
buf = length.to_bytes(4, byteorder = 'big', signed=True)
buf += serialized
await ws.send(buf)
async def rithmic_logout(self, ws):
'''
Send a logout request
'''
rq = request_logout_pb2.RequestLogout()
rq.template_id = 12;
rq.user_msg.append("hello")
serialized = rq.SerializeToString()
length = len(serialized)
buf = bytearray()
buf = length.to_bytes(4, byteorder = 'big', signed=True)
buf += serialized
await ws.send(buf)
async def disconnect_from_rithmic(self, ws):
'''
Close the websocket connection.
'''
await ws.close(1000, "see you tomorrow")
def run(self, side, qty):
'''
Start task to submit order.
'''
loop = asyncio.get_event_loop()
#ssl_context = None
if "wss://" in self.uri:
# Set up the ssl context.
self.ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
localhost_pem = pathlib.Path(__file__).with_name("rithmic_ssl_cert_auth_params")
self.ssl_context.load_verify_locations(localhost_pem)
ws = loop.run_until_complete(self.connect_to_rithmic())
loop.run_until_complete(self.rithmic_login(ws,request_login_pb2.RequestLogin.SysInfraType.ORDER_PLANT))
loop.run_until_complete(self.login_info(ws))
if self.g_rcvd_account and self.g_rcvd_trade_route:
loop.run_until_complete(self.subscribe_for_order_updates(ws))
print('Entering new_order')
loop.run_until_complete(self.new_order(ws, side, qty))
print('Exiting new_order')
loop.run_until_complete(self.consume(ws))
if ws.open:
print(f"Logging out from Order Plant...")
loop.run_until_complete(self.rithmic_logout(ws))
print(f"Disconnecting from Order Plant...")
loop.run_until_complete(self.disconnect_from_rithmic(ws))
print(f"Done!")
else:
print(f"Connection appears to be closed. Exiting app.")