Add initial `kraken` live order loading
parent
c8bff81220
commit
e2cd8c4aef
|
@ -39,7 +39,6 @@ from bidict import bidict
|
||||||
import pendulum
|
import pendulum
|
||||||
import trio
|
import trio
|
||||||
import tractor
|
import tractor
|
||||||
import wsproto
|
|
||||||
|
|
||||||
from piker.pp import (
|
from piker.pp import (
|
||||||
Position,
|
Position,
|
||||||
|
@ -49,6 +48,8 @@ from piker.pp import (
|
||||||
open_pps,
|
open_pps,
|
||||||
)
|
)
|
||||||
from piker.clearing._messages import (
|
from piker.clearing._messages import (
|
||||||
|
Order,
|
||||||
|
Status,
|
||||||
BrokerdCancel,
|
BrokerdCancel,
|
||||||
BrokerdError,
|
BrokerdError,
|
||||||
BrokerdFill,
|
BrokerdFill,
|
||||||
|
@ -126,7 +127,7 @@ async def handle_order_requests(
|
||||||
oid=msg['oid'],
|
oid=msg['oid'],
|
||||||
symbol=msg['symbol'],
|
symbol=msg['symbol'],
|
||||||
reason=(
|
reason=(
|
||||||
f'TooFastEdit reqid:{reqid}, could not cancelling..'
|
f'Edit too fast:{reqid}, cancelling..'
|
||||||
),
|
),
|
||||||
|
|
||||||
)
|
)
|
||||||
|
@ -249,7 +250,7 @@ async def handle_order_requests(
|
||||||
|
|
||||||
@acm
|
@acm
|
||||||
async def subscribe(
|
async def subscribe(
|
||||||
ws: wsproto.WSConnection,
|
ws: NoBsWs,
|
||||||
token: str,
|
token: str,
|
||||||
subs: list[tuple[str, dict]] = [
|
subs: list[tuple[str, dict]] = [
|
||||||
('ownTrades', {
|
('ownTrades', {
|
||||||
|
@ -632,8 +633,6 @@ async def handle_order_updates(
|
||||||
# to do all fill/status/pp updates in that sub and just use
|
# to do all fill/status/pp updates in that sub and just use
|
||||||
# this one for ledger syncs?
|
# this one for ledger syncs?
|
||||||
|
|
||||||
# XXX: ASK SUPPORT ABOUT THIS!
|
|
||||||
|
|
||||||
# For eg. we could take the "last 50 trades" and do a diff
|
# For eg. we could take the "last 50 trades" and do a diff
|
||||||
# with the ledger and then only do a re-sync if something
|
# with the ledger and then only do a re-sync if something
|
||||||
# seems amiss?
|
# seems amiss?
|
||||||
|
@ -696,7 +695,6 @@ async def handle_order_updates(
|
||||||
status_msg = BrokerdStatus(
|
status_msg = BrokerdStatus(
|
||||||
reqid=reqid,
|
reqid=reqid,
|
||||||
time_ns=time.time_ns(),
|
time_ns=time.time_ns(),
|
||||||
|
|
||||||
account=acc_name,
|
account=acc_name,
|
||||||
status='filled',
|
status='filled',
|
||||||
filled=size,
|
filled=size,
|
||||||
|
@ -870,38 +868,56 @@ async def handle_order_updates(
|
||||||
f'{update_msg}\n'
|
f'{update_msg}\n'
|
||||||
'Cancelling order for now!..'
|
'Cancelling order for now!..'
|
||||||
)
|
)
|
||||||
|
# call ws api to cancel:
|
||||||
|
# https://docs.kraken.com/websockets/#message-cancelOrder
|
||||||
|
await ws.send_msg({
|
||||||
|
'event': 'cancelOrder',
|
||||||
|
'token': token,
|
||||||
|
'reqid': reqid or 0,
|
||||||
|
'txid': [txid],
|
||||||
|
})
|
||||||
|
continue
|
||||||
|
|
||||||
elif noid: # a non-ems-active order
|
# a non-ems-active order, emit live
|
||||||
# TODO: handle these and relay them
|
# order embedded in status msg.
|
||||||
# through the EMS to the client / UI
|
elif noid:
|
||||||
# side!
|
# parse out existing live order
|
||||||
log.cancel(
|
descr = rest['descr']
|
||||||
f'Rx unknown active order {txid}:\n'
|
fqsn = descr['pair'].replace(
|
||||||
f'{update_msg}\n'
|
'/', '').lower()
|
||||||
'Cancelling order for now!..'
|
price = float(descr['price'])
|
||||||
|
size = float(rest['vol'])
|
||||||
|
action = descr['type']
|
||||||
|
|
||||||
|
# register the userref value from
|
||||||
|
# kraken (usually an `int` staring
|
||||||
|
# at 1?) as our reqid.
|
||||||
|
reqids2txids[reqid] = txid
|
||||||
|
oid = str(reqid)
|
||||||
|
ids[oid] = reqid # NOTE!: str -> int
|
||||||
|
|
||||||
|
# fill out ``Status`` + boxed ``Order``
|
||||||
|
status_msg = Status(
|
||||||
|
time_ns=time.time_ns(),
|
||||||
|
resp='open',
|
||||||
|
oid=oid,
|
||||||
|
reqid=reqid,
|
||||||
|
|
||||||
|
# embedded order info
|
||||||
|
req=Order(
|
||||||
|
action=action,
|
||||||
|
exec_mode='live',
|
||||||
|
oid=oid,
|
||||||
|
symbol=fqsn,
|
||||||
|
account=acc_name,
|
||||||
|
price=price,
|
||||||
|
size=size,
|
||||||
|
),
|
||||||
|
src='kraken',
|
||||||
)
|
)
|
||||||
|
apiflows[reqid].maps.append(status_msg)
|
||||||
# call ws api to cancel:
|
await ems_stream.send(status_msg)
|
||||||
# https://docs.kraken.com/websockets/#message-cancelOrder
|
continue
|
||||||
await ws.send_msg({
|
|
||||||
'event': 'cancelOrder',
|
|
||||||
'token': token,
|
|
||||||
'reqid': reqid or 0,
|
|
||||||
'txid': [txid],
|
|
||||||
})
|
|
||||||
continue
|
|
||||||
|
|
||||||
# remap statuses to ems set.
|
|
||||||
ems_status = {
|
|
||||||
'open': 'submitted',
|
|
||||||
'closed': 'filled',
|
|
||||||
'canceled': 'cancelled',
|
|
||||||
# do we even need to forward
|
|
||||||
# this state to the ems?
|
|
||||||
'pending': 'pending',
|
|
||||||
}[status]
|
|
||||||
# TODO: i like the open / closed semantics
|
|
||||||
# more we should consider them for internals
|
|
||||||
|
|
||||||
# send BrokerdStatus messages for all
|
# send BrokerdStatus messages for all
|
||||||
# order state updates
|
# order state updates
|
||||||
|
@ -912,7 +928,7 @@ async def handle_order_updates(
|
||||||
account=f'kraken.{acctid}',
|
account=f'kraken.{acctid}',
|
||||||
|
|
||||||
# everyone doin camel case..
|
# everyone doin camel case..
|
||||||
status=ems_status, # force lower case
|
status=status, # force lower case
|
||||||
|
|
||||||
filled=vlm,
|
filled=vlm,
|
||||||
reason='', # why held?
|
reason='', # why held?
|
||||||
|
|
Loading…
Reference in New Issue