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,9 +172,13 @@ 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
# only add new msg AFTER the existing check
dialogs.add_msg(oid, msg)
else:
# XXX NOTE: update before the ack!
# track latest request state such that map # track latest request state such that map
# lookups start at the most recent msg and then # lookups start at the most recent msg and then
# scan reverse-chronologically. # scan reverse-chronologically.
@ -190,6 +195,8 @@ async def handle_order_requests(
await ems_order_stream.send(resp) 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'