Files
gogo2/test_leverage_fix.py
Dobromir Popov 3e35b9cddb leverage calc fix
2025-07-20 22:41:37 +03:00

75 lines
2.5 KiB
Python

#!/usr/bin/env python3
"""
Test Leverage Fix
This script tests if the leverage is now being applied correctly to trade P&L calculations.
"""
import sys
import os
from datetime import datetime
# Add project root to path
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from core.trading_executor import TradingExecutor, Position
def test_leverage_fix():
"""Test that leverage is now being applied correctly"""
print("🧪 Testing Leverage Fix")
print("=" * 50)
# Create trading executor
executor = TradingExecutor()
# Check current leverage setting
current_leverage = executor.get_leverage()
print(f"Current leverage setting: x{current_leverage}")
# Test leverage in P&L calculation
position = Position(
symbol="ETH/USDT",
side="SHORT",
quantity=0.005, # 0.005 ETH
entry_price=3755.33,
entry_time=datetime.now(),
order_id="test_123"
)
# Test P&L calculation with current price
current_price = 3740.51 # Price went down, should be profitable for SHORT
# Calculate P&L with leverage
pnl_with_leverage = position.calculate_pnl(current_price, leverage=current_leverage)
pnl_without_leverage = position.calculate_pnl(current_price, leverage=1.0)
print(f"\nPosition: SHORT 0.005 ETH @ $3755.33")
print(f"Current price: $3740.51")
print(f"Price difference: ${3755.33 - 3740.51:.2f} (favorable for SHORT)")
print(f"\nP&L without leverage (x1): ${pnl_without_leverage:.2f}")
print(f"P&L with leverage (x{current_leverage}): ${pnl_with_leverage:.2f}")
print(f"Leverage multiplier effect: {pnl_with_leverage / pnl_without_leverage:.1f}x")
# Expected calculation
position_value = 0.005 * 3755.33 # ~$18.78
price_diff = 3755.33 - 3740.51 # $14.82 favorable
raw_pnl = price_diff * 0.005 # ~$0.074
leveraged_pnl = raw_pnl * current_leverage # ~$3.70
print(f"\nExpected calculation:")
print(f"Position value: ${position_value:.2f}")
print(f"Raw P&L: ${raw_pnl:.3f}")
print(f"Leveraged P&L (before fees): ${leveraged_pnl:.2f}")
# Check if the calculation is correct
if abs(pnl_with_leverage - leveraged_pnl) < 0.1: # Allow for small fee differences
print("✅ Leverage calculation appears correct!")
else:
print("❌ Leverage calculation may have issues")
print("\n" + "=" * 50)
print("Test completed. Check if new trades show leveraged P&L in dashboard.")
if __name__ == "__main__":
test_leverage_fix()