cleanup, cob ladder still broken

This commit is contained in:
Dobromir Popov
2025-07-07 01:07:48 +03:00
parent 97d9bc97ee
commit 9101448e78
75 changed files with 546 additions and 12123 deletions

View File

@ -113,6 +113,15 @@ class DQNAgent:
# Initialize avg_reward for dashboard compatibility
self.avg_reward = 0.0 # Average reward tracking for dashboard
# Market regime adaptation weights
self.market_regime_weights = {
'trending': 1.0,
'sideways': 0.8,
'volatile': 1.2,
'bullish': 1.1,
'bearish': 1.1
}
# Load best checkpoint if available
if self.enable_checkpoints:
self.load_best_checkpoint()
@ -490,7 +499,17 @@ class DQNAgent:
state_tensor = torch.FloatTensor(state).unsqueeze(0).to(self.device)
q_values = self.policy_net(state_tensor)
# Ensure q_values has correct shape for softmax
# Handle case where network might return a tuple instead of tensor
if isinstance(q_values, tuple):
# If it's a tuple, take the first element (usually the main output)
q_values = q_values[0]
# Ensure q_values is a tensor and has correct shape for softmax
if not hasattr(q_values, 'dim'):
logger.error(f"DQN: q_values is not a tensor: {type(q_values)}")
# Return default action with low confidence
return 1, 0.1 # Default to HOLD action
if q_values.dim() == 1:
q_values = q_values.unsqueeze(0)

View File

@ -117,52 +117,52 @@ class EnhancedCNN(nn.Module):
# Ultra massive convolutional backbone with much deeper residual blocks
self.conv_layers = nn.Sequential(
# Initial ultra large conv block
nn.Conv1d(self.channels, 512, kernel_size=7, padding=3), # Ultra wide initial layer
nn.BatchNorm1d(512),
nn.Conv1d(self.channels, 1024, kernel_size=7, padding=3), # Ultra wide initial layer (increased from 512)
nn.BatchNorm1d(1024),
nn.ReLU(),
nn.Dropout(0.1),
# First residual stage - 512 channels
ResidualBlock(512, 768),
ResidualBlock(768, 768),
ResidualBlock(768, 768),
ResidualBlock(768, 768), # Additional layer
nn.MaxPool1d(kernel_size=2, stride=2),
nn.Dropout(0.2),
# Second residual stage - 768 to 1024 channels
ResidualBlock(768, 1024),
ResidualBlock(1024, 1024),
ResidualBlock(1024, 1024),
ResidualBlock(1024, 1024), # Additional layer
nn.MaxPool1d(kernel_size=2, stride=2),
nn.Dropout(0.25),
# Third residual stage - 1024 to 1536 channels
ResidualBlock(1024, 1536),
# First residual stage - 1024 channels (increased from 512)
ResidualBlock(1024, 1536), # Increased from 768
ResidualBlock(1536, 1536),
ResidualBlock(1536, 1536),
ResidualBlock(1536, 1536), # Additional layer
nn.MaxPool1d(kernel_size=2, stride=2),
nn.Dropout(0.3),
nn.Dropout(0.2),
# Fourth residual stage - 1536 to 2048 channels
# Second residual stage - 1536 to 2048 channels (increased from 768 to 1024)
ResidualBlock(1536, 2048),
ResidualBlock(2048, 2048),
ResidualBlock(2048, 2048),
ResidualBlock(2048, 2048), # Additional layer
nn.MaxPool1d(kernel_size=2, stride=2),
nn.Dropout(0.3),
nn.Dropout(0.25),
# Fifth residual stage - ULTRA MASSIVE 2048 to 3072 channels
# Third residual stage - 2048 to 3072 channels (increased from 1024 to 1536)
ResidualBlock(2048, 3072),
ResidualBlock(3072, 3072),
ResidualBlock(3072, 3072),
ResidualBlock(3072, 3072),
ResidualBlock(3072, 3072), # Additional layer
nn.MaxPool1d(kernel_size=2, stride=2),
nn.Dropout(0.3),
# Fourth residual stage - 3072 to 4096 channels (increased from 1536 to 2048)
ResidualBlock(3072, 4096),
ResidualBlock(4096, 4096),
ResidualBlock(4096, 4096),
ResidualBlock(4096, 4096), # Additional layer
nn.MaxPool1d(kernel_size=2, stride=2),
nn.Dropout(0.3),
# Fifth residual stage - ULTRA MASSIVE 4096 to 6144 channels (increased from 2048 to 3072)
ResidualBlock(4096, 6144),
ResidualBlock(6144, 6144),
ResidualBlock(6144, 6144),
ResidualBlock(6144, 6144),
nn.AdaptiveAvgPool1d(1) # Global average pooling
)
# Ultra massive feature dimension after conv layers
self.conv_features = 3072
self.conv_features = 6144 # Increased from 3072
else:
# For 1D vectors, use ultra massive dense preprocessing
self.conv_layers = None
@ -171,36 +171,36 @@ class EnhancedCNN(nn.Module):
# ULTRA MASSIVE fully connected feature extraction layers
if self.conv_layers is None:
# For 1D inputs - ultra massive feature extraction
self.fc1 = nn.Linear(self.feature_dim, 3072)
self.features_dim = 3072
self.fc1 = nn.Linear(self.feature_dim, 6144) # Increased from 3072
self.features_dim = 6144 # Increased from 3072
else:
# For data processed by ultra massive conv layers
self.fc1 = nn.Linear(self.conv_features, 3072)
self.features_dim = 3072
self.fc1 = nn.Linear(self.conv_features, 6144) # Increased from 3072
self.features_dim = 6144 # Increased from 3072
# ULTRA MASSIVE common feature extraction with multiple deep layers
self.fc_layers = nn.Sequential(
self.fc1,
nn.ReLU(),
nn.Dropout(0.3),
nn.Linear(3072, 3072), # Keep ultra massive width
nn.Linear(6144, 6144), # Keep ultra massive width (increased from 3072)
nn.ReLU(),
nn.Dropout(0.3),
nn.Linear(3072, 2560), # Ultra wide hidden layer
nn.Linear(6144, 4096), # Ultra wide hidden layer (increased from 2560)
nn.ReLU(),
nn.Dropout(0.3),
nn.Linear(2560, 2048), # Still very wide
nn.Linear(4096, 3072), # Still very wide (increased from 2048)
nn.ReLU(),
nn.Dropout(0.3),
nn.Linear(2048, 1536), # Large hidden layer
nn.Linear(3072, 2048), # Large hidden layer (increased from 1536)
nn.ReLU(),
nn.Dropout(0.3),
nn.Linear(1536, 1024), # Final feature representation
nn.Linear(2048, 1024), # Final feature representation (increased from 1024, but keeping the same value to align with attention layers)
nn.ReLU()
)
# Multiple attention mechanisms for different aspects (larger capacity)
self.price_attention = SelfAttention(1024) # Increased from 768
# Multiple specialized attention mechanisms (larger capacity)
self.price_attention = SelfAttention(1024) # Keeping 1024
self.volume_attention = SelfAttention(1024)
self.trend_attention = SelfAttention(1024)
self.volatility_attention = SelfAttention(1024)
@ -209,108 +209,108 @@ class EnhancedCNN(nn.Module):
# Ultra massive attention fusion layer
self.attention_fusion = nn.Sequential(
nn.Linear(1024 * 6, 2048), # Combine all 6 attention outputs
nn.Linear(1024 * 6, 4096), # Combine all 6 attention outputs (increased from 2048)
nn.ReLU(),
nn.Dropout(0.3),
nn.Linear(2048, 1536),
nn.Linear(4096, 3072), # Increased from 1536
nn.ReLU(),
nn.Dropout(0.3),
nn.Linear(1536, 1024)
nn.Linear(3072, 1024) # Keeping 1024
)
# ULTRA MASSIVE dueling architecture with much deeper networks
self.advantage_stream = nn.Sequential(
nn.Linear(1024, 768),
nn.Linear(1024, 1536), # Increased from 768
nn.ReLU(),
nn.Dropout(0.3),
nn.Linear(768, 512),
nn.Linear(1536, 1024), # Increased from 512
nn.ReLU(),
nn.Dropout(0.3),
nn.Linear(512, 256),
nn.Linear(1024, 512), # Increased from 256
nn.ReLU(),
nn.Dropout(0.3),
nn.Linear(256, 128),
nn.Linear(512, 256), # Increased from 128
nn.ReLU(),
nn.Linear(128, self.n_actions)
nn.Linear(256, self.n_actions)
)
self.value_stream = nn.Sequential(
nn.Linear(1024, 768),
nn.Linear(1024, 1536), # Increased from 768
nn.ReLU(),
nn.Dropout(0.3),
nn.Linear(768, 512),
nn.Linear(1536, 1024), # Increased from 512
nn.ReLU(),
nn.Dropout(0.3),
nn.Linear(512, 256),
nn.Linear(1024, 512), # Increased from 256
nn.ReLU(),
nn.Dropout(0.3),
nn.Linear(256, 128),
nn.Linear(512, 256), # Increased from 128
nn.ReLU(),
nn.Linear(128, 1)
nn.Linear(256, 1)
)
# ULTRA MASSIVE extrema detection head with deeper ensemble predictions
self.extrema_head = nn.Sequential(
nn.Linear(1024, 768),
nn.Linear(1024, 1536), # Increased from 768
nn.ReLU(),
nn.Dropout(0.3),
nn.Linear(768, 512),
nn.Linear(1536, 1024), # Increased from 512
nn.ReLU(),
nn.Dropout(0.3),
nn.Linear(512, 256),
nn.Linear(1024, 512), # Increased from 256
nn.ReLU(),
nn.Dropout(0.3),
nn.Linear(256, 128),
nn.Linear(512, 256), # Increased from 128
nn.ReLU(),
nn.Linear(128, 3) # 0=bottom, 1=top, 2=neither
nn.Linear(256, 3) # 0=bottom, 1=top, 2=neither
)
# ULTRA MASSIVE multi-timeframe price prediction heads
self.price_pred_immediate = nn.Sequential(
nn.Linear(1024, 512),
nn.Linear(1024, 1024), # Increased from 512
nn.ReLU(),
nn.Dropout(0.3),
nn.Linear(512, 256),
nn.Linear(1024, 512), # Increased from 256
nn.ReLU(),
nn.Dropout(0.3),
nn.Linear(256, 128),
nn.Linear(512, 256), # Increased from 128
nn.ReLU(),
nn.Linear(128, 3) # Up, Down, Sideways
nn.Linear(256, 3) # Up, Down, Sideways
)
self.price_pred_midterm = nn.Sequential(
nn.Linear(1024, 512),
nn.Linear(1024, 1024), # Increased from 512
nn.ReLU(),
nn.Dropout(0.3),
nn.Linear(512, 256),
nn.Linear(1024, 512), # Increased from 256
nn.ReLU(),
nn.Dropout(0.3),
nn.Linear(256, 128),
nn.Linear(512, 256), # Increased from 128
nn.ReLU(),
nn.Linear(128, 3) # Up, Down, Sideways
nn.Linear(256, 3) # Up, Down, Sideways
)
self.price_pred_longterm = nn.Sequential(
nn.Linear(1024, 512),
nn.Linear(1024, 1024), # Increased from 512
nn.ReLU(),
nn.Dropout(0.3),
nn.Linear(512, 256),
nn.Linear(1024, 512), # Increased from 256
nn.ReLU(),
nn.Dropout(0.3),
nn.Linear(256, 128),
nn.Linear(512, 256), # Increased from 128
nn.ReLU(),
nn.Linear(128, 3) # Up, Down, Sideways
nn.Linear(256, 3) # Up, Down, Sideways
)
# ULTRA MASSIVE value prediction with ensemble approaches
self.price_pred_value = nn.Sequential(
nn.Linear(1024, 768),
nn.Linear(1024, 1536), # Increased from 768
nn.ReLU(),
nn.Dropout(0.3),
nn.Linear(768, 512),
nn.Linear(1536, 1024), # Increased from 512
nn.ReLU(),
nn.Dropout(0.3),
nn.Linear(512, 256),
nn.Linear(1024, 256),
nn.ReLU(),
nn.Dropout(0.3),
nn.Linear(256, 128),
@ -391,7 +391,7 @@ class EnhancedCNN(nn.Module):
# Handle 4D input [batch, timeframes, window, features] or 3D input [batch, timeframes, features]
if len(x.shape) == 4:
# Flatten window and features: [batch, timeframes, window*features]
x = x.view(batch_size, x.size(1), -1)
x = x.reshape(batch_size, x.size(1), -1)
if self.conv_layers is not None:
# Now x is 3D: [batch, timeframes, features]
@ -405,10 +405,10 @@ class EnhancedCNN(nn.Module):
# Apply ultra massive convolutions
x_conv = self.conv_layers(x_reshaped)
# Flatten: [batch, channels, 1] -> [batch, channels]
x_flat = x_conv.view(batch_size, -1)
x_flat = x_conv.reshape(batch_size, -1)
else:
# If no conv layers, just flatten
x_flat = x.view(batch_size, -1)
x_flat = x.reshape(batch_size, -1)
else:
# For 2D input [batch, features]
x_flat = x
@ -512,30 +512,30 @@ class EnhancedCNN(nn.Module):
# Log advanced predictions for better decision making
if hasattr(self, '_log_predictions') and self._log_predictions:
# Log volatility prediction
volatility = torch.softmax(advanced_predictions['volatility'], dim=1)
volatility_class = torch.argmax(volatility, dim=1).item()
volatility = torch.softmax(advanced_predictions['volatility'], dim=1).squeeze(0)
volatility_class = int(torch.argmax(volatility).item())
volatility_labels = ['Very Low', 'Low', 'Medium', 'High', 'Very High']
# Log support/resistance prediction
sr = torch.softmax(advanced_predictions['support_resistance'], dim=1)
sr_class = torch.argmax(sr, dim=1).item()
sr = torch.softmax(advanced_predictions['support_resistance'], dim=1).squeeze(0)
sr_class = int(torch.argmax(sr).item())
sr_labels = ['Strong Support', 'Weak Support', 'Neutral', 'Weak Resistance', 'Strong Resistance', 'Breakout']
# Log market regime prediction
regime = torch.softmax(advanced_predictions['market_regime'], dim=1)
regime_class = torch.argmax(regime, dim=1).item()
regime = torch.softmax(advanced_predictions['market_regime'], dim=1).squeeze(0)
regime_class = int(torch.argmax(regime).item())
regime_labels = ['Bull Trend', 'Bear Trend', 'Sideways', 'Volatile Up', 'Volatile Down', 'Accumulation', 'Distribution']
# Log risk assessment
risk = torch.softmax(advanced_predictions['risk_assessment'], dim=1)
risk_class = torch.argmax(risk, dim=1).item()
risk = torch.softmax(advanced_predictions['risk_assessment'], dim=1).squeeze(0)
risk_class = int(torch.argmax(risk).item())
risk_labels = ['Low Risk', 'Medium Risk', 'High Risk', 'Extreme Risk']
logger.info(f"ULTRA MASSIVE Model Predictions:")
logger.info(f" Volatility: {volatility_labels[volatility_class]} ({volatility[0, volatility_class]:.3f})")
logger.info(f" Support/Resistance: {sr_labels[sr_class]} ({sr[0, sr_class]:.3f})")
logger.info(f" Market Regime: {regime_labels[regime_class]} ({regime[0, regime_class]:.3f})")
logger.info(f" Risk Level: {risk_labels[risk_class]} ({risk[0, risk_class]:.3f})")
logger.info(f" Volatility: {volatility_labels[volatility_class]} ({volatility[volatility_class]:.3f})")
logger.info(f" Support/Resistance: {sr_labels[sr_class]} ({sr[sr_class]:.3f})")
logger.info(f" Market Regime: {regime_labels[regime_class]} ({regime[regime_class]:.3f})")
logger.info(f" Risk Level: {risk_labels[risk_class]} ({risk[risk_class]:.3f})")
return action