From 857d1b91f079227403f895a101b0017d3ab3f80c Mon Sep 17 00:00:00 2001 From: DiTus Date: Sun, 21 Dec 2025 10:41:55 +0100 Subject: [PATCH] feat(kpi): integrate live Hyperliquid equity tracking for accurate NAV --- clp_hedger.py | 19 ++++++++++++++++--- uniswap_manager.py | 2 +- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/clp_hedger.py b/clp_hedger.py index dd67b1e..0f2b570 100644 --- a/clp_hedger.py +++ b/clp_hedger.py @@ -487,14 +487,21 @@ class ScalperHedger: def get_current_position(self, coin: str) -> Dict[str, Decimal]: try: user_state = self.info.user_state(self.vault_address or self.account.address) + + # Extract total account equity (marginSummary.accountValue) + equity = Decimal("0") + if "marginSummary" in user_state and "accountValue" in user_state["marginSummary"]: + equity = to_decimal(user_state["marginSummary"]["accountValue"]) + for pos in user_state["assetPositions"]: if pos["position"]["coin"] == coin: return { 'size': to_decimal(pos["position"]["szi"]), - 'pnl': to_decimal(pos["position"]["unrealizedPnl"]) + 'pnl': to_decimal(pos["position"]["unrealizedPnl"]), + 'equity': equity } - return {'size': Decimal("0"), 'pnl': Decimal("0")} - except: return {'size': Decimal("0"), 'pnl': Decimal("0")} + return {'size': Decimal("0"), 'pnl': Decimal("0"), 'equity': equity} + except: return {'size': Decimal("0"), 'pnl': Decimal("0"), 'equity': Decimal("0")} def get_open_orders(self) -> List[Dict]: try: @@ -757,6 +764,12 @@ class ScalperHedger: pos_data = self.get_current_position(COIN_SYMBOL) current_size = pos_data['size'] current_pnl = pos_data['pnl'] + current_equity = pos_data['equity'] + + # Update JSON with latest equity stats + update_position_stats(self.active_position_id, { + "hedge_equity_usd": float(current_equity) + }) # 3. Calculate Logic calc = self.strategy.calculate_rebalance(price, current_size) diff --git a/uniswap_manager.py b/uniswap_manager.py index d474c24..345b695 100644 --- a/uniswap_manager.py +++ b/uniswap_manager.py @@ -779,7 +779,7 @@ def main(): 'uniswap_fees_unclaimed_usd': float(total_fees_usd), # Hedge Data (from JSON updated by clp_hedger) - 'hedge_equity_usd': 0.0, # TODO: Need to fetch live equity from Hedger or API + 'hedge_equity_usd': float(active_auto_pos.get('hedge_equity_usd', 0.0)), 'hedge_pnl_realized_usd': active_auto_pos.get('hedge_pnl_realized', 0.0), 'hedge_fees_paid_usd': active_auto_pos.get('hedge_fees_paid', 0.0) }