@@ -111,17 +111,20 @@ async def get_position_info(self, pair: bs.Pair) -> Optional[PositionInfo]:
111111 return pos_info
112112
113113 async def check_loss (self ):
114- pairs = [pos_info .pair for pos_info in self ._positions .values () if pos_info .current != 0 ]
115- # For every pair get position information along with bid and ask prices.
116- coros = [self .get_position_info (pair ) for pair in pairs ]
117- coros .extend (self ._exchange .get_bid_ask (pair ) for pair in pairs )
118- res = await asyncio .gather (* coros )
119- midpoint = int (len (res ) / 2 )
120- all_pos_info = res [0 :midpoint ]
121- all_bid_ask = res [midpoint :]
122-
123- # Log each position an check PnL.
124- for pos_info , (bid , ask ) in zip (all_pos_info , all_bid_ask ):
114+ # Refresh positions that have an open order.
115+ refresh_pairs = [pair for pair , pos_info in self ._positions .items () if pos_info .order_open ]
116+ coros = [self .get_position_info (pair ) for pair in refresh_pairs ]
117+ if coros :
118+ await asyncio .gather (* coros )
119+
120+ # Check unrealized PnL for all non-neutral positions.
121+ non_neutral = [pos_info for pos_info in self ._positions .values () if pos_info .current != Decimal (0 )]
122+ if not non_neutral :
123+ return
124+
125+ # Get bid and ask prices and calculate unrealized PnL.
126+ bids_and_asks = await asyncio .gather (* [self ._exchange .get_bid_ask (pos_info .pair ) for pos_info in non_neutral ])
127+ for pos_info , (bid , ask ) in zip (non_neutral , bids_and_asks ):
125128 pnl_pct = pos_info .calculate_unrealized_pnl_pct (bid , ask )
126129 logging .info (StructuredMessage (
127130 f"Position for { pos_info .pair } " , current = pos_info .current , target = pos_info .target ,
0 commit comments