From 30bce42c0b6a1572fab9957b50e1c961859dfdbd Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Mon, 19 Sep 2022 16:11:41 -0400 Subject: [PATCH] Don't spin paper clear loop on non-clearing ticks Not sure what exactly happened but it seemed clears weren't working in some cases without this, also there's no point in spinning the simulated clearing loop if we're handling a non-clearing tick type. --- piker/clearing/_paper_engine.py | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/piker/clearing/_paper_engine.py b/piker/clearing/_paper_engine.py index b74780f5..a40cf7c6 100644 --- a/piker/clearing/_paper_engine.py +++ b/piker/clearing/_paper_engine.py @@ -61,13 +61,13 @@ log = get_logger(__name__) class PaperBoi(Struct): - """ - Emulates a broker order client providing the same API and - delivering an order-event response stream but with methods for + ''' + Emulates a broker order client providing approximately the same API + and delivering an order-event response stream but with methods for triggering desired events based on forward testing engine - requirements. + requirements (eg open, closed, fill msgs). - """ + ''' broker: str ems_trades_stream: tractor.MsgStream @@ -336,9 +336,10 @@ async def simulate_fills( return tick_price >= our_price match tick: + + # on an ask queue tick, only clear buy entries case { 'price': tick_price, - # 'type': ('ask' | 'trade' | 'last'), 'type': 'ask', }: client.last_ask = ( @@ -351,9 +352,9 @@ async def simulate_fills( itertools.repeat(buy_on_ask) ) + # on a bid queue tick, only clear sell entries case { 'price': tick_price, - # 'type': ('bid' | 'trade' | 'last'), 'type': 'bid', }: client.last_bid = ( @@ -366,6 +367,10 @@ async def simulate_fills( itertools.repeat(sell_on_bid) ) + # TODO: fix this block, though it definitely + # costs a lot more CPU-wise + # - doesn't seem like clears are happening still on + # "resting" limit orders? case { 'price': tick_price, 'type': ('trade' | 'last'), @@ -390,6 +395,13 @@ async def simulate_fills( iter_entries = interleave() + # NOTE: all other (non-clearable) tick event types + # - we don't want to sping the simulated clear loop + # below unecessarily and further don't want to pop + # simulated live orders prematurely. + case _: + continue + # iterate all potentially clearable book prices # in FIFO order per side. for order_info, pred in iter_entries: @@ -532,7 +544,10 @@ async def trades_dialogue( # TODO: load paper positions per broker from .toml config file # and pass as symbol to position data mapping: ``dict[str, dict]`` - await ctx.started((pp_msgs, ['paper'])) + await ctx.started(( + pp_msgs, + ['paper'], + )) async with ( ctx.open_stream() as ems_stream,