feat(kpi): integrate live Hyperliquid equity tracking for accurate NAV

This commit is contained in:
2025-12-21 10:41:55 +01:00
parent 42e0dfc5c6
commit 857d1b91f0
2 changed files with 17 additions and 4 deletions

View File

@ -487,14 +487,21 @@ class ScalperHedger:
def get_current_position(self, coin: str) -> Dict[str, Decimal]: def get_current_position(self, coin: str) -> Dict[str, Decimal]:
try: try:
user_state = self.info.user_state(self.vault_address or self.account.address) 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"]: for pos in user_state["assetPositions"]:
if pos["position"]["coin"] == coin: if pos["position"]["coin"] == coin:
return { return {
'size': to_decimal(pos["position"]["szi"]), '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")} return {'size': Decimal("0"), 'pnl': Decimal("0"), 'equity': equity}
except: return {'size': Decimal("0"), 'pnl': Decimal("0")} except: return {'size': Decimal("0"), 'pnl': Decimal("0"), 'equity': Decimal("0")}
def get_open_orders(self) -> List[Dict]: def get_open_orders(self) -> List[Dict]:
try: try:
@ -757,6 +764,12 @@ class ScalperHedger:
pos_data = self.get_current_position(COIN_SYMBOL) pos_data = self.get_current_position(COIN_SYMBOL)
current_size = pos_data['size'] current_size = pos_data['size']
current_pnl = pos_data['pnl'] 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 # 3. Calculate Logic
calc = self.strategy.calculate_rebalance(price, current_size) calc = self.strategy.calculate_rebalance(price, current_size)

View File

@ -779,7 +779,7 @@ def main():
'uniswap_fees_unclaimed_usd': float(total_fees_usd), 'uniswap_fees_unclaimed_usd': float(total_fees_usd),
# Hedge Data (from JSON updated by clp_hedger) # 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_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) 'hedge_fees_paid_usd': active_auto_pos.get('hedge_fees_paid', 0.0)
} }