From 4ab35ab879249440576ca9a71ab747a8886eed20 Mon Sep 17 00:00:00 2001 From: DiTus Date: Sat, 20 Dec 2025 12:52:05 +0100 Subject: [PATCH] feat(manager): display Total PnL (Unrealized + Fees) in logs --- uniswap_manager.py | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/uniswap_manager.py b/uniswap_manager.py index 1849054..79e4e82 100644 --- a/uniswap_manager.py +++ b/uniswap_manager.py @@ -116,7 +116,7 @@ WETH_ADDRESS = "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1" USDC_ADDRESS = "0xaf88d065e77c8cC2239327C5EDb3A432268e5831" STATUS_FILE = "hedge_status.json" -MONITOR_INTERVAL_SECONDS = 60 +MONITOR_INTERVAL_SECONDS = 666 CLOSE_POSITION_ENABLED = True OPEN_POSITION_ENABLED = True REBALANCE_ON_CLOSE_BELOW_RANGE = True @@ -735,8 +735,31 @@ def main(): except Exception as e: logger.debug(f"Fee simulation failed for {token_id}: {e}") - fee_text = f" | Fees: {unclaimed0:.4f}/{unclaimed1:.2f} (~${total_fees_usd:.2f})" - logger.info(f"Position {token_id}: {status_msg} | Price: {current_price:.4f} [{lower_price:.4f} - {upper_price:.4f}]{fee_text}") + # Calculate Total PnL (Fees + Price Appreciation/Depreciation) + # We need the initial investment value (target_value) + initial_value = Decimal(str(active_auto_pos.get('target_value', 0))) + + # Estimate Current Position Liquidity Value (approximate) + # For exact value, we'd need amounts for liquidity at current tick + # But we can approximate using the target value logic reversed or just assume target ~ current if range is tight and price is close. + # BETTER: Use get_amounts_for_liquidity with current price to get current holdings + + curr_amt0_wei, curr_amt1_wei = get_amounts_for_liquidity( + pool_data['sqrtPriceX96'], + get_sqrt_ratio_at_tick(tick_lower), + get_sqrt_ratio_at_tick(tick_upper), + pos_details['liquidity'] + ) + curr_amt0 = Decimal(curr_amt0_wei) / Decimal(10**pos_details['token0_decimals']) + curr_amt1 = Decimal(curr_amt1_wei) / Decimal(10**pos_details['token1_decimals']) + + current_pos_value_usd = (curr_amt0 * current_price) + curr_amt1 + + pnl_unrealized = current_pos_value_usd - initial_value + total_pnl_usd = pnl_unrealized + total_fees_usd + + 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}") if not in_range and CLOSE_POSITION_ENABLED: logger.warning(f"🛑 Closing Position {token_id} (Out of Range)")