From 7d772a628aa53a9a6a5d45122bd836573ce8a604 Mon Sep 17 00:00:00 2001 From: DiTus Date: Sun, 21 Dec 2025 09:32:27 +0100 Subject: [PATCH] feat(kpi): integrate kpi_tracker into uniswap_manager main loop --- uniswap_manager.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/uniswap_manager.py b/uniswap_manager.py index 79e4e82..d474c24 100644 --- a/uniswap_manager.py +++ b/uniswap_manager.py @@ -15,6 +15,13 @@ from eth_account import Account from eth_account.signers.local import LocalAccount from dotenv import load_dotenv +# --- IMPORTS FOR KPI --- +try: + from tools.kpi_tracker import log_kpi_snapshot +except ImportError: + logging.warning("KPI Tracker not found. Performance logging disabled.") + log_kpi_snapshot = None + # Set Decimal precision high enough for EVM math getcontext().prec = 60 @@ -761,6 +768,30 @@ def main(): pnl_text = f" | TotPnL: ${total_pnl_usd:.2f} (Fees: ${total_fees_usd:.2f})" logger.info(f"Position {token_id}: {status_msg} | Price: {current_price:.4f} [{lower_price:.4f} - {upper_price:.4f}]{pnl_text}") + # --- KPI LOGGING --- + if log_kpi_snapshot: + snapshot = { + 'initial_eth': active_auto_pos.get('amount0_initial', 0), + 'initial_usdc': active_auto_pos.get('amount1_initial', 0), + 'current_eth_price': float(current_price), + 'uniswap_pos_value_usd': float(current_pos_value_usd), + 'uniswap_fees_claimed_usd': 0.0, # Not tracked accumulated yet in JSON, using Unclaimed mainly + '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_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) + } + # We use 'target_value' as a proxy for 'Initial Hedge Equity' + 'Initial Uni Val' if strictly tracking strategy? + # For now, let's pass what we have. + # To get 'hedge_equity', we ideally need clp_hedger to write it to JSON. + # Current implementation of kpi_tracker uses 'hedge_equity' in NAV. + # If we leave it 0, NAV will be underreported. + # WORKAROUND: Assume Hedge PnL Realized IS the equity change if we ignore margin. + + log_kpi_snapshot(snapshot) + if not in_range and CLOSE_POSITION_ENABLED: logger.warning(f"🛑 Closing Position {token_id} (Out of Range)") update_position_status(token_id, "CLOSING")