Fix logic to display pnl in status label immediately
parent
67de83afa9
commit
aa91055a16
|
@ -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
|
|
||||||
sym.key not in _pnl_tasks
|
|
||||||
):
|
|
||||||
_pnl_tasks[sym.key] = True
|
|
||||||
|
|
||||||
# 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
|
# last historical close price
|
||||||
feed.shm.array[-1][['close']][0],
|
last = feed.shm.array[-1][['close']][0]
|
||||||
),
|
pnl_value = copysign(1, size) * pnl(
|
||||||
|
tracker.live_pp.avg_price,
|
||||||
|
last,
|
||||||
)
|
)
|
||||||
|
|
||||||
log.info(
|
# maybe start update task
|
||||||
f'Starting pnl display for {tracker.alloc.account}')
|
global _pnl_tasks
|
||||||
|
if sym.key not in _pnl_tasks:
|
||||||
|
_pnl_tasks[sym.key] = True
|
||||||
self.order_mode.nursery.start_soon(
|
self.order_mode.nursery.start_soon(
|
||||||
display_pnl,
|
update_pnl_from_feed,
|
||||||
feed,
|
feed,
|
||||||
mode,
|
mode,
|
||||||
)
|
)
|
||||||
return True
|
|
||||||
|
|
||||||
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(
|
||||||
|
|
Loading…
Reference in New Issue