binance: support order "modifies" B)

Only a couple tweaks to make this work according to the docs:
https://binance-docs.github.io/apidocs/futures/en/#modify-order-trade

- use a PUT request.
- provide the original user id in a `'origClientOrderId'` msg field.
- don't expect the same oid in the PUT response.

Other broker-mode related details:
- don't call `OrderDialogs.add_msg()` until after the existing check
  since we want to check against the *last* msgs contents not the new
  request.
- ensure we pass the `modify=True` flag in the edit case.
basic_buy_bot
Tyler Goodlet 2023-06-18 19:51:13 -04:00
parent 6eee6ead79
commit dc3ac8de01
3 changed files with 59 additions and 24 deletions

View File

@ -628,13 +628,24 @@ class Client:
oid: int | None = None, oid: int | None = None,
tif: str = 'GTC', tif: str = 'GTC',
recv_window: int = 60000 recv_window: int = 60000,
# iceberg_quantity: float | None = None, # iceberg_quantity: float | None = None,
# order_resp_type: str | None = None, resp_type: str = 'ACK',
# TODO: this is probably useful for doing stops, maybe we
# can set it only on dark-stops?
# close_all: bool = False,
modify: bool = False,
) -> str: ) -> str:
''' '''
Submit a live limit order to ze binance. Submit or modify a live limit order to ze binance.
For modify see:
- spot:
- futes https://binance-docs.github.io/apidocs/futures/en/#modify-order-trade
''' '''
# lookup the binance-native symbol from search table # lookup the binance-native symbol from search table
@ -647,10 +658,22 @@ class Client:
('quantity', quantity), ('quantity', quantity),
('price', price), ('price', price),
('recvWindow', recv_window), ('recvWindow', recv_window),
('newOrderRespType', 'ACK'), ('newOrderRespType', resp_type),
('timestamp', binance_timestamp(now())) ('timestamp', binance_timestamp(now()))
# ('closeAll', close_all),
]) ])
if oid:
action: str = 'post'
# NOTE: modifies only require diff key for user oid:
# https://binance-docs.github.io/apidocs/futures/en/#modify-order-trade
if modify:
assert oid
params['origClientOrderId'] = oid
action: str = 'put'
elif oid:
params['newClientOrderId'] = oid params['newClientOrderId'] = oid
log.info( log.info(
@ -661,11 +684,14 @@ class Client:
'order', 'order',
params=params, params=params,
signed=True, signed=True,
action='post' action=action,
) )
# ensure our id is tracked by them # ensure our id is tracked by them
if oid: if (
oid
and not modify
):
assert oid == resp['clientOrderId'] assert oid == resp['clientOrderId']
reqid: str = resp['orderId'] reqid: str = resp['orderId']

View File

@ -164,6 +164,7 @@ async def handle_order_requests(
# validate # validate
order = BrokerdOrder(**msg) order = BrokerdOrder(**msg)
oid: str = order.oid # emsd order id oid: str = order.oid # emsd order id
modify: bool = False
# NOTE: check and report edits # NOTE: check and report edits
if existing := dialogs.get(order.oid): if existing := dialogs.get(order.oid):
@ -171,25 +172,31 @@ async def handle_order_requests(
f'Existing order for {oid} updated:\n' f'Existing order for {oid} updated:\n'
f'{pformat(existing.maps[-1])} -> {pformat(msg)}' f'{pformat(existing.maps[-1])} -> {pformat(msg)}'
) )
# TODO: figure out what special params we have to send? modify = True
# https://binance-docs.github.io/apidocs/futures/en/#modify-order-trade
# track latest request state such that map # only add new msg AFTER the existing check
# lookups start at the most recent msg and then dialogs.add_msg(oid, msg)
# scan reverse-chronologically.
dialogs.add_msg(oid, msg)
# XXX: ACK the request **immediately** before sending else:
# the api side request to ensure the ems maps the oid -> # XXX NOTE: update before the ack!
# reqid correctly! # track latest request state such that map
resp = BrokerdOrderAck( # lookups start at the most recent msg and then
oid=oid, # ems order request id # scan reverse-chronologically.
reqid=oid, # our custom int mapping dialogs.add_msg(oid, msg)
account='binance', # piker account
) # XXX: ACK the request **immediately** before sending
await ems_order_stream.send(resp) # the api side request to ensure the ems maps the oid ->
# reqid correctly!
resp = BrokerdOrderAck(
oid=oid, # ems order request id
reqid=oid, # our custom int mapping
account='binance', # piker account
)
await ems_order_stream.send(resp)
# call our client api to submit the order # call our client api to submit the order
# NOTE: modifies only require diff key for user oid:
# https://binance-docs.github.io/apidocs/futures/en/#modify-order-trade
try: try:
reqid = await client.submit_limit( reqid = await client.submit_limit(
symbol=order.symbol, symbol=order.symbol,
@ -197,6 +204,7 @@ async def handle_order_requests(
quantity=order.size, quantity=order.size,
price=order.price, price=order.price,
oid=oid, oid=oid,
modify=modify,
) )
# SMH they do gen their own order id: ints.. # SMH they do gen their own order id: ints..

View File

@ -43,8 +43,6 @@ _futes_url = f'https://fapi.{_domain}'
_spot_ws: str = 'wss://stream.binance.com/ws' _spot_ws: str = 'wss://stream.binance.com/ws'
# 'wss://ws-api.binance.com:443/ws-api/v3', # 'wss://ws-api.binance.com:443/ws-api/v3',
# NOTE: spot test network only allows certain ep sets:
# https://testnet.binance.vision/
_testnet_spot_ws: str = 'wss://testnet.binance.vision/ws-api/v3' _testnet_spot_ws: str = 'wss://testnet.binance.vision/ws-api/v3'
# https://binance-docs.github.io/apidocs/futures/en/#websocket-market-streams # https://binance-docs.github.io/apidocs/futures/en/#websocket-market-streams
@ -52,6 +50,9 @@ _futes_ws: str = f'wss://fstream.{_domain}/ws/'
_auth_futes_ws: str = 'wss://fstream-auth.{_domain}/ws/' _auth_futes_ws: str = 'wss://fstream-auth.{_domain}/ws/'
# test nets # test nets
# NOTE: spot test network only allows certain ep sets:
# https://testnet.binance.vision/
# https://www.binance.com/en/support/faq/how-to-test-my-functions-on-binance-testnet-ab78f9a1b8824cf0a106b4229c76496d
_testnet_spot_url: str = 'https://testnet.binance.vision/api' _testnet_spot_url: str = 'https://testnet.binance.vision/api'
_testnet_spot_ws: str = 'wss://testnet.binance.vision/ws' _testnet_spot_ws: str = 'wss://testnet.binance.vision/ws'