trading risk management

This commit is contained in:
Dobromir Popov
2025-07-28 16:42:11 +03:00
parent 44821b2a89
commit db23ad10da
3 changed files with 305 additions and 90 deletions

View File

@ -1245,92 +1245,94 @@ class CleanTradingDashboard:
return [html.I(className="fas fa-exclamation-triangle me-1"), "Store Failed"]
return [html.I(className="fas fa-save me-1"), "Store All Models"]
# Trading Mode Toggle
@self.app.callback(
Output('trading-mode-display', 'children'),
Output('trading-mode-display', 'className'),
[Input('trading-mode-switch', 'value')]
)
def update_trading_mode(switch_value):
"""Update trading mode display and apply changes"""
try:
is_live = 'live' in (switch_value or [])
self.trading_mode_live = is_live
# Update trading executor mode if available
if hasattr(self, 'trading_executor') and self.trading_executor:
if hasattr(self.trading_executor, 'set_trading_mode'):
# Use the new set_trading_mode method
success = self.trading_executor.set_trading_mode('live' if is_live else 'simulation')
if success:
logger.info(f"TRADING MODE: {'LIVE' if is_live else 'SIMULATION'} - Mode updated successfully")
else:
logger.error(f"Failed to update trading mode to {'LIVE' if is_live else 'SIMULATION'}")
else:
# Fallback to direct property setting
if is_live:
self.trading_executor.trading_mode = 'live'
self.trading_executor.simulation_mode = False
logger.info("TRADING MODE: LIVE - Real orders will be executed!")
else:
self.trading_executor.trading_mode = 'simulation'
self.trading_executor.simulation_mode = True
logger.info("TRADING MODE: SIMULATION - Orders are simulated")
# Return display text and styling
if is_live:
return "LIVE", "fw-bold text-danger"
else:
return "SIM", "fw-bold text-warning"
except Exception as e:
logger.error(f"Error updating trading mode: {e}")
return "ERROR", "fw-bold text-danger"
# Trading Mode Toggle - TEMPORARILY DISABLED TO FIX UI ERROR
# @self.app.callback(
# Output('trading-mode-display', 'children'),
# Output('trading-mode-display', 'className'),
# [Input('trading-mode-switch', 'value')]
# )
# def update_trading_mode(switch_value):
# """Update trading mode display and apply changes"""
# logger.debug(f"Trading mode callback triggered with value: {switch_value}")
# try:
# is_live = 'live' in (switch_value or [])
# self.trading_mode_live = is_live
#
# # Update trading executor mode if available
# if hasattr(self, 'trading_executor') and self.trading_executor:
# if hasattr(self.trading_executor, 'set_trading_mode'):
# # Use the new set_trading_mode method
# success = self.trading_executor.set_trading_mode('live' if is_live else 'simulation')
# if success:
# logger.info(f"TRADING MODE: {'LIVE' if is_live else 'SIMULATION'} - Mode updated successfully")
# else:
# logger.error(f"Failed to update trading mode to {'LIVE' if is_live else 'SIMULATION'}")
# else:
# # Fallback to direct property setting
# if is_live:
# self.trading_executor.trading_mode = 'live'
# self.trading_executor.simulation_mode = False
# logger.info("TRADING MODE: LIVE - Real orders will be executed!")
# else:
# self.trading_executor.trading_mode = 'simulation'
# self.trading_executor.simulation_mode = True
# logger.info("TRADING MODE: SIMULATION - Orders are simulated")
#
# # Return display text and styling
# if is_live:
# return "LIVE", "fw-bold text-danger"
# else:
# return "SIM", "fw-bold text-warning"
#
# except Exception as e:
# logger.error(f"Error updating trading mode: {e}")
# return "ERROR", "fw-bold text-danger"
# Cold Start Toggle
@self.app.callback(
Output('cold-start-display', 'children'),
Output('cold-start-display', 'className'),
[Input('cold-start-switch', 'value')]
)
def update_cold_start(switch_value):
"""Update cold start training mode"""
try:
is_enabled = 'enabled' in (switch_value or [])
self.cold_start_enabled = is_enabled
# Update orchestrator cold start mode if available
if hasattr(self, 'orchestrator') and self.orchestrator:
if hasattr(self.orchestrator, 'set_cold_start_training_enabled'):
# Use the new set_cold_start_training_enabled method
success = self.orchestrator.set_cold_start_training_enabled(is_enabled)
if success:
logger.info(f"COLD START: {'ON' if is_enabled else 'OFF'} - Training mode updated successfully")
else:
logger.error(f"Failed to update cold start training to {'ON' if is_enabled else 'OFF'}")
else:
# Fallback to direct property setting
if hasattr(self.orchestrator, 'cold_start_enabled'):
self.orchestrator.cold_start_enabled = is_enabled
# Update training frequency based on cold start mode
if hasattr(self.orchestrator, 'training_frequency'):
if is_enabled:
self.orchestrator.training_frequency = 'high' # Train on every signal
logger.info("COLD START: ON - Excessive training enabled")
else:
self.orchestrator.training_frequency = 'normal' # Normal training
logger.info("COLD START: OFF - Normal training frequency")
# Return display text and styling
if is_enabled:
return "ON", "fw-bold text-success"
else:
return "OFF", "fw-bold text-secondary"
except Exception as e:
logger.error(f"Error updating cold start mode: {e}")
return "ERROR", "fw-bold text-danger"
# Cold Start Toggle - TEMPORARILY DISABLED TO FIX UI ERROR
# @self.app.callback(
# Output('cold-start-display', 'children'),
# Output('cold-start-display', 'className'),
# [Input('cold-start-switch', 'value')]
# )
# def update_cold_start(switch_value):
# """Update cold start training mode"""
# logger.debug(f"Cold start callback triggered with value: {switch_value}")
# try:
# is_enabled = 'enabled' in (switch_value or [])
# self.cold_start_enabled = is_enabled
#
# # Update orchestrator cold start mode if available
# if hasattr(self, 'orchestrator') and self.orchestrator:
# if hasattr(self.orchestrator, 'set_cold_start_training_enabled'):
# # Use the new set_cold_start_training_enabled method
# success = self.orchestrator.set_cold_start_training_enabled(is_enabled)
# if success:
# logger.info(f"COLD START: {'ON' if is_enabled else 'OFF'} - Training mode updated successfully")
# else:
# logger.error(f"Failed to update cold start training to {'ON' if is_enabled else 'OFF'}")
# else:
# # Fallback to direct property setting
# if hasattr(self.orchestrator, 'cold_start_enabled'):
# self.orchestrator.cold_start_enabled = is_enabled
#
# # Update training frequency based on cold start mode
# if hasattr(self.orchestrator, 'training_frequency'):
# if is_enabled:
# self.orchestrator.training_frequency = 'high' # Train on every signal
# logger.info("COLD START: ON - Excessive training enabled")
# else:
# self.orchestrator.training_frequency = 'normal' # Normal training
# logger.info("COLD START: OFF - Normal training frequency")
#
# # Return display text and styling
# if is_enabled:
# return "ON", "fw-bold text-success"
# else:
# return "OFF", "fw-bold text-secondary"
#
# except Exception as e:
# logger.error(f"Error updating cold start mode: {e}")
# return "ERROR", "fw-bold text-danger"
def _get_current_price(self, symbol: str) -> Optional[float]:
"""Get current price for symbol - ONLY using our data providers"""