Skip to content

Commit fbc15b9

Browse files
committed
Hard code futes venue(s) for now in brokerd..
1 parent cdeee8d commit fbc15b9

File tree

1 file changed

+44
-18
lines changed

1 file changed

+44
-18
lines changed

piker/brokers/binance/broker.py

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,11 @@
6161
Status,
6262
Order,
6363
)
64-
from .venues import Pair
64+
from .venues import (
65+
Pair,
66+
_futes_ws,
67+
_testnet_futes_ws,
68+
)
6569
from .api import Client
6670

6771
log = get_logger('piker.brokers.binance')
@@ -213,33 +217,41 @@ async def open_trade_dialog(
213217

214218
) -> AsyncIterator[dict[str, Any]]:
215219

220+
# TODO: how do we set this from the EMS such that
221+
# positions are loaded from the correct venue on the user
222+
# stream at startup? (that is in an attempt to support both
223+
# spot and futes markets?)
224+
# - I guess we just want to instead start 2 separate user
225+
# stream tasks right? unless we want another actor pool?
226+
# XXX: see issue: <urlhere>
227+
venue_name: str = 'futes'
228+
venue_mode: str = 'usdtm_futes'
229+
account_name: str = 'usdtm'
230+
use_testnet: bool = False
231+
216232
async with open_cached_client('binance') as client:
217-
for key, subconf in client.conf.items():
218-
if subconf.get('api_key'):
219-
break
233+
subconf: dict = client.conf[venue_name]
234+
use_testnet = subconf.get('use_testnet', False)
220235

221236
# XXX: if no futes.api_key or spot.api_key has been set we
222237
# always fall back to the paper engine!
223-
else:
238+
if not subconf.get('api_key'):
224239
await ctx.started('paper')
225240
return
226241

227242
async with (
228243
open_cached_client('binance') as client,
229244
):
230-
client.mkt_mode: str = 'usdtm_futes'
245+
client.mkt_mode: str = venue_mode
231246

232-
# if client.
233-
venue: str = client.mkt_mode
247+
# TODO: map these wss urls depending on spot or futes
248+
# setting passed when this task is spawned?
249+
wss_url: str = _futes_ws if not use_testnet else _testnet_futes_ws
234250

235251
wss: NoBsWs
236252
async with (
237253
client.manage_listen_key() as listen_key,
238-
open_autorecon_ws(
239-
f'wss://stream.binancefuture.com/ws/{listen_key}',
240-
# f'wss://stream.binance.com:9443/ws/{listen_key}',
241-
) as wss,
242-
254+
open_autorecon_ws(f'{wss_url}/ws/{listen_key}') as wss,
243255
):
244256
nsid: int = time_ns()
245257
await wss.send_msg({
@@ -270,7 +282,7 @@ async def open_trade_dialog(
270282
positions: list[BrokerdPosition] = []
271283

272284
for resp_dict in msg['result']:
273-
resp = resp_dict['res']
285+
resp: dict = resp_dict['res']
274286
req: str = resp_dict['req']
275287

276288
# @account response should be something like:
@@ -329,7 +341,9 @@ async def open_trade_dialog(
329341
bs_mktid: str = entry['symbol']
330342
entry_size: float = float(entry['positionAmt'])
331343

332-
pair: Pair | None = client._venue2pairs[venue].get(bs_mktid)
344+
pair: Pair | None = client._venue2pairs[
345+
venue_mode
346+
].get(bs_mktid)
333347
if (
334348
pair
335349
and entry_size > 0
@@ -338,7 +352,7 @@ async def open_trade_dialog(
338352

339353
ppmsg = BrokerdPosition(
340354
broker='binance',
341-
account='binance.usdtm',
355+
account=f'binance.{account_name}',
342356

343357
# TODO: maybe we should be passing back
344358
# a `MktPair` here?
@@ -357,6 +371,13 @@ async def open_trade_dialog(
357371

358372
await ctx.started((positions, list(accounts)))
359373

374+
# TODO: package more state tracking into the dialogs API?
375+
# - hmm maybe we could include `OrderDialogs.dids:
376+
# bidict` as part of the interface and then ask for
377+
# a reqid field to be passed at init?
378+
# |-> `OrderDialog(reqid_field='orderId')` kinda thing?
379+
# - also maybe bundle in some kind of dialog to account
380+
# table?
360381
dialogs = OrderDialogs()
361382
dids: dict[str, int] = bidict()
362383

@@ -404,7 +425,8 @@ async def open_trade_dialog(
404425
)
405426
tn.start_soon(
406427
handle_order_updates,
407-
venue,
428+
venue_mode,
429+
account_name,
408430
client,
409431
ems_stream,
410432
wss,
@@ -417,6 +439,7 @@ async def open_trade_dialog(
417439

418440
async def handle_order_updates(
419441
venue: str,
442+
account_name: str,
420443
client: Client,
421444
ems_stream: tractor.MsgStream,
422445
wss: NoBsWs,
@@ -574,6 +597,9 @@ async def handle_order_updates(
574597
time_ns=time_ns(),
575598
# reqid=reqid,
576599
reqid=oid,
600+
601+
# TODO: i feel like we don't need to make the
602+
# ems and upstream clients aware of this?
577603
# account='binance.usdtm',
578604

579605
status=status,
@@ -622,7 +648,7 @@ async def handle_order_updates(
622648
pair: Pair | None = client._venue2pairs[venue].get(bs_mktid)
623649
ppmsg = BrokerdPosition(
624650
broker='binance',
625-
account='binance.usdtm',
651+
account=f'binance.{account_name}',
626652

627653
# TODO: maybe we should be passing back
628654
# a `MktPair` here?

0 commit comments

Comments
 (0)