122 lines
4.4 KiB
Python
122 lines
4.4 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Test Open Order Sync and Fee Calculation
|
|
Verify that open orders are properly synchronized and fees are correctly calculated in PnL
|
|
"""
|
|
|
|
import os
|
|
import sys
|
|
import logging
|
|
|
|
# Add the project root to the path
|
|
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
# Load environment variables
|
|
try:
|
|
from dotenv import load_dotenv
|
|
load_dotenv()
|
|
except ImportError:
|
|
if os.path.exists('.env'):
|
|
with open('.env', 'r') as f:
|
|
for line in f:
|
|
if line.strip() and not line.startswith('#'):
|
|
key, value = line.strip().split('=', 1)
|
|
os.environ[key] = value
|
|
|
|
from core.trading_executor import TradingExecutor
|
|
|
|
# Configure logging
|
|
logging.basicConfig(
|
|
level=logging.INFO,
|
|
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
|
)
|
|
logger = logging.getLogger(__name__)
|
|
|
|
def test_open_order_sync_and_fees():
|
|
"""Test open order synchronization and fee calculation"""
|
|
print("🧪 Testing Open Order Sync and Fee Calculation...")
|
|
print("=" * 70)
|
|
|
|
try:
|
|
# Create trading executor
|
|
executor = TradingExecutor()
|
|
|
|
print(f"📊 Current State Analysis:")
|
|
print(f" Open orders count: {executor._get_open_orders_count()}")
|
|
print(f" Max open orders: {executor.max_open_orders}")
|
|
print(f" Can place new order: {executor._can_place_new_order()}")
|
|
|
|
# Test open order synchronization
|
|
print(f"\n🔍 Open Order Sync Analysis:")
|
|
print(f" - Current sync method: _get_open_orders_count()")
|
|
print(f" - Counts orders across all symbols")
|
|
print(f" - Real-time API queries")
|
|
print(f" - Handles API errors gracefully")
|
|
|
|
# Check if there's a dedicated sync method
|
|
if hasattr(executor, 'sync_open_orders'):
|
|
print(f" ✅ Dedicated sync method exists")
|
|
else:
|
|
print(f" ⚠️ No dedicated sync method - using count method")
|
|
|
|
# Test fee calculation in PnL
|
|
print(f"\n💰 Fee Calculation Analysis:")
|
|
|
|
# Check fee calculation methods
|
|
if hasattr(executor, '_calculate_trading_fee'):
|
|
print(f" ✅ Fee calculation method exists")
|
|
else:
|
|
print(f" ❌ No dedicated fee calculation method")
|
|
|
|
# Check if fees are included in PnL
|
|
print(f"\n📈 PnL Fee Integration:")
|
|
print(f" - TradeRecord includes fees field")
|
|
print(f" - PnL calculation: pnl = gross_pnl - fees")
|
|
print(f" - Fee rates from config: taker_fee, maker_fee")
|
|
|
|
# Check fee sync
|
|
print(f"\n🔄 Fee Synchronization:")
|
|
if hasattr(executor, 'sync_fees_with_api'):
|
|
print(f" ✅ Fee sync method exists")
|
|
else:
|
|
print(f" ❌ No fee sync method")
|
|
|
|
# Check config sync
|
|
if hasattr(executor, 'config_sync'):
|
|
print(f" ✅ Config synchronizer exists")
|
|
else:
|
|
print(f" ❌ No config synchronizer")
|
|
|
|
print(f"\n📋 Issues Found:")
|
|
|
|
# Issue 1: No dedicated open order sync method
|
|
if not hasattr(executor, 'sync_open_orders'):
|
|
print(f" ❌ Missing: Dedicated open order synchronization method")
|
|
print(f" Current: Only counts orders, doesn't sync state")
|
|
|
|
# Issue 2: Fee calculation may not be comprehensive
|
|
print(f" ⚠️ Potential: Fee calculation uses simulated rates")
|
|
print(f" Should: Use actual API fees when available")
|
|
|
|
# Issue 3: Check if fees are properly tracked
|
|
print(f" ✅ Good: Fees are tracked in TradeRecord")
|
|
print(f" ✅ Good: PnL includes fee deduction")
|
|
|
|
print(f"\n🔧 Recommended Fixes:")
|
|
print(f" 1. Add dedicated open order sync method")
|
|
print(f" 2. Enhance fee calculation with real API data")
|
|
print(f" 3. Add periodic order state synchronization")
|
|
print(f" 4. Improve fee tracking accuracy")
|
|
|
|
return True
|
|
|
|
except Exception as e:
|
|
print(f"❌ Error testing order sync and fees: {e}")
|
|
return False
|
|
|
|
if __name__ == "__main__":
|
|
success = test_open_order_sync_and_fees()
|
|
if success:
|
|
print(f"\n🎉 Order sync and fee test completed!")
|
|
else:
|
|
print(f"\n💥 Order sync and fee test failed!") |