add order cancel support over websockets

kraken_orders
Konstantine Tsafatinos 2022-02-17 17:40:28 -05:00
parent d826a66c8c
commit 46948e0a8b
1 changed files with 47 additions and 28 deletions

View File

@ -609,35 +609,43 @@ async def handle_order_requests(
elif action == 'cancel': elif action == 'cancel':
msg = BrokerdCancel(**request_msg) msg = BrokerdCancel(**request_msg)
# Send order cancellation to kraken cancel_msg = {
resp = await client.submit_cancel( "event": "cancelOrder",
reqid=msg.reqid "token": token,
) "txid": [msg.reqid]
}
try: await ws.send_msg(cancel_msg)
# Check to make sure there was no error returned by
# the kraken endpoint. Assert one order was cancelled
assert resp['error'] == []
assert resp['result']['count'] == 1
## TODO: Change this code using .get ## Send order cancellation to kraken
try: #resp = await client.submit_cancel(
pending = resp['result']['pending'] # reqid=msg.reqid
# Check to make sure the cancellation is NOT pending, #)
# then send the confirmation to the ems order stream
except KeyError: #try:
await ems_order_stream.send( # # Check to make sure there was no error returned by
BrokerdStatus( # # the kraken endpoint. Assert one order was cancelled
reqid=msg.reqid, # assert resp['error'] == []
account=msg.account, # assert resp['result']['count'] == 1
time_ns=time.time_ns(),
status='cancelled', # ## TODO: Change this code using .get
reason='Order cancelled', # try:
broker_details={'name': 'kraken'} # pending = resp['result']['pending']
).dict() # # Check to make sure the cancellation is NOT pending,
) # # then send the confirmation to the ems order stream
except AssertionError: # except KeyError:
log.error(f'Order cancel was not successful') # await ems_order_stream.send(
# BrokerdStatus(
# reqid=msg.reqid,
# account=msg.account,
# time_ns=time.time_ns(),
# status='cancelled',
# reason='Order cancelled',
# broker_details={'name': 'kraken'}
# ).dict()
# )
#except AssertionError:
# log.error(f'Order cancel was not successful')
else: else:
log.error(f'Unknown order command: {request_msg}') log.error(f'Unknown order command: {request_msg}')
@ -746,8 +754,19 @@ async def trades_dialogue(
async for msg in process_order_msgs(ws): async for msg in process_order_msgs(ws):
pprint(msg) pprint(msg)
for order in msg: for order in msg:
## TODO: Maybe do a better ## TODO: Maybe do a better check and handle accounts
if type(order) == dict: if type(order) == dict:
if order['status'] == 'canceled':
await ems_stream.send(
BrokerdStatus(
account='kraken.spot',
reqid=order['txid'],
time_ns=time.time_ns(),
status='cancelled',
reason='Order cancelled',
broker_details={'name': 'kraken'}
).dict()
)
for pending_order in pending_orders: for pending_order in pending_orders:
if pending_order.txid == order['txid'] and order['status'] == 'open': if pending_order.txid == order['txid'] and order['status'] == 'open':
await ems_stream.send( await ems_stream.send(