Skip to content

Commit

Permalink
Add order status tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillermo Rodriguez authored and goodboy committed Jun 9, 2023
1 parent 797d04f commit c9052a2
Showing 1 changed file with 67 additions and 8 deletions.
75 changes: 67 additions & 8 deletions piker/brokers/binance.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,7 @@ async def periodic_keep_alive(
async with trio.open_nursery() as n:
n.start_soon(periodic_keep_alive, key)
yield key
n.cancel_scope.cancel()

await self.close_listen_key(key)

Expand Down Expand Up @@ -910,10 +911,9 @@ async def handle_order_requests(
)

elif action == 'cancel':
# msg = BrokerdCancel(**request_msg)
#
# await client.submit_cancel(symbol, msg.reqid)
...
msg = BrokerdCancel(**request_msg)

await client.submit_cancel(msg.symbol, msg.reqid)

else:
log.error(f'Unknown order command: {request_msg}')
Expand Down Expand Up @@ -947,10 +947,69 @@ async def trades_dialogue(
):
n.start_soon(handle_order_requests, ems_stream)
await trio.sleep_forever()
# async with open_autorecon_ws(
# f'wss://stream.binance.com:9443/ws/{listen_key}',
# ) as ws:
# ...

async with open_autorecon_ws(
f'wss://stream.binance.com:9443/ws/{listen_key}',
) as ws:
event = await ws.recv_msg()

if event.get('e') == 'executionReport':
"""
https://binance-docs.github.io/apidocs/spot/en/#payload-balance-update
"""

oid = event.get('c')
side = event.get('S').lower()
status = event.get('X')
order_qty = float(event.get('q'))
filled_qty = float(event.get('z'))
cumm_transacted_qty = float(event.get('Z'))
price_avg = cum_transacted_qty / filled_qty

broker_time = float(event.get('T'))

commission_amount = float(event.get('n'))
commission_asset = event.get('N')

if status == 'TRADE':
if order_qty == filled_qty:
msg = BrokerdFill(
reqid=oid,
time_ns=time.time_ns(),
action=side,
price=price_avg,
broker_details={
'name': 'binance',
'commissions': {
'amount': commission_amount,
'asset': commission_asset
},
'broker_time': broker_time
},
broker_time=broker_time
)

else:
if status == 'NEW':
status = 'submitted'

elif status == 'CANCELED':
status = 'cancelled'

msg = BrokerdStatus(
reqid=oid,
time_ns=time.time_ns(),
status=status,
filled=filled_qty,
remaining=order_qty - filled_qty,
broker_details={'name': 'binance'}
)

else:
# XXX: temporary, to catch unhandled msgs
breakpoint()

await ems_stream.send(msg.dict())


@tractor.context
Expand Down

0 comments on commit c9052a2

Please sign in to comment.