More explicit order-cancel errors handling

kraken_cleaning
Tyler Goodlet 2022-04-10 17:07:08 -04:00
parent 88306a6c1e
commit c39fa825d0
1 changed files with 49 additions and 23 deletions

View File

@ -33,7 +33,7 @@ import tractor
from pydantic.dataclasses import dataclass from pydantic.dataclasses import dataclass
from pydantic import BaseModel from pydantic import BaseModel
import wsproto import wsproto
from itertools import count import itertools
import urllib.parse import urllib.parse
import hashlib import hashlib
import hmac import hmac
@ -540,7 +540,8 @@ async def handle_order_requests(
request_msg: dict request_msg: dict
order: BrokerdOrder order: BrokerdOrder
userref_counter = count() userref_counter = itertools.count()
async for request_msg in ems_order_stream: async for request_msg in ems_order_stream:
log.info(f'Received order request {request_msg}') log.info(f'Received order request {request_msg}')
@ -618,38 +619,63 @@ async def handle_order_requests(
) )
# Check to make sure there was no error returned by # Check to make sure there was no error returned by
# the kraken endpoint. Assert one order was cancelled # the kraken endpoint. Assert one order was cancelled.
assert resp['error'] == [] try:
assert resp['result']['count'] == 1 result = resp['result']
count = result['count']
# Check to make sure the cancellation is NOT pending, # check for 'error' key if we received no 'result'
# then send the confirmation to the ems order stream except KeyError:
pending = resp['result'].get('pending') error = resp.get('error')
if pending:
oid = order.oid
log.error(f'Order {oid} cancel was not successful')
await ems_order_stream.send( await ems_order_stream.send(
BrokerdError( BrokerdError(
oid=oid, oid=order.oid,
reqid=temp_id, reqid=temp_id,
symbol=order.symbol, symbol=order.symbol,
reason="Failed order cancel", reason="Failed order cancel",
broker_details=resp broker_details=resp
).dict() ).dict()
) )
if not error:
raise BrokerError(f'Unknown order cancel response: {resp}')
else: else:
await ems_order_stream.send( if not count: # no orders were cancelled?
BrokerdStatus(
reqid=msg.reqid, # XXX: what exactly is this from and why would we care?
account=msg.account, # there doesn't seem to be any docs here?
time_ns=time.time_ns(), # https://docs.kraken.com/rest/#operation/cancelOrder
status='cancelled',
reason='Order cancelled', # Check to make sure the cancellation is NOT pending,
broker_details={'name': 'kraken'} # then send the confirmation to the ems order stream
).dict() pending = result.get('pending')
) if pending:
log.error(f'Order {oid} cancel was not yet successful')
await ems_order_stream.send(
BrokerdError(
oid=order.oid,
reqid=temp_id,
symbol=order.symbol,
reason="Order cancel is still pending?",
broker_details=resp
).dict()
)
else: # order cancel success case.
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()
)
else: else:
log.error(f'Unknown order command: {request_msg}') log.error(f'Unknown order command: {request_msg}')