#!/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!")