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
trade_record = { self._sync_position_from_executor(symbol)
'symbol': symbol,
'side': action,
'quantity': size,
'entry_price': signal.get('price', 0),
'entry_time': datetime.now(),
'pnl': 0.0,
'fees': 0.001, # Small demo fee
'confidence': signal.get('confidence', 0),
'trade_type': 'auto_signal'
}
# Create/update current position for unrealized P&L tracking # Get trade history from executor for completed trades
current_price = signal.get('price', 0) executor_trades = self.trading_executor.get_trade_history() if hasattr(self.trading_executor, 'get_trade_history') else []
if action == 'BUY':
# Create or add to LONG position # Only add completed trades to closed_trades (not position opens)
self.current_position = { if executor_trades:
'side': 'LONG', latest_trade = executor_trades[-1]
'size': size, # Check if this is a completed trade (has exit price/time)
'price': current_price, if hasattr(latest_trade, 'exit_time') and latest_trade.exit_time:
'symbol': symbol, trade_record = {
'entry_time': datetime.now(), 'symbol': latest_trade.symbol,
'leverage': 50 'side': latest_trade.side,
} 'quantity': latest_trade.quantity,
logger.info(f"Auto-signal created LONG position: {size:.3f} @ ${current_price:.2f}") 'entry_price': latest_trade.entry_price,
else: # SELL 'exit_price': latest_trade.exit_price,
# Create SHORT position or close LONG 'entry_time': latest_trade.entry_time,
if self.current_position and self.current_position.get('side') == 'LONG': 'exit_time': latest_trade.exit_time,
# Close LONG position and calculate realized P&L 'pnl': latest_trade.pnl,
entry_price = self.current_position.get('price', 0) 'fees': latest_trade.fees,
position_size = self.current_position.get('size', 0) 'confidence': latest_trade.confidence,
if entry_price and position_size: 'trade_type': 'auto_signal'
# 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}")
# 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)
self.session_pnl += latest_trade.pnl
logger.info(f"Auto-signal completed trade: {action} P&L ${latest_trade.pnl:.2f}")
self.closed_trades.append(trade_record) # Position status will be shown from sync with executor
if self.current_position:
# Update session metrics immediately side = self.current_position.get('side', 'UNKNOWN')
if action == 'SELL': size = self.current_position.get('size', 0)
demo_pnl = 0.05 # Demo profit for SELL price = self.current_position.get('price', 0)
self.session_pnl += demo_pnl logger.info(f"Auto-signal position: {side} {size:.3f} @ ${price:.2f}")
trade_record['pnl'] = demo_pnl else:
trade_record['exit_price'] = signal.get('price', 0) logger.info(f"Auto-signal: No open position after {action}")
trade_record['exit_time'] = datetime.now()
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
trade_record = { self._sync_position_from_executor(symbol)
'symbol': symbol,
'side': action,
'quantity': 0.01,
'entry_price': current_price,
'exit_price': current_price,
'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,
'trade_type': 'manual',
'model_inputs_at_entry': model_inputs, # CRITICAL: Store model inputs for training
'entry_market_state': model_inputs.get('market_state', {}),
'entry_features': model_inputs.get('features', {}),
'entry_predictions': model_inputs.get('predictions', {}),
'training_ready': True # Mark as ready for cold start training
}
# Create/update current position for unrealized P&L tracking # Get trade history from executor for completed trades
if action == 'BUY': executor_trades = self.trading_executor.get_trade_history() if hasattr(self.trading_executor, 'get_trade_history') else []
# Create or add to LONG position
self.current_position = { # Only add completed trades to closed_trades (not position opens)
'side': 'LONG', if executor_trades:
'size': 0.004, # Example position size as mentioned by user latest_trade = executor_trades[-1]
'price': current_price, # Check if this is a completed trade (has exit price/time)
'symbol': symbol, if hasattr(latest_trade, 'exit_time') and latest_trade.exit_time:
'entry_time': datetime.now(), trade_record = {
'leverage': 50 'symbol': latest_trade.symbol,
} 'side': latest_trade.side,
logger.info(f"Created LONG position: {self.current_position['size']:.3f} @ ${current_price:.2f}") 'quantity': latest_trade.quantity,
else: # SELL 'entry_price': latest_trade.entry_price,
# Create SHORT position or close LONG 'exit_price': latest_trade.exit_price,
if self.current_position and self.current_position.get('side') == 'LONG': 'entry_time': latest_trade.entry_time,
# Close LONG position 'exit_time': latest_trade.exit_time,
self.current_position = None 'pnl': latest_trade.pnl,
logger.info("Closed LONG position") 'fees': latest_trade.fees,
else: 'confidence': latest_trade.confidence,
# Create SHORT position 'trade_type': 'manual',
self.current_position = { 'model_inputs_at_entry': model_inputs,
'side': 'SHORT', 'training_ready': True
'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}")
# 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}")
# Add to closed trades for display # Store for cold start training when trade closes using core TradeDataManager
self.closed_trades.append(trade_record) try:
case_id = trade_data_manager.store_trade_for_training({
'symbol': symbol,
'side': action,
'quantity': 0.01,
'entry_price': current_price,
'pnl': 0.0, # Will be updated when position closes
'confidence': 1.0,
'trade_type': 'manual',
'model_inputs_at_entry': model_inputs,
'training_ready': True
})
if case_id:
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}")
# Store for cold start training when trade closes using core TradeDataManager # Store for cold start training when trade closes using core TradeDataManager
try: try: