|
@@ -5,6 +5,7 @@ Handles synchronization of bot's position state with the exchange.
|
|
|
|
|
|
import logging
|
|
|
import asyncio
|
|
|
+import uuid
|
|
|
from datetime import datetime, timezone
|
|
|
from typing import Optional, Dict, Any, List
|
|
|
|
|
@@ -89,16 +90,17 @@ class PositionSynchronizer:
|
|
|
|
|
|
logger.info(f"🔄 AUTO-SYNC: Orphaned position detected - {symbol} {position_side.upper()} {actual_contracts_size} @ ${final_entry_price:.4f} {price_source_log}")
|
|
|
|
|
|
+ unique_sync_id = str(uuid.uuid4())[:8]
|
|
|
lifecycle_id = stats.create_trade_lifecycle(
|
|
|
symbol=symbol, side=order_side,
|
|
|
- entry_order_id=f"external_sync_{int(datetime.now(timezone.utc).timestamp())}",
|
|
|
+ entry_order_id=f"external_sync_{unique_sync_id}",
|
|
|
trade_type='external_sync'
|
|
|
)
|
|
|
|
|
|
if lifecycle_id:
|
|
|
success = stats.update_trade_position_opened(
|
|
|
lifecycle_id, final_entry_price, actual_contracts_size,
|
|
|
- f"external_fill_sync_{int(datetime.now(timezone.utc).timestamp())}"
|
|
|
+ f"external_fill_sync_{unique_sync_id}"
|
|
|
)
|
|
|
|
|
|
if success:
|
|
@@ -189,11 +191,12 @@ class PositionSynchronizer:
|
|
|
elif position_side == 'short':
|
|
|
realized_pnl = position_size_for_pnl * (entry_price - exit_price_for_calc)
|
|
|
|
|
|
+ unique_flat_id = str(uuid.uuid4())[:8]
|
|
|
success = stats.update_trade_position_closed(
|
|
|
lifecycle_id=lc_id,
|
|
|
exit_price=exit_price_for_calc,
|
|
|
realized_pnl=realized_pnl,
|
|
|
- exchange_fill_id=f"auto_sync_flat_{int(datetime.now(timezone.utc).timestamp())}"
|
|
|
+ exchange_fill_id=f"auto_sync_flat_{unique_flat_id}"
|
|
|
)
|
|
|
|
|
|
if success:
|
|
@@ -331,16 +334,17 @@ class PositionSynchronizer:
|
|
|
|
|
|
logger.info(f"🔄 STARTUP: Auto-syncing orphaned position: {symbol} {position_side.upper()} {formatter.format_amount(contracts_abs, token_for_log)} @ {formatter.format_price_with_symbol(entry_price, token_for_log)} {price_source_log}")
|
|
|
|
|
|
+ unique_id = str(uuid.uuid4())[:8]
|
|
|
lifecycle_id = stats.create_trade_lifecycle(
|
|
|
symbol=symbol, side=order_side,
|
|
|
- entry_order_id=f"startup_sync_{int(datetime.now(timezone.utc).timestamp())}",
|
|
|
+ entry_order_id=f"startup_sync_{unique_id}",
|
|
|
trade_type='external_startup_sync'
|
|
|
)
|
|
|
|
|
|
if lifecycle_id:
|
|
|
success = stats.update_trade_position_opened(
|
|
|
lifecycle_id, entry_price, contracts_abs,
|
|
|
- f"startup_fill_sync_{int(datetime.now(timezone.utc).timestamp())}"
|
|
|
+ f"startup_fill_sync_{unique_id}"
|
|
|
)
|
|
|
if success:
|
|
|
synced_count += 1
|
|
@@ -415,11 +419,12 @@ class PositionSynchronizer:
|
|
|
elif position_side_lc == 'short':
|
|
|
realized_pnl = position_size_for_pnl * (entry_price_lc - exit_price_for_calc)
|
|
|
|
|
|
+ unique_close_id = str(uuid.uuid4())[:8]
|
|
|
success_close = stats.update_trade_position_closed(
|
|
|
lifecycle_id=lc_id,
|
|
|
exit_price=exit_price_for_calc,
|
|
|
realized_pnl=realized_pnl,
|
|
|
- exchange_fill_id=f"startup_sync_flat_{int(datetime.now(timezone.utc).timestamp())}"
|
|
|
+ exchange_fill_id=f"startup_sync_flat_{unique_close_id}"
|
|
|
)
|
|
|
|
|
|
if success_close:
|