Use a pnl task per symbol

fsp_feeds
Tyler Goodlet 2021-09-07 12:54:32 -04:00
parent b5c1120ad0
commit 063788499a
1 changed files with 40 additions and 33 deletions

View File

@ -625,8 +625,7 @@ async def open_order_mode(
live_pp = mode.pp.live_pp live_pp = mode.pp.live_pp
size = live_pp.size size = live_pp.size
if size: if size:
global _zero_pp global _pnl_tasks
_zero_pp = False
# compute and display pnl status immediately # compute and display pnl status immediately
mode.pane.pnl_label.format( mode.pane.pnl_label.format(
@ -681,7 +680,7 @@ async def open_order_mode(
yield mode yield mode
_zero_pp: bool = True _pnl_tasks: dict[str, bool] = {}
async def display_pnl( async def display_pnl(
@ -693,12 +692,15 @@ async def display_pnl(
Error if this task is spawned where there is a net-zero pp. Error if this task is spawned where there is a net-zero pp.
''' '''
global _zero_pp global _pnl_tasks
assert not _zero_pp
pp = order_mode.pp pp = order_mode.pp
live = pp.live_pp live = pp.live_pp
sym = live.symbol.key
assert not _pnl_tasks.get(sym)
_pnl_tasks[sym] = True
if live.size < 0: if live.size < 0:
types = ('ask', 'last', 'last', 'utrade') types = ('ask', 'last', 'last', 'utrade')
@ -709,6 +711,7 @@ async def display_pnl(
raise RuntimeError('No pp?!?!') raise RuntimeError('No pp?!?!')
# real-time update pnl on the status pane # real-time update pnl on the status pane
try:
async with feed.stream.subscribe() as bstream: async with feed.stream.subscribe() as bstream:
# last_tick = time.time() # last_tick = time.time()
async for quotes in bstream: async for quotes in bstream:
@ -722,11 +725,9 @@ async def display_pnl(
# print(f'{1/period} Hz') # print(f'{1/period} Hz')
size = live.size size = live.size
if size == 0: if size == 0:
# terminate this update task since we're # terminate this update task since we're
# no longer in a pp # no longer in a pp
_zero_pp = True
order_mode.pane.pnl_label.format(pnl=0) order_mode.pane.pnl_label.format(pnl=0)
return return
@ -740,6 +741,9 @@ async def display_pnl(
) )
# last_tick = time.time() # last_tick = time.time()
finally:
assert _pnl_tasks[sym]
assert _pnl_tasks.pop(sym)
async def process_trades_and_update_ui( async def process_trades_and_update_ui(
@ -754,7 +758,7 @@ async def process_trades_and_update_ui(
get_index = mode.chart.get_index get_index = mode.chart.get_index
tracker = mode.pp tracker = mode.pp
global _zero_pp global _pnl_tasks
# this is where we receive **back** messages # this is where we receive **back** messages
# about executions **from** the EMS actor # about executions **from** the EMS actor
@ -771,14 +775,17 @@ async def process_trades_and_update_ui(
sym = mode.chart.linked.symbol sym = mode.chart.linked.symbol
if msg['symbol'].lower() in sym.key: if msg['symbol'].lower() in sym.key:
tracker.live_pp.update_from_msg(msg) tracker.live_pp.update_from_msg(msg)
tracker.update_from_pp() tracker.update_from_pp()
# update order pane widgets # update order pane widgets
mode.pane.update_status_ui() mode.pane.update_status_ui()
if mode.pp.live_pp.size and _zero_pp: if (
_zero_pp = False tracker.live_pp.size and
sym.key not in _pnl_tasks
):
n.start_soon( n.start_soon(
display_pnl, display_pnl,
feed, feed,