This commit is contained in:
Dobromir Popov
2025-06-25 22:29:08 +03:00
parent 651dbe2efa
commit 5dbc177016

View File

@ -237,6 +237,10 @@ class CleanTradingDashboard:
def update_metrics(n): def update_metrics(n):
"""Update key metrics""" """Update key metrics"""
try: try:
# Sync position from trading executor first
symbol = 'ETH/USDT'
self._sync_position_from_executor(symbol)
# Get current price # Get current price
current_price = self._get_current_price('ETH/USDT') current_price = self._get_current_price('ETH/USDT')
price_str = f"${current_price:.2f}" if current_price else "Loading..." price_str = f"${current_price:.2f}" if current_price else "Loading..."
@ -1290,6 +1294,30 @@ class CleanTradingDashboard:
logger.debug(f"Error checking signal generation status: {e}") logger.debug(f"Error checking signal generation status: {e}")
return False return False
def _sync_position_from_executor(self, symbol: str):
"""Sync current position from trading executor"""
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': 50,
'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")
except Exception as e:
logger.debug(f"Error syncing position from executor: {e}")
def _get_cnn_pivot_prediction(self) -> Optional[Dict]: def _get_cnn_pivot_prediction(self) -> Optional[Dict]:
"""Get CNN pivot point prediction from orchestrator""" """Get CNN pivot point prediction from orchestrator"""
try: try:
@ -1539,67 +1567,45 @@ class CleanTradingDashboard:
logger.info(f"EXECUTED {action} signal: {symbol} @ ${signal.get('price', 0):.2f} " logger.info(f"EXECUTED {action} signal: {symbol} @ ${signal.get('price', 0):.2f} "
f"(conf: {signal['confidence']:.2f}, size: {size}) - {execution_reason}") f"(conf: {signal['confidence']:.2f}, size: {size}) - {execution_reason}")
# Create trade record for tracking # Sync position from trading executor after execution
self._sync_position_from_executor(symbol)
# Get trade history from executor for completed trades
executor_trades = self.trading_executor.get_trade_history() if hasattr(self.trading_executor, 'get_trade_history') else []
# Only add completed trades to closed_trades (not position opens)
if executor_trades:
latest_trade = executor_trades[-1]
# Check if this is a completed trade (has exit price/time)
if hasattr(latest_trade, 'exit_time') and latest_trade.exit_time:
trade_record = { trade_record = {
'symbol': symbol, 'symbol': latest_trade.symbol,
'side': action, 'side': latest_trade.side,
'quantity': size, 'quantity': latest_trade.quantity,
'entry_price': signal.get('price', 0), 'entry_price': latest_trade.entry_price,
'entry_time': datetime.now(), 'exit_price': latest_trade.exit_price,
'pnl': 0.0, 'entry_time': latest_trade.entry_time,
'fees': 0.001, # Small demo fee 'exit_time': latest_trade.exit_time,
'confidence': signal.get('confidence', 0), 'pnl': latest_trade.pnl,
'fees': latest_trade.fees,
'confidence': latest_trade.confidence,
'trade_type': 'auto_signal' 'trade_type': 'auto_signal'
} }
# Create/update current position for unrealized P&L tracking # Only add if not already in closed_trades
current_price = signal.get('price', 0) if not any(t.get('entry_time') == trade_record['entry_time'] for t in self.closed_trades):
if action == 'BUY':
# Create or add to LONG position
self.current_position = {
'side': 'LONG',
'size': size,
'price': current_price,
'symbol': symbol,
'entry_time': datetime.now(),
'leverage': 50
}
logger.info(f"Auto-signal created LONG position: {size:.3f} @ ${current_price:.2f}")
else: # SELL
# Create SHORT position or close LONG
if self.current_position and self.current_position.get('side') == 'LONG':
# Close LONG position and calculate realized P&L
entry_price = self.current_position.get('price', 0)
position_size = self.current_position.get('size', 0)
if entry_price and position_size:
# Calculate leveraged P&L for position close
raw_pnl = (current_price - entry_price) * position_size
leveraged_pnl = raw_pnl * 50 # x50 leverage
self.session_pnl += leveraged_pnl
trade_record['pnl'] = leveraged_pnl
logger.info(f"Closed LONG position: P&L ${leveraged_pnl:.2f} (x50 leverage)")
self.current_position = None
else:
# Create SHORT position
self.current_position = {
'side': 'SHORT',
'size': size,
'price': current_price,
'symbol': symbol,
'entry_time': datetime.now(),
'leverage': 50
}
logger.info(f"Auto-signal created SHORT position: {size:.3f} @ ${current_price:.2f}")
self.closed_trades.append(trade_record) self.closed_trades.append(trade_record)
self.session_pnl += latest_trade.pnl
logger.info(f"Auto-signal completed trade: {action} P&L ${latest_trade.pnl:.2f}")
# Update session metrics immediately # Position status will be shown from sync with executor
if action == 'SELL': if self.current_position:
demo_pnl = 0.05 # Demo profit for SELL side = self.current_position.get('side', 'UNKNOWN')
self.session_pnl += demo_pnl size = self.current_position.get('size', 0)
trade_record['pnl'] = demo_pnl price = self.current_position.get('price', 0)
trade_record['exit_price'] = signal.get('price', 0) logger.info(f"Auto-signal position: {side} {size:.3f} @ ${price:.2f}")
trade_record['exit_time'] = datetime.now() else:
logger.info(f"Auto-signal: No open position after {action}")
else: else:
signal['blocked'] = True signal['blocked'] = True
@ -1689,6 +1695,9 @@ class CleanTradingDashboard:
logger.warning("No current price available for manual trade") logger.warning("No current price available for manual trade")
return return
# Sync current position from trading executor first
self._sync_position_from_executor(symbol)
# CAPTURE ALL MODEL INPUTS FOR COLD START TRAINING using core TradeDataManager # CAPTURE ALL MODEL INPUTS FOR COLD START TRAINING using core TradeDataManager
try: try:
from core.trade_data_manager import TradeDataManager from core.trade_data_manager import TradeDataManager
@ -1729,58 +1738,55 @@ class CleanTradingDashboard:
decision['executed'] = True decision['executed'] = True
logger.info(f"Manual {action} executed at ${current_price:.2f}") logger.info(f"Manual {action} executed at ${current_price:.2f}")
# Create a trade record for tracking WITH model inputs # Sync position from trading executor after execution
self._sync_position_from_executor(symbol)
# Get trade history from executor for completed trades
executor_trades = self.trading_executor.get_trade_history() if hasattr(self.trading_executor, 'get_trade_history') else []
# Only add completed trades to closed_trades (not position opens)
if executor_trades:
latest_trade = executor_trades[-1]
# Check if this is a completed trade (has exit price/time)
if hasattr(latest_trade, 'exit_time') and latest_trade.exit_time:
trade_record = { trade_record = {
'symbol': latest_trade.symbol,
'side': latest_trade.side,
'quantity': latest_trade.quantity,
'entry_price': latest_trade.entry_price,
'exit_price': latest_trade.exit_price,
'entry_time': latest_trade.entry_time,
'exit_time': latest_trade.exit_time,
'pnl': latest_trade.pnl,
'fees': latest_trade.fees,
'confidence': latest_trade.confidence,
'trade_type': 'manual',
'model_inputs_at_entry': model_inputs,
'training_ready': True
}
# Only add if not already in closed_trades
if not any(t.get('entry_time') == trade_record['entry_time'] for t in self.closed_trades):
self.closed_trades.append(trade_record)
logger.info(f"Added completed trade to closed_trades: {action} P&L ${latest_trade.pnl:.2f}")
# Store for cold start training when trade closes using core TradeDataManager
try:
case_id = trade_data_manager.store_trade_for_training({
'symbol': symbol, 'symbol': symbol,
'side': action, 'side': action,
'quantity': 0.01, 'quantity': 0.01,
'entry_price': current_price, 'entry_price': current_price,
'exit_price': current_price, 'pnl': 0.0, # Will be updated when position closes
'entry_time': datetime.now(),
'exit_time': datetime.now(),
'pnl': 0.0, # Manual test trades have 0 P&L initially
'fees': 0.0,
'confidence': 1.0, 'confidence': 1.0,
'trade_type': 'manual', 'trade_type': 'manual',
'model_inputs_at_entry': model_inputs, # CRITICAL: Store model inputs for training 'model_inputs_at_entry': model_inputs,
'entry_market_state': model_inputs.get('market_state', {}), 'training_ready': True
'entry_features': model_inputs.get('features', {}), })
'entry_predictions': model_inputs.get('predictions', {}), if case_id:
'training_ready': True # Mark as ready for cold start training logger.info(f"Trade stored for training with case ID: {case_id}")
} except Exception as e:
logger.warning(f"Failed to store trade for training: {e}")
# Create/update current position for unrealized P&L tracking
if action == 'BUY':
# Create or add to LONG position
self.current_position = {
'side': 'LONG',
'size': 0.004, # Example position size as mentioned by user
'price': current_price,
'symbol': symbol,
'entry_time': datetime.now(),
'leverage': 50
}
logger.info(f"Created LONG position: {self.current_position['size']:.3f} @ ${current_price:.2f}")
else: # SELL
# Create SHORT position or close LONG
if self.current_position and self.current_position.get('side') == 'LONG':
# Close LONG position
self.current_position = None
logger.info("Closed LONG position")
else:
# Create SHORT position
self.current_position = {
'side': 'SHORT',
'size': 0.004,
'price': current_price,
'symbol': symbol,
'entry_time': datetime.now(),
'leverage': 50
}
logger.info(f"Created SHORT position: {self.current_position['size']:.3f} @ ${current_price:.2f}")
# Add to closed trades for display
self.closed_trades.append(trade_record)
# Store for cold start training when trade closes using core TradeDataManager # Store for cold start training when trade closes using core TradeDataManager
try: try: