Fix logic to display pnl in status label immediately

chart_mod_breakup
Tyler Goodlet 2021-09-14 18:31:49 -04:00
parent 67de83afa9
commit aa91055a16
1 changed files with 22 additions and 28 deletions

View File

@ -50,7 +50,7 @@ log = get_logger(__name__)
_pnl_tasks: dict[str, bool] = {} _pnl_tasks: dict[str, bool] = {}
async def display_pnl( async def update_pnl_from_feed(
feed: Feed, feed: Feed,
order_mode: OrderMode, # noqa order_mode: OrderMode, # noqa
@ -67,6 +67,8 @@ async def display_pnl(
live = pp.live_pp live = pp.live_pp
key = live.symbol.key key = live.symbol.key
log.info(f'Starting pnl display for {pp.alloc.account}')
if live.size < 0: if live.size < 0:
types = ('ask', 'last', 'last', 'utrade') types = ('ask', 'last', 'last', 'utrade')
@ -303,7 +305,7 @@ class SettingsPane:
self, self,
tracker: PositionTracker, tracker: PositionTracker,
) -> bool: ) -> None:
'''Display the PnL for the current symbol and personal positioning (pp). '''Display the PnL for the current symbol and personal positioning (pp).
If a position is open start a background task which will If a position is open start a background task which will
@ -314,36 +316,28 @@ class SettingsPane:
sym = mode.chart.linked.symbol sym = mode.chart.linked.symbol
size = tracker.live_pp.size size = tracker.live_pp.size
feed = mode.quote_feed feed = mode.quote_feed
global _pnl_tasks pnl_value = 0
if ( if size:
size and # last historical close price
sym.key not in _pnl_tasks last = feed.shm.array[-1][['close']][0]
): pnl_value = copysign(1, size) * pnl(
_pnl_tasks[sym.key] = True tracker.live_pp.avg_price,
last,
# immediately compute and display pnl status from last quote
self.pnl_label.format(
pnl=copysign(1, size) * pnl(
tracker.live_pp.avg_price,
# last historical close price
feed.shm.array[-1][['close']][0],
),
) )
log.info( # maybe start update task
f'Starting pnl display for {tracker.alloc.account}') global _pnl_tasks
self.order_mode.nursery.start_soon( if sym.key not in _pnl_tasks:
display_pnl, _pnl_tasks[sym.key] = True
feed, self.order_mode.nursery.start_soon(
mode, update_pnl_from_feed,
) feed,
return True mode,
)
else: # immediately display in status label
# set 0% pnl self.pnl_label.format(pnl=pnl_value)
self.pnl_label.format(pnl=0)
return False
def position_line( def position_line(