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,37 +711,39 @@ 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
async with feed.stream.subscribe() as bstream: try:
# last_tick = time.time() async with feed.stream.subscribe() as bstream:
async for quotes in bstream: # last_tick = time.time()
async for quotes in bstream:
# now = time.time() # now = time.time()
# period = now - last_tick # period = now - last_tick
for sym, quote in quotes.items(): for sym, quote in quotes.items():
for tick in iterticks(quote, types): for tick in iterticks(quote, types):
# print(f'{1/period} Hz') # print(f'{1/period} Hz')
size = live.size size = live.size
if size == 0:
# terminate this update task since we're
# no longer in a pp
order_mode.pane.pnl_label.format(pnl=0)
return
if size == 0: else:
# terminate this update task since we're # compute and display pnl status
# no longer in a pp order_mode.pane.pnl_label.format(
_zero_pp = True pnl=copysign(1, size) * pnl(
order_mode.pane.pnl_label.format(pnl=0) live.avg_price,
return tick['price'],
),
)
else: # last_tick = time.time()
# compute and display pnl status finally:
order_mode.pane.pnl_label.format( assert _pnl_tasks[sym]
pnl=copysign(1, size) * pnl( assert _pnl_tasks.pop(sym)
live.avg_price,
tick['price'],
),
)
# last_tick = time.time()
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,