wip UI clear session
This commit is contained in:
@ -654,6 +654,60 @@ class TradingOrchestrator:
|
||||
|
||||
return stats
|
||||
|
||||
def clear_session_data(self):
|
||||
"""Clear all session-related data for fresh start"""
|
||||
try:
|
||||
# Clear recent decisions and predictions
|
||||
self.recent_decisions = {}
|
||||
self.last_decision_time = {}
|
||||
self.last_signal_time = {}
|
||||
self.last_confirmed_signal = {}
|
||||
self.signal_accumulator = {self.symbol: []}
|
||||
|
||||
# Clear prediction tracking
|
||||
for symbol in self.recent_dqn_predictions:
|
||||
self.recent_dqn_predictions[symbol].clear()
|
||||
for symbol in self.recent_cnn_predictions:
|
||||
self.recent_cnn_predictions[symbol].clear()
|
||||
for symbol in self.prediction_accuracy_history:
|
||||
self.prediction_accuracy_history[symbol].clear()
|
||||
|
||||
# Close any open positions before clearing tracking
|
||||
self._close_all_positions()
|
||||
|
||||
# Clear position tracking
|
||||
self.current_positions = {}
|
||||
self.position_status = {}
|
||||
|
||||
# Clear training data (but keep model states)
|
||||
self.sensitivity_learning_queue = []
|
||||
self.perfect_move_buffer = []
|
||||
|
||||
# Clear inference history (but keep recent for training)
|
||||
for model_name in list(self.inference_history.keys()):
|
||||
# Keep only the last inference for each model to maintain training capability
|
||||
if len(self.inference_history[model_name]) > 1:
|
||||
last_inference = self.inference_history[model_name][-1]
|
||||
self.inference_history[model_name].clear()
|
||||
self.inference_history[model_name].append(last_inference)
|
||||
|
||||
# Clear fusion training data
|
||||
self.fusion_training_data = []
|
||||
self.last_fusion_inputs = {}
|
||||
|
||||
# Reset decision callbacks data
|
||||
for callback in self.decision_callbacks:
|
||||
if hasattr(callback, 'clear_session'):
|
||||
callback.clear_session()
|
||||
|
||||
logger.info("✅ Orchestrator session data cleared")
|
||||
logger.info("🧠 Model states preserved for continued training")
|
||||
logger.info("📊 Prediction history cleared")
|
||||
logger.info("💼 Position tracking reset")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"❌ Error clearing orchestrator session data: {e}")
|
||||
|
||||
def sync_model_states_with_dashboard(self):
|
||||
"""Sync model states with current dashboard values"""
|
||||
# Update based on the dashboard stats provided
|
||||
@ -3022,6 +3076,62 @@ class TradingOrchestrator:
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
def _close_all_positions(self):
|
||||
"""Close all open positions when clearing session"""
|
||||
try:
|
||||
if not self.trading_executor:
|
||||
logger.debug("No trading executor available - cannot close positions")
|
||||
return
|
||||
|
||||
# Get list of symbols to check for positions
|
||||
symbols_to_check = [self.symbol] + self.ref_symbols
|
||||
positions_closed = 0
|
||||
|
||||
for symbol in symbols_to_check:
|
||||
try:
|
||||
# Check if there's an open position
|
||||
if self._has_open_position(symbol):
|
||||
logger.info(f"Closing open position for {symbol}")
|
||||
|
||||
# Get current position details
|
||||
if hasattr(self.trading_executor, 'get_current_position'):
|
||||
position = self.trading_executor.get_current_position(symbol)
|
||||
if position:
|
||||
side = position.get('side', 'LONG')
|
||||
size = position.get('size', 0)
|
||||
|
||||
# Determine close action (opposite of current position)
|
||||
close_action = 'SELL' if side.upper() == 'LONG' else 'BUY'
|
||||
|
||||
# Execute close order
|
||||
if hasattr(self.trading_executor, 'execute_trade'):
|
||||
result = self.trading_executor.execute_trade(
|
||||
symbol=symbol,
|
||||
action=close_action,
|
||||
size=size,
|
||||
reason="Session clear - closing all positions"
|
||||
)
|
||||
|
||||
if result and result.get('success'):
|
||||
positions_closed += 1
|
||||
logger.info(f"✅ Closed {side} position for {symbol}: {size} units")
|
||||
else:
|
||||
logger.warning(f"⚠️ Failed to close position for {symbol}: {result}")
|
||||
else:
|
||||
logger.warning(f"Trading executor has no execute_trade method")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error closing position for {symbol}: {e}")
|
||||
continue
|
||||
|
||||
if positions_closed > 0:
|
||||
logger.info(f"✅ Closed {positions_closed} open positions during session clear")
|
||||
else:
|
||||
logger.debug("No open positions to close")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error closing positions during session clear: {e}")
|
||||
|
||||
def _calculate_aggressiveness_thresholds(self, current_pnl: float, symbol: str) -> tuple:
|
||||
"""Calculate confidence thresholds based on aggressiveness settings"""
|
||||
# Base thresholds
|
||||
|
Reference in New Issue
Block a user