From 0b123c9af908642ea768ee8edd0d44d0730acdd5 Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Sun, 21 Sep 2025 16:05:50 -0400 Subject: [PATCH] `ib`: various type-annot, multiline styling and todos updates --- piker/brokers/ib/api.py | 20 ++++++++++---------- piker/brokers/ib/broker.py | 16 +++++++++++++--- piker/brokers/ib/feed.py | 17 +++++++++++++---- 3 files changed, 36 insertions(+), 17 deletions(-) diff --git a/piker/brokers/ib/api.py b/piker/brokers/ib/api.py index 5c8563ba..01659bf5 100644 --- a/piker/brokers/ib/api.py +++ b/piker/brokers/ib/api.py @@ -338,15 +338,15 @@ class Client: fqme: str, # EST in ISO 8601 format is required... below is EPOCH - start_dt: datetime | str = "1970-01-01T00:00:00.000000-05:00", - end_dt: datetime | str = "", + start_dt: datetime|str = "1970-01-01T00:00:00.000000-05:00", + end_dt: datetime|str = "", # ohlc sample period in seconds sample_period_s: int = 1, # optional "duration of time" equal to the # length of the returned history frame. - duration: str | None = None, + duration: str|None = None, **kwargs, @@ -720,8 +720,8 @@ class Client: async def find_contracts( self, - pattern: str | None = None, - contract: Contract | None = None, + pattern: str|None = None, + contract: Contract|None = None, qualify: bool = True, err_on_qualify: bool = True, @@ -866,7 +866,7 @@ class Client: self, fqme: str, - ) -> datetime | None: + ) -> datetime|None: ''' Return the first datetime stamp for `fqme` or `None` on request failure. @@ -922,7 +922,7 @@ class Client: tries: int = 100, raise_on_timeout: bool = False, - ) -> Ticker | None: + ) -> Ticker|None: ''' Return a single (snap) quote for symbol. @@ -934,7 +934,7 @@ class Client: ready: ticker.TickerUpdateEvent = ticker.updateEvent # ensure a last price gets filled in before we deliver quote - timeouterr: Exception | None = None + timeouterr: Exception|None = None warnset: bool = False for _ in range(tries): @@ -1509,7 +1509,7 @@ class MethodProxy: self, pattern: str, - ) -> dict[str, Any] | trio.Event: + ) -> dict[str, Any]|trio.Event: ev = self.event_table.get(pattern) @@ -1546,7 +1546,7 @@ async def open_aio_client_method_relay( # relay all method requests to ``asyncio``-side client and deliver # back results while not to_trio._closed: - msg: tuple[str, dict] | dict | None = await from_trio.get() + msg: tuple[str, dict]|dict|None = await from_trio.get() match msg: case None: # termination sentinel log.info('asyncio `Client` method-proxy SHUTDOWN!') diff --git a/piker/brokers/ib/broker.py b/piker/brokers/ib/broker.py index f9c27bba..70dfda19 100644 --- a/piker/brokers/ib/broker.py +++ b/piker/brokers/ib/broker.py @@ -547,7 +547,10 @@ async def open_trade_dialog( ), # TODO: do this as part of `open_account()`!? - open_symcache('ib', only_from_memcache=True) as symcache, + open_symcache( + 'ib', + only_from_memcache=True, + ) as symcache, ): # Open a trade ledgers stack for appending trade records over # multiple accounts. @@ -556,7 +559,9 @@ async def open_trade_dialog( tables: dict[str, Account] = {} order_msgs: list[Status] = [] conf = get_config() - accounts_def_inv: bidict[str, str] = bidict(conf['accounts']).inverse + accounts_def_inv: bidict[str, str] = bidict( + conf['accounts'] + ).inverse with ( ExitStack() as lstack, @@ -706,7 +711,11 @@ async def open_trade_dialog( # client-account and build out position msgs to deliver to # EMS. for acctid, acnt in tables.items(): - active_pps, closed_pps = acnt.dump_active() + active_pps: dict[str, Position] + ( + active_pps, + closed_pps, + ) = acnt.dump_active() for pps in [active_pps, closed_pps]: piker_pps: list[Position] = list(pps.values()) @@ -722,6 +731,7 @@ async def open_trade_dialog( ) if ibpos: bs_mktid: str = str(ibpos.contract.conId) + msg = await update_and_audit_pos_msg( acctid, pikerpos, diff --git a/piker/brokers/ib/feed.py b/piker/brokers/ib/feed.py index 90002fae..1dc27d86 100644 --- a/piker/brokers/ib/feed.py +++ b/piker/brokers/ib/feed.py @@ -1,5 +1,5 @@ # piker: trading gear for hackers -# Copyright (C) Tyler Goodlet (in stewardship for pikers) +# Copyright (C) 2018-forever Tyler Goodlet (in stewardship for pikers) # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as published by @@ -13,10 +13,12 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -""" -Data feed endpoints pre-wrapped and ready for use with ``tractor``/``trio``. -""" +''' +Data feed endpoints pre-wrapped and ready for use with `tractor`/`trio` +via "infected-asyncio-mode". + +''' from __future__ import annotations import asyncio from contextlib import ( @@ -895,6 +897,13 @@ def normalize( return data +# ?TODO? feels like this task-fn could be factored to reduce some +# indentation levels? +# -[ ] the reconnect while loop on ib-gw "data farm connection.."s +# -[ ] everything embedded under the `async with aclosing(stream):` +# as the "meat" of the quote delivery once the connection is +# stable. +# async def stream_quotes( send_chan: trio.abc.SendChannel,