limit max positions
This commit is contained in:
@ -118,6 +118,9 @@ class CleanTradingDashboard:
|
||||
)
|
||||
self.component_manager = DashboardComponentManager()
|
||||
|
||||
# Initialize enhanced position sync system
|
||||
self._initialize_enhanced_position_sync()
|
||||
|
||||
# Initialize Universal Data Adapter access through orchestrator
|
||||
if UNIVERSAL_DATA_AVAILABLE:
|
||||
self.universal_adapter = UniversalDataAdapter(self.data_provider)
|
||||
@ -2711,26 +2714,58 @@ class CleanTradingDashboard:
|
||||
}
|
||||
|
||||
def _sync_position_from_executor(self, symbol: str):
|
||||
"""Sync current position from trading executor"""
|
||||
"""Sync current position from trading executor and real Bybit positions"""
|
||||
try:
|
||||
if self.trading_executor and hasattr(self.trading_executor, 'get_current_position'):
|
||||
executor_position = self.trading_executor.get_current_position(symbol)
|
||||
if executor_position:
|
||||
# Update dashboard position to match executor
|
||||
self.current_position = {
|
||||
'side': executor_position.get('side', 'UNKNOWN'),
|
||||
'size': executor_position.get('size', 0),
|
||||
'price': executor_position.get('price', 0),
|
||||
'symbol': executor_position.get('symbol', symbol),
|
||||
'entry_time': executor_position.get('entry_time', datetime.now()),
|
||||
'leverage': self.current_leverage, # Store current leverage with position
|
||||
'unrealized_pnl': executor_position.get('unrealized_pnl', 0)
|
||||
}
|
||||
logger.debug(f"Synced position from executor: {self.current_position['side']} {self.current_position['size']:.3f}")
|
||||
# First try to get real position from Bybit
|
||||
real_position = None
|
||||
if self.trading_executor and hasattr(self.trading_executor, 'exchange'):
|
||||
try:
|
||||
# Get real positions from Bybit
|
||||
bybit_positions = self.trading_executor.exchange.get_positions(symbol)
|
||||
if bybit_positions:
|
||||
# Use the first real position found
|
||||
real_position = bybit_positions[0]
|
||||
logger.info(f"Found real Bybit position: {real_position}")
|
||||
else:
|
||||
logger.debug("No real positions found on Bybit")
|
||||
except Exception as e:
|
||||
logger.debug(f"Error getting real Bybit positions: {e}")
|
||||
|
||||
# If we have a real position, use it
|
||||
if real_position:
|
||||
self.current_position = {
|
||||
'side': 'LONG' if real_position.get('side', '').lower() == 'buy' else 'SHORT',
|
||||
'size': real_position.get('size', 0),
|
||||
'price': real_position.get('entry_price', 0),
|
||||
'symbol': real_position.get('symbol', symbol),
|
||||
'entry_time': datetime.now(), # We don't have entry time from API
|
||||
'leverage': real_position.get('leverage', self.current_leverage),
|
||||
'unrealized_pnl': real_position.get('unrealized_pnl', 0)
|
||||
}
|
||||
logger.info(f"Synced real Bybit position: {self.current_position['side']} {self.current_position['size']:.3f} @ ${self.current_position['price']:.2f}")
|
||||
else:
|
||||
# Fallback to executor's internal tracking
|
||||
if self.trading_executor and hasattr(self.trading_executor, 'get_current_position'):
|
||||
executor_position = self.trading_executor.get_current_position(symbol)
|
||||
if executor_position:
|
||||
# Update dashboard position to match executor
|
||||
self.current_position = {
|
||||
'side': executor_position.get('side', 'UNKNOWN'),
|
||||
'size': executor_position.get('size', 0),
|
||||
'price': executor_position.get('price', 0),
|
||||
'symbol': executor_position.get('symbol', symbol),
|
||||
'entry_time': executor_position.get('entry_time', datetime.now()),
|
||||
'leverage': self.current_leverage, # Store current leverage with position
|
||||
'unrealized_pnl': executor_position.get('unrealized_pnl', 0)
|
||||
}
|
||||
logger.debug(f"Synced position from executor: {self.current_position['side']} {self.current_position['size']:.3f}")
|
||||
else:
|
||||
# No position in executor
|
||||
self.current_position = None
|
||||
logger.debug("No position in trading executor")
|
||||
else:
|
||||
# No position in executor
|
||||
self.current_position = None
|
||||
logger.debug("No position in trading executor")
|
||||
logger.debug("No trading executor available")
|
||||
except Exception as e:
|
||||
logger.debug(f"Error syncing position from executor: {e}")
|
||||
|
||||
@ -3999,7 +4034,7 @@ class CleanTradingDashboard:
|
||||
logger.error(f"Error verifying position sync after trade: {e}")
|
||||
|
||||
def _periodic_position_sync_check(self):
|
||||
"""Periodically check and sync position with MEXC account"""
|
||||
"""Periodically check and sync position with Bybit account"""
|
||||
try:
|
||||
symbol = 'ETH/USDT'
|
||||
|
||||
@ -4007,26 +4042,18 @@ class CleanTradingDashboard:
|
||||
if not self.trading_executor or getattr(self.trading_executor, 'simulation_mode', True):
|
||||
return
|
||||
|
||||
# Determine current desired state based on dashboard position
|
||||
# Sync real positions from Bybit
|
||||
logger.debug(f"PERIODIC SYNC: Syncing real Bybit positions for {symbol}")
|
||||
self._sync_position_from_executor(symbol)
|
||||
|
||||
# Log current position state
|
||||
if self.current_position:
|
||||
side = self.current_position.get('side', 'UNKNOWN')
|
||||
if side.upper() in ['LONG', 'BUY']:
|
||||
desired_state = 'LONG'
|
||||
elif side.upper() in ['SHORT', 'SELL']:
|
||||
desired_state = 'SHORT'
|
||||
else:
|
||||
desired_state = 'NO_POSITION'
|
||||
size = self.current_position.get('size', 0)
|
||||
price = self.current_position.get('price', 0)
|
||||
logger.info(f"PERIODIC SYNC: Current position: {side} {size:.3f} @ ${price:.2f}")
|
||||
else:
|
||||
desired_state = 'NO_POSITION'
|
||||
|
||||
# Perform periodic sync check
|
||||
logger.debug(f"PERIODIC SYNC: Checking position sync for {symbol} (desired: {desired_state})")
|
||||
sync_success = self._sync_position_with_mexc(symbol, desired_state)
|
||||
|
||||
if sync_success:
|
||||
logger.debug(f"PERIODIC SYNC: Position sync verified for {symbol}")
|
||||
else:
|
||||
logger.warning(f"PERIODIC SYNC: Position sync issue detected for {symbol}")
|
||||
logger.info(f"PERIODIC SYNC: No current position for {symbol}")
|
||||
|
||||
except Exception as e:
|
||||
logger.debug(f"Error in periodic position sync check: {e}")
|
||||
@ -4914,6 +4941,25 @@ class CleanTradingDashboard:
|
||||
logger.error(f"Error initializing enhanced training system: {e}")
|
||||
self.training_system = None
|
||||
|
||||
def _initialize_enhanced_position_sync(self):
|
||||
"""Initialize enhanced position synchronization system"""
|
||||
try:
|
||||
logger.info("Initializing enhanced position sync system...")
|
||||
|
||||
# Initialize position sync if trading executor is available
|
||||
if self.trading_executor:
|
||||
# Set up periodic position sync
|
||||
self.position_sync_enabled = True
|
||||
self.position_sync_interval = 30 # seconds
|
||||
logger.info("Enhanced position sync system initialized")
|
||||
else:
|
||||
logger.warning("Trading executor not available - position sync disabled")
|
||||
self.position_sync_enabled = False
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error initializing enhanced position sync: {e}")
|
||||
self.position_sync_enabled = False
|
||||
|
||||
def _initialize_cob_integration(self):
|
||||
"""Initialize COB integration using orchestrator's COB system"""
|
||||
try:
|
||||
@ -6846,4 +6892,4 @@ def create_clean_dashboard(data_provider: Optional[DataProvider] = None, orchest
|
||||
)
|
||||
|
||||
|
||||
# test edit
|
||||
# test edit
|
||||
|
Reference in New Issue
Block a user