#!/usr/bin/env python3
"""
Test Expected vs Executed PnL avec nouveau slippage 2%
"""
import sys
sys.path.append('/var/www/html/HyperOracle/crypto_trader')
from hyperliquid_client import HyperliquidClient

def test_expected_vs_executed():
    client = HyperliquidClient()
    positions = client.get_positions()

    if not positions:
        print("Aucune position pour tester")
        return

    print("=== TEST EXPECTED VS EXECUTED PnL ===")
    print("Position | Size      | Entry    | Expected +2% | Executed (slip 2%)")
    print("-" * 70)

    total_expected = 0
    total_executed = 0

    for pos in positions[:5]:  # Test sur 5 positions max
        symbol = pos.get('symbol', '').replace('-PERP', '')
        size = float(pos.get('size', 0))
        entry = float(pos.get('entry_price', 0))

        if size != 0 and entry > 0:
            # Simuler mouvement +2% favorable
            position_value = abs(size) * entry

            if size > 0:  # LONG
                target_price = entry * 1.02
                movement_profit = position_value * 0.02  # +2%
            else:  # SHORT
                target_price = entry * 0.98
                movement_profit = position_value * 0.02  # +2%

            # Expected: profit théorique
            expected_profit = movement_profit

            # Executed: avec slippage 2%
            executed_profit = expected_profit * 0.98

            total_expected += expected_profit
            total_executed += executed_profit

            print(f"{symbol:4} | {size:8.3f} | ${entry:6.1f} | ${expected_profit:8.2f} | ${executed_profit:8.2f}")

    print("-" * 70)
    print(f"TOTAL | {'':8} | {'':6} | ${total_expected:8.2f} | ${total_executed:8.2f}")

    loss_due_to_slippage = total_expected - total_executed
    print(f"\n💡 AMÉLIORATION vs ancien slippage 5%:")
    print(f"   Perte avec slippage 2%: ${loss_due_to_slippage:.2f}")
    print(f"   Perte avec slippage 5%: ${total_expected * 0.05:.2f}")
    print(f"   GAIN: +${total_expected * 0.03:.2f} de profit conservé!")

if __name__ == "__main__":
    test_expected_vs_executed()