folder stricture reorganize

This commit is contained in:
Dobromir Popov
2025-06-25 11:42:12 +03:00
parent 61b31a3089
commit 03fa28a12d
127 changed files with 3108 additions and 1774 deletions

34
.vscode/launch.json vendored
View File

@ -110,6 +110,28 @@
"COB_ETH_BUCKET_SIZE": "1" "COB_ETH_BUCKET_SIZE": "1"
}, },
"preLaunchTask": "Kill Stale Processes" "preLaunchTask": "Kill Stale Processes"
},
{
"name": "🧹 Clean Trading Dashboard (Universal Data Stream)",
"type": "python",
"request": "launch",
"program": "run_clean_dashboard.py",
"console": "integratedTerminal",
"justMyCode": false,
"env": {
"PYTHONUNBUFFERED": "1",
"CUDA_VISIBLE_DEVICES": "0",
"ENABLE_UNIVERSAL_DATA_STREAM": "1",
"ENABLE_NN_DECISION_FUSION": "1",
"ENABLE_COB_INTEGRATION": "1",
"DASHBOARD_PORT": "8051"
},
"preLaunchTask": "Kill Stale Processes",
"presentation": {
"hidden": false,
"group": "Universal Data Stream",
"order": 1
}
} }
], ],
@ -180,6 +202,18 @@
"group": "COB Trading", "group": "COB Trading",
"order": 5 "order": 5
} }
},
{
"name": "🧹 Clean Dashboard + Universal Data Stream Monitor",
"configurations": [
"🧹 Clean Trading Dashboard (Universal Data Stream)"
],
"stopAll": true,
"presentation": {
"hidden": false,
"group": "Universal Data Stream",
"order": 2
}
} }
] ]
} }

View File

@ -1,183 +0,0 @@
# Dashboard Performance Optimization Summary
## Problem Identified
The `update_dashboard` function in the main TradingDashboard (`web/dashboard.py`) was extremely slow, causing no data to appear on the web UI. The original function was performing too many blocking operations and heavy computations on every update interval.
## Root Causes
1. **Heavy Data Fetching**: Multiple API calls per update to get 1s and 1m data (300+ data points)
2. **Complex Chart Generation**: Full chart recreation with Williams pivot analysis every update
3. **Expensive Operations**: Signal generation, training metrics, and CNN monitoring every interval
4. **No Caching**: Repeated computation of the same data
5. **Blocking I/O**: Dashboard status updates with long timeouts
6. **Large Data Processing**: Processing hundreds of data points for each chart update
## Optimizations Implemented
### 1. Smart Update Scheduling
- **Price Updates**: Every 1 second (essential data)
- **Chart Updates**: Every 5 seconds (visual updates)
- **Heavy Operations**: Every 10 seconds (complex computations)
- **Cleanup**: Every 60 seconds (memory management)
```python
is_price_update = True # Price updates every interval (1s)
is_chart_update = n_intervals % 5 == 0 # Chart updates every 5s
is_heavy_update = n_intervals % 10 == 0 # Heavy operations every 10s
is_cleanup_update = n_intervals % 60 == 0 # Cleanup every 60s
```
### 2. Intelligent Price Caching
- **WebSocket Priority**: Use real-time WebSocket prices first (fastest)
- **Price Cache**: Cache prices for 30 seconds to avoid redundant API calls
- **Fallback Strategy**: Only hit data provider during heavy updates
```python
# Try WebSocket price first (fastest)
current_price = self.get_realtime_price(symbol)
if current_price:
data_source = "WEBSOCKET"
else:
# Use cached price if available and recent
if hasattr(self, '_last_price_cache'):
cache_time, cached_price = self._last_price_cache
if time.time() - cache_time < 30:
current_price = cached_price
data_source = "PRICE_CACHE"
```
### 3. Chart Optimization
- **Reduced Data**: Only 20 data points instead of 300+
- **Chart Caching**: Cache charts for 20 seconds
- **Simplified Rendering**: Remove heavy Williams pivot analysis from frequent updates
- **Height Reduction**: Smaller chart size for faster rendering
```python
def _create_price_chart_optimized(self, symbol, current_price):
# Use minimal data for chart
df = self.data_provider.get_historical_data(symbol, '1m', limit=20, refresh=False)
# Simple line chart without heavy processing
fig.update_layout(height=300, showlegend=False)
```
### 4. Component Caching System
All heavy UI components are now cached and only updated during heavy update cycles:
- **Training Metrics**: Cached for 10 seconds
- **Decisions List**: Limited to 5 entries, cached
- **Session Performance**: Simplified calculations, cached
- **Closed Trades Table**: Limited to 3 entries, cached
- **CNN Monitoring**: Minimal computation, cached
### 5. Signal Generation Optimization
- **Reduced Frequency**: Only during heavy updates (every 10 seconds)
- **Minimal Data**: Use cached 15-bar data for signal generation
- **Data Caching**: Cache signal data for 30 seconds
### 6. Error Handling & Fallbacks
- **Graceful Degradation**: Return cached states when operations fail
- **Fast Error Recovery**: Don't break the entire dashboard on single component failure
- **Non-Blocking Operations**: All heavy operations have timeouts and fallbacks
## Performance Improvements Achieved
### Before Optimization:
- **Update Time**: 2000-5000ms per update
- **Data Fetching**: 300+ data points per update
- **Chart Generation**: Full recreation every second
- **API Calls**: Multiple blocking calls per update
- **Memory Usage**: Growing continuously due to lack of cleanup
### After Optimization:
- **Update Time**: 10-50ms for light updates, 100-200ms for heavy updates
- **Data Fetching**: 20 data points for charts, cached prices
- **Chart Generation**: Every 5 seconds with cached data
- **API Calls**: Minimal, mostly cached data
- **Memory Usage**: Controlled with regular cleanup
### Performance Metrics:
- **95% reduction** in average update time
- **85% reduction** in data fetching
- **80% reduction** in chart generation overhead
- **90% reduction** in API calls
## Code Structure Changes
### New Helper Methods Added:
1. `_get_empty_dashboard_state()` - Emergency fallback state
2. `_process_signal_optimized()` - Lightweight signal processing
3. `_create_price_chart_optimized()` - Fast chart generation
4. `_create_training_metrics_cached()` - Cached training metrics
5. `_create_decisions_list_cached()` - Cached decisions with limits
6. `_create_session_performance_cached()` - Cached performance data
7. `_create_closed_trades_table_cached()` - Cached trades table
8. `_create_cnn_monitoring_content_cached()` - Cached CNN status
### Caching Variables Added:
- `_last_price_cache` - Price caching with timestamps
- `_cached_signal_data` - Signal generation data cache
- `_cached_chart_data_time` - Chart cache timestamp
- `_cached_price_chart` - Chart object cache
- `_cached_training_metrics` - Training metrics cache
- `_cached_decisions_list` - Decisions list cache
- `_cached_session_perf` - Session performance cache
- `_cached_closed_trades` - Closed trades cache
- `_cached_system_status` - System status cache
- `_cached_cnn_content` - CNN monitoring cache
- `_last_dashboard_state` - Emergency dashboard state cache
## User Experience Improvements
### Immediate Benefits:
- **Fast Loading**: Dashboard loads within 1-2 seconds
- **Responsive Updates**: Price updates every second
- **Smooth Charts**: Chart updates every 5 seconds without blocking
- **No Freezing**: Dashboard never freezes during updates
- **Real-time Feel**: WebSocket prices provide real-time experience
### Data Availability:
- **Always Show Data**: Dashboard shows cached data even during errors
- **Progressive Loading**: Show essential data first, details load progressively
- **Error Resilience**: Single component failures don't break entire dashboard
## Configuration Options
The optimization can be tuned via these intervals:
```python
# Tunable performance parameters
PRICE_UPDATE_INTERVAL = 1 # seconds
CHART_UPDATE_INTERVAL = 5 # seconds
HEAVY_UPDATE_INTERVAL = 10 # seconds
CLEANUP_INTERVAL = 60 # seconds
PRICE_CACHE_DURATION = 30 # seconds
CHART_CACHE_DURATION = 20 # seconds
```
## Monitoring & Debugging
### Performance Logging:
- Logs slow updates (>100ms) as warnings
- Regular performance logs every 30 seconds
- Detailed timing breakdown for heavy operations
### Debug Information:
- Data source indicators ([WEBSOCKET], [PRICE_CACHE], [DATA_PROVIDER])
- Update type tracking (chart, heavy, cleanup flags)
- Cache hit/miss information
## Backward Compatibility
- All original functionality preserved
- Existing API interfaces unchanged
- Configuration parameters respected
- No breaking changes to external integrations
## Results
The optimized dashboard now provides:
- **Sub-second price updates** via WebSocket caching
- **Smooth user experience** with progressive loading
- **Reduced server load** with intelligent caching
- **Improved reliability** with error handling
- **Better resource utilization** with controlled cleanup
The dashboard is now production-ready for high-frequency trading environments and can handle extended operation without performance degradation.

View File

@ -1,377 +0,0 @@
# Enhanced Multi-Modal Trading Architecture Guide
## Overview
This document describes the enhanced multi-modal trading system that implements sophisticated decision-making through coordinated CNN and RL modules. The system is designed to handle multi-timeframe analysis across multiple symbols (ETH, BTC) with continuous learning capabilities.
## Architecture Components
### 1. Enhanced Trading Orchestrator (`core/enhanced_orchestrator.py`)
The heart of the system that coordinates all components:
**Key Features:**
- **Multi-Symbol Coordination**: Makes decisions across ETH and BTC considering correlations
- **Timeframe Integration**: Combines predictions from multiple timeframes (1m, 5m, 15m, 1h, 4h, 1d)
- **Perfect Move Marking**: Identifies and marks optimal trading decisions for CNN training
- **RL Evaluation Loop**: Evaluates trading outcomes to train RL agents
**Data Structures:**
```python
@dataclass
class TimeframePrediction:
timeframe: str
action: str # 'BUY', 'SELL', 'HOLD'
confidence: float # 0.0 to 1.0
probabilities: Dict[str, float]
timestamp: datetime
market_features: Dict[str, float]
@dataclass
class TradingAction:
symbol: str
action: str
quantity: float
confidence: float
price: float
timestamp: datetime
reasoning: Dict[str, Any]
timeframe_analysis: List[TimeframePrediction]
```
**Decision Making Process:**
1. Gather market states for all symbols and timeframes
2. Get CNN predictions for each timeframe with confidence scores
3. Combine timeframe predictions using weighted averaging
4. Consider symbol correlations (ETH-BTC correlation ~0.85)
5. Apply confidence thresholds and risk management
6. Generate coordinated trading decisions
7. Queue actions for RL evaluation
### 2. Enhanced CNN Trainer (`training/enhanced_cnn_trainer.py`)
Implements supervised learning on marked perfect moves:
**Key Features:**
- **Perfect Move Dataset**: Trains on historically optimal decisions
- **Timeframe-Specific Heads**: Separate prediction heads for each timeframe
- **Confidence Prediction**: Predicts both action and confidence simultaneously
- **Multi-Loss Training**: Combines action classification and confidence regression
**Network Architecture:**
```python
# Convolutional feature extraction
Conv1D(features=5, filters=64, kernel=3) -> BatchNorm -> ReLU -> Dropout
Conv1D(filters=128, kernel=3) -> BatchNorm -> ReLU -> Dropout
Conv1D(filters=256, kernel=3) -> BatchNorm -> ReLU -> Dropout
AdaptiveAvgPool1d(1) # Global average pooling
# Timeframe-specific heads
for each timeframe:
Linear(256 -> 128) -> ReLU -> Dropout
Linear(128 -> 64) -> ReLU -> Dropout
# Action prediction
Linear(64 -> 3) # BUY, HOLD, SELL
# Confidence prediction
Linear(64 -> 32) -> ReLU -> Linear(32 -> 1) -> Sigmoid
```
**Training Process:**
1. Collect perfect moves from orchestrator with known outcomes
2. Create dataset with features, optimal actions, and target confidence
3. Train with combined loss: `action_loss + 0.5 * confidence_loss`
4. Use early stopping and model checkpointing
5. Generate comprehensive training reports and visualizations
### 3. Enhanced RL Trainer (`training/enhanced_rl_trainer.py`)
Implements continuous learning from trading evaluations:
**Key Features:**
- **Prioritized Experience Replay**: Learns from important experiences first
- **Market Regime Adaptation**: Adjusts confidence based on market conditions
- **Multi-Symbol Agents**: Separate RL agents for each trading symbol
- **Double DQN Architecture**: Reduces overestimation bias
**Agent Architecture:**
```python
# Main Network
Linear(state_size -> 256) -> ReLU -> Dropout
Linear(256 -> 256) -> ReLU -> Dropout
Linear(256 -> 128) -> ReLU -> Dropout
# Dueling heads
value_head = Linear(128 -> 1)
advantage_head = Linear(128 -> action_space)
# Q-values = V(s) + A(s,a) - mean(A(s,a))
```
**Learning Process:**
1. Store trading experiences with TD-error priorities
2. Sample batches using prioritized replay
3. Train with Double DQN to reduce overestimation
4. Update target networks periodically
5. Adapt exploration (epsilon) based on market regime stability
### 4. Market State and Feature Engineering
**Market State Components:**
```python
@dataclass
class MarketState:
symbol: str
timestamp: datetime
prices: Dict[str, float] # {timeframe: price}
features: Dict[str, np.ndarray] # {timeframe: feature_matrix}
volatility: float
volume: float
trend_strength: float
market_regime: str # 'trending', 'ranging', 'volatile'
```
**Feature Engineering:**
- **OHLCV Data**: Open, High, Low, Close, Volume for each timeframe
- **Technical Indicators**: RSI, MACD, Bollinger Bands, etc.
- **Market Regime Detection**: Automatic classification of market conditions
- **Volatility Analysis**: Real-time volatility calculations
- **Volume Analysis**: Volume ratio compared to historical averages
## System Workflow
### 1. Initialization Phase
```python
# Load configuration
config = get_config('config.yaml')
# Initialize components
data_provider = DataProvider(config)
orchestrator = EnhancedTradingOrchestrator(data_provider)
cnn_trainer = EnhancedCNNTrainer(config, orchestrator)
rl_trainer = EnhancedRLTrainer(config, orchestrator)
# Load existing models or create new ones
models = initialize_models(load_existing=True)
register_models_with_orchestrator(models)
```
### 2. Trading Loop
```python
while running:
# 1. Gather market data for all symbols and timeframes
market_states = await get_all_market_states()
# 2. Generate CNN predictions for each timeframe
for symbol in symbols:
for timeframe in timeframes:
prediction = cnn_model.predict_timeframe(features, timeframe)
# 3. Combine timeframe predictions with weights
combined_prediction = combine_timeframe_predictions(predictions)
# 4. Consider symbol correlations
coordinated_decision = coordinate_symbols(predictions, correlations)
# 5. Apply confidence thresholds and risk management
final_decision = apply_risk_management(coordinated_decision)
# 6. Execute trades (or log decisions)
execute_trading_decision(final_decision)
# 7. Queue for RL evaluation
queue_for_rl_evaluation(final_decision, market_state)
```
### 3. Continuous Learning Loop
```python
# RL Learning (every hour)
async def rl_learning_loop():
while running:
# Evaluate past trading actions
await evaluate_trading_outcomes()
# Train RL agents on new experiences
for symbol, agent in rl_agents.items():
agent.replay() # Learn from prioritized experiences
# Adapt to market regime changes
adapt_to_market_conditions()
await asyncio.sleep(3600) # Wait 1 hour
# CNN Learning (every 6 hours)
async def cnn_learning_loop():
while running:
# Check for sufficient perfect moves
perfect_moves = get_perfect_moves_for_training()
if len(perfect_moves) >= 200:
# Train CNN on perfect moves
training_report = train_cnn_on_perfect_moves(perfect_moves)
# Update registered model
update_model_registry(trained_model)
await asyncio.sleep(6 * 3600) # Wait 6 hours
```
## Key Algorithms
### 1. Timeframe Prediction Combination
```python
def combine_timeframe_predictions(timeframe_predictions, symbol):
action_scores = {'BUY': 0.0, 'SELL': 0.0, 'HOLD': 0.0}
total_weight = 0.0
timeframe_weights = {
'1m': 0.05, '5m': 0.10, '15m': 0.15,
'1h': 0.25, '4h': 0.25, '1d': 0.20
}
for pred in timeframe_predictions:
weight = timeframe_weights[pred.timeframe] * pred.confidence
action_scores[pred.action] += weight
total_weight += weight
# Normalize and select best action
best_action = max(action_scores, key=action_scores.get)
confidence = action_scores[best_action] / total_weight
return best_action, confidence
```
### 2. Perfect Move Marking
```python
def mark_perfect_move(action, initial_state, final_state, reward):
# Determine optimal action based on outcome
if reward > 0.02: # Significant positive outcome
optimal_action = action.action # Action was correct
optimal_confidence = min(0.95, abs(reward) * 10)
elif reward < -0.02: # Significant negative outcome
optimal_action = opposite_action(action.action) # Should have done opposite
optimal_confidence = min(0.95, abs(reward) * 10)
else: # Neutral outcome
optimal_action = 'HOLD' # Should have held
optimal_confidence = 0.3
# Create perfect move for CNN training
perfect_move = PerfectMove(
symbol=action.symbol,
timeframe=timeframe,
timestamp=action.timestamp,
optimal_action=optimal_action,
confidence_should_have_been=optimal_confidence,
market_state_before=initial_state,
market_state_after=final_state,
actual_outcome=reward
)
return perfect_move
```
### 3. RL Reward Calculation
```python
def calculate_reward(action, price_change, confidence):
base_reward = 0.0
# Reward based on action correctness
if action == 'BUY' and price_change > 0:
base_reward = price_change * 10 # Reward proportional to gain
elif action == 'SELL' and price_change < 0:
base_reward = abs(price_change) * 10 # Reward for avoiding loss
elif action == 'HOLD':
if abs(price_change) < 0.005: # Correct hold
base_reward = 0.01
else: # Missed opportunity
base_reward = -0.01
else:
base_reward = -abs(price_change) * 5 # Penalty for wrong actions
# Scale by confidence
confidence_multiplier = 0.5 + confidence # 0.5 to 1.5 range
return base_reward * confidence_multiplier
```
## Configuration and Deployment
### 1. Running the System
```bash
# Basic trading mode
python enhanced_trading_main.py --mode trade
# Training only mode
python enhanced_trading_main.py --mode train
# Fresh start without loading existing models
python enhanced_trading_main.py --mode trade --no-load-models
# Custom configuration
python enhanced_trading_main.py --config custom_config.yaml
```
### 2. Key Configuration Parameters
```yaml
# Enhanced Orchestrator Settings
orchestrator:
confidence_threshold: 0.6 # Higher threshold for enhanced system
decision_frequency: 30 # Faster decisions (30 seconds)
# CNN Configuration
cnn:
timeframes: ["1m", "5m", "15m", "1h", "4h", "1d"]
confidence_threshold: 0.6
model_dir: "models/enhanced_cnn"
# RL Configuration
rl:
hidden_size: 256
buffer_size: 10000
model_dir: "models/enhanced_rl"
market_regime_weights:
trending: 1.2
ranging: 0.8
volatile: 0.6
```
### 3. Memory Management
The system is designed to work within 8GB memory constraints:
- Total system limit: 8GB
- Per-model limit: 2GB
- Automatic memory cleanup every 30 minutes
- GPU memory management with dynamic allocation
### 4. Monitoring and Logging
- Comprehensive logging with component-specific levels
- TensorBoard integration for training visualization
- Performance metrics tracking
- Memory usage monitoring
- Real-time decision logging with full reasoning
## Performance Characteristics
### Expected Behavior:
1. **Decision Frequency**: 30-second intervals between decisions
2. **CNN Training**: Every 6 hours when sufficient perfect moves available
3. **RL Training**: Continuous learning every hour
4. **Memory Usage**: <8GB total system usage
5. **Confidence Thresholds**: 0.6+ for trading actions
### Key Metrics:
- **Decision Accuracy**: Tracked via RL reward system
- **Confidence Calibration**: CNN confidence vs actual outcomes
- **Symbol Correlation**: ETH-BTC coordination effectiveness
- **Training Progress**: Loss curves and validation accuracy
- **Market Adaptation**: Performance across different regimes
## Future Enhancements
1. **Additional Symbols**: Easy extension to support more trading pairs
2. **Advanced Features**: Sentiment analysis, news integration
3. **Risk Management**: Portfolio-level risk optimization
4. **Backtesting**: Historical performance evaluation
5. **Live Trading**: Real exchange integration
6. **Model Ensembles**: Multiple CNN/RL model combinations
This architecture provides a robust foundation for sophisticated algorithmic trading with continuous learning and adaptation capabilities.

View File

@ -1,116 +0,0 @@
# Enhanced Dashboard Summary
## Dashboard Improvements Completed
### Removed Less Important Information
-**Timezone Information Removed**: Removed "Sofia Time Zone" references to focus on more critical data
-**Streamlined Header**: Updated to show "Neural DPS Active" instead of timezone details
### Added Model Training Information
#### 1. Model Training Progress Section
- **RL Training Metrics**:
- Queue Size: Shows current RL evaluation queue size
- Win Rate: Real-time win rate percentage
- Total Actions: Number of actions processed
- **CNN Training Metrics**:
- Perfect Moves: Count of detected perfect trading opportunities
- Confidence Threshold: Current confidence threshold setting
- Decision Frequency: How often decisions are made
#### 2. Orchestrator Data Flow Section
- **Data Input Status**:
- Symbols: Active trading symbols being processed
- Streaming Status: Real-time data streaming indicator
- Subscribers: Number of feature subscribers
- **Processing Status**:
- Tick Counts: Real-time tick processing counts per symbol
- Buffer Sizes: Current buffer utilization
- Neural DPS Status: Neural Data Processing System activity
#### 3. RL & CNN Training Events Log
- **Real-time Training Events**:
- 🧠 CNN Events: Perfect move detections with confidence scores
- 🤖 RL Events: Experience replay completions and learning updates
- ⚡ Tick Events: High-confidence tick feature processing
- **Event Information**:
- Timestamp for each event
- Event type (CNN/RL/TICK)
- Confidence scores
- Detailed event descriptions
### Technical Implementation
#### New Dashboard Methods Added:
1. `_create_model_training_status()`: Displays RL and CNN training progress
2. `_create_orchestrator_status()`: Shows data flow and processing status
3. `_create_training_events_log()`: Real-time training events feed
#### Dashboard Layout Updates:
- Added model training and orchestrator status sections
- Integrated training events log above trading actions
- Updated callback to include new data outputs
- Enhanced error handling for new components
### Integration with Existing Systems
#### Orchestrator Integration:
- Pulls metrics from `orchestrator.get_performance_metrics()`
- Accesses tick processor stats via `orchestrator.tick_processor.get_processing_stats()`
- Displays perfect moves from `orchestrator.perfect_moves`
#### Real-time Updates:
- All new sections update every 1 second with the main dashboard callback
- Graceful fallback when orchestrator data is not available
- Error handling for missing or incomplete data
### Dashboard Information Hierarchy
#### Priority 1 - Critical Trading Data:
- Session P&L and balance
- Live prices (ETH/USDT, BTC/USDT)
- Trading actions and positions
#### Priority 2 - Model Performance:
- RL training progress and metrics
- CNN training events and perfect moves
- Neural DPS processing status
#### Priority 3 - Technical Status:
- Orchestrator data flow
- Buffer utilization
- System health indicators
#### Priority 4 - Debug Information:
- Server callback status
- Chart data availability
- Error messages
### Benefits of Enhanced Dashboard
1. **Model Monitoring**: Real-time visibility into RL and CNN training progress
2. **Data Flow Tracking**: Clear view of orchestrator input/output processing
3. **Training Events**: Live feed of learning events and perfect move detections
4. **Performance Metrics**: Continuous monitoring of model performance indicators
5. **System Health**: Real-time status of Neural DPS and data processing
### Next Steps for Further Enhancement
1. **Add Model Loss Tracking**: Display training loss curves for RL and CNN
2. **Feature Importance**: Show which features are most influential in decisions
3. **Prediction Accuracy**: Track prediction accuracy over time
4. **Resource Utilization**: Monitor GPU/CPU usage during training
5. **Model Comparison**: Compare performance between different model versions
## Usage
The enhanced dashboard now provides comprehensive monitoring of:
- Model training progress and events
- Orchestrator data processing flow
- Real-time learning activities
- System performance metrics
All information updates in real-time and provides critical insights for monitoring the trading system's learning and decision-making processes.

View File

@ -183,6 +183,46 @@
"total_parameters": null, "total_parameters": null,
"wandb_run_id": null, "wandb_run_id": null,
"wandb_artifact_name": null "wandb_artifact_name": null
},
{
"checkpoint_id": "extrema_trainer_20250625_105812",
"model_name": "extrema_trainer",
"model_type": "extrema_trainer",
"file_path": "NN\\models\\saved\\extrema_trainer\\extrema_trainer_20250625_105812.pt",
"created_at": "2025-06-25T10:58:12.424290",
"file_size_mb": 0.0013427734375,
"performance_score": 0.1,
"accuracy": 0.0,
"loss": null,
"val_accuracy": null,
"val_loss": null,
"reward": null,
"pnl": null,
"epoch": null,
"training_time_hours": null,
"total_parameters": null,
"wandb_run_id": null,
"wandb_artifact_name": null
},
{
"checkpoint_id": "extrema_trainer_20250625_110836",
"model_name": "extrema_trainer",
"model_type": "extrema_trainer",
"file_path": "NN\\models\\saved\\extrema_trainer\\extrema_trainer_20250625_110836.pt",
"created_at": "2025-06-25T11:08:36.772996",
"file_size_mb": 0.0013427734375,
"performance_score": 0.1,
"accuracy": 0.0,
"loss": null,
"val_accuracy": null,
"val_loss": null,
"reward": null,
"pnl": null,
"epoch": null,
"training_time_hours": null,
"total_parameters": null,
"wandb_run_id": null,
"wandb_artifact_name": null
} }
] ]
} }

View File

@ -1,173 +0,0 @@
# Strict Position Management & UI Cleanup Update
## Overview
Updated the trading system to implement strict position management rules and cleaned up the dashboard visualization as requested.
## UI Changes
### 1. **Removed Losing Trade Triangles**
- **Removed**: Losing entry/exit triangle markers from the dashboard
- **Kept**: Only dashed lines for trade visualization
- **Benefit**: Cleaner, less cluttered interface focused on essential information
### Dashboard Visualization Now Shows:
- ✅ Profitable trade triangles (filled)
- ✅ Dashed lines for all trades
- ❌ Losing trade triangles (removed)
## Position Management Changes
### 2. **Strict Position Rules**
#### Previous Behavior:
- Consecutive signals could create complex position transitions
- Multiple position states possible
- Less predictable position management
#### New Strict Behavior:
**FLAT Position:**
- `BUY` signal → Enter LONG position
- `SELL` signal → Enter SHORT position
**LONG Position:**
- `BUY` signal → **IGNORED** (already long)
- `SELL` signal → **IMMEDIATE CLOSE** (and enter SHORT if no conflicts)
**SHORT Position:**
- `SELL` signal → **IGNORED** (already short)
- `BUY` signal → **IMMEDIATE CLOSE** (and enter LONG if no conflicts)
### 3. **Safety Features**
#### Conflict Resolution:
- **Multiple opposite positions**: Close ALL immediately
- **Conflicting signals**: Prioritize closing existing positions
- **Position limits**: Maximum 1 position per symbol
#### Immediate Actions:
- Close opposite positions on first opposing signal
- No waiting for consecutive signals
- Clear position state at all times
## Technical Implementation
### Enhanced Orchestrator Updates:
```python
def _make_2_action_decision():
"""STRICT Logic Implementation"""
if position_side == 'FLAT':
# Any signal is entry
is_entry = True
elif position_side == 'LONG' and raw_action == 'SELL':
# IMMEDIATE EXIT
is_exit = True
elif position_side == 'SHORT' and raw_action == 'BUY':
# IMMEDIATE EXIT
is_exit = True
else:
# IGNORE same-direction signals
return None
```
### Position Tracking:
```python
def _update_2_action_position():
"""Strict position management"""
# Close opposite positions immediately
# Only open new positions when flat
# Safety checks for conflicts
```
### Safety Methods:
```python
def _close_conflicting_positions():
"""Close any conflicting positions"""
def close_all_positions():
"""Emergency close all positions"""
```
## Benefits
### 1. **Simplicity**
- Clear, predictable position logic
- Easy to understand and debug
- Reduced complexity in decision making
### 2. **Risk Management**
- Immediate opposite closures
- No accumulation of conflicting positions
- Clear position limits
### 3. **Performance**
- Faster decision execution
- Reduced computational overhead
- Better position tracking
### 4. **UI Clarity**
- Cleaner visualization
- Focus on essential information
- Less visual noise
## Performance Metrics Update
Updated performance tracking to reflect strict mode:
```yaml
system_type: 'strict-2-action'
position_mode: 'STRICT'
safety_features:
immediate_opposite_closure: true
conflict_detection: true
position_limits: '1 per symbol'
multi_position_protection: true
ui_improvements:
losing_triangles_removed: true
dashed_lines_only: true
cleaner_visualization: true
```
## Testing
### System Test Results:
- ✅ Core components initialized successfully
- ✅ Enhanced orchestrator with strict mode enabled
- ✅ 2-Action system: BUY/SELL only (no HOLD)
- ✅ Position tracking with strict rules
- ✅ Safety features enabled
### Dashboard Status:
- ✅ Losing triangles removed
- ✅ Dashed lines preserved
- ✅ Cleaner visualization active
- ✅ Strict position management integrated
## Usage
### Starting the System:
```bash
# Test strict position management
python main_clean.py --mode test
# Run with strict rules and clean UI
python main_clean.py --mode web --port 8051
```
### Key Features:
- **Immediate Execution**: Opposite signals close positions immediately
- **Clean UI**: Only essential visual elements
- **Position Safety**: Maximum 1 position per symbol
- **Conflict Resolution**: Automatic conflict detection and resolution
## Summary
The system now operates with:
1. **Strict position management** - immediate opposite closures, single positions only
2. **Clean visualization** - removed losing triangles, kept dashed lines
3. **Enhanced safety** - conflict detection and automatic resolution
4. **Simplified logic** - clear, predictable position transitions
This provides a more robust, predictable, and visually clean trading system focused on essential functionality.

View File

@ -1 +0,0 @@

View File

@ -81,7 +81,8 @@ orchestrator:
# Model weights for decision combination # Model weights for decision combination
cnn_weight: 0.7 # Weight for CNN predictions cnn_weight: 0.7 # Weight for CNN predictions
rl_weight: 0.3 # Weight for RL decisions rl_weight: 0.3 # Weight for RL decisions
confidence_threshold: 0.6 # Increased for enhanced system confidence_threshold: 0.20 # Lowered from 0.35 for low-volatility markets
confidence_threshold_close: 0.10 # Lowered from 0.15 for easier exits
decision_frequency: 30 # Seconds between decisions (faster) decision_frequency: 30 # Seconds between decisions (faster)
# Multi-symbol coordination # Multi-symbol coordination

View File

@ -190,6 +190,12 @@ class DataProvider:
logger.info("Centralized data distribution enabled") logger.info("Centralized data distribution enabled")
logger.info("Pivot-based normalization system enabled") logger.info("Pivot-based normalization system enabled")
# Rate limiting
self.last_request_time = {}
self.request_interval = 0.2 # 200ms between requests
self.retry_delay = 60 # 1 minute retry delay for 451 errors
self.max_retries = 3
def _ensure_datetime_index(self, df: pd.DataFrame) -> pd.DataFrame: def _ensure_datetime_index(self, df: pd.DataFrame) -> pd.DataFrame:
"""Ensure dataframe has proper datetime index""" """Ensure dataframe has proper datetime index"""
if df is None or df.empty: if df is None or df.empty:
@ -2467,3 +2473,45 @@ class DataProvider:
return self.generate_synthetic_bom_features(symbol)[70:] # Last 50 features return self.generate_synthetic_bom_features(symbol)[70:] # Last 50 features
except: except:
return [0.0] * 50 return [0.0] * 50
def _handle_rate_limit(self, url: str):
"""Handle rate limiting with exponential backoff"""
current_time = time.time()
# Check if we need to wait
if url in self.last_request_time:
time_since_last = current_time - self.last_request_time[url]
if time_since_last < self.request_interval:
sleep_time = self.request_interval - time_since_last
logger.info(f"Rate limiting: sleeping {sleep_time:.2f}s")
time.sleep(sleep_time)
self.last_request_time[url] = time.time()
def _make_request_with_retry(self, url: str, params: dict = None):
"""Make HTTP request with retry logic for 451 errors"""
for attempt in range(self.max_retries):
try:
self._handle_rate_limit(url)
response = requests.get(url, params=params, timeout=30)
if response.status_code == 451:
logger.warning(f"Rate limit hit (451), attempt {attempt + 1}/{self.max_retries}")
if attempt < self.max_retries - 1:
sleep_time = self.retry_delay * (2 ** attempt) # Exponential backoff
logger.info(f"Waiting {sleep_time}s before retry...")
time.sleep(sleep_time)
continue
else:
logger.error("Max retries reached, using cached data")
return None
response.raise_for_status()
return response
except Exception as e:
logger.error(f"Request failed (attempt {attempt + 1}): {e}")
if attempt < self.max_retries - 1:
time.sleep(5 * (attempt + 1))
return None

File diff suppressed because it is too large Load Diff

277
core/nn_decision_fusion.py Normal file
View File

@ -0,0 +1,277 @@
#!/usr/bin/env python3
"""
Neural Network Decision Fusion System
Central NN that merges all model outputs + market data for final trading decisions
"""
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
from typing import Dict, List, Optional, Any
from dataclasses import dataclass
from datetime import datetime
import logging
logger = logging.getLogger(__name__)
@dataclass
class ModelPrediction:
"""Standardized prediction from any model"""
model_name: str
prediction_type: str # 'price', 'direction', 'action'
value: float # -1 to 1 for direction, actual price for price predictions
confidence: float # 0 to 1
timestamp: datetime
metadata: Optional[Dict[str, Any]] = None
@dataclass
class MarketContext:
"""Current market context for decision fusion"""
symbol: str
current_price: float
price_change_1m: float
price_change_5m: float
volume_ratio: float
volatility: float
timestamp: datetime
@dataclass
class FusionDecision:
"""Final trading decision from fusion NN"""
action: str # 'BUY', 'SELL', 'HOLD'
confidence: float # 0 to 1
expected_return: float # Expected return percentage
risk_score: float # 0 to 1, higher = riskier
position_size: float # Recommended position size
reasoning: str # Human-readable explanation
model_contributions: Dict[str, float] # How much each model contributed
timestamp: datetime
class DecisionFusionNetwork(nn.Module):
"""Small NN that fuses model predictions with market context"""
def __init__(self, input_dim: int = 32, hidden_dim: int = 64):
super().__init__()
self.fusion_layers = nn.Sequential(
nn.Linear(input_dim, hidden_dim),
nn.ReLU(),
nn.Dropout(0.2),
nn.Linear(hidden_dim, hidden_dim // 2),
nn.ReLU(),
nn.Linear(hidden_dim // 2, 16)
)
# Output heads
self.action_head = nn.Linear(16, 3) # BUY, SELL, HOLD
self.confidence_head = nn.Linear(16, 1)
self.return_head = nn.Linear(16, 1)
def forward(self, features: torch.Tensor) -> Dict[str, torch.Tensor]:
"""Forward pass through fusion network"""
fusion_output = self.fusion_layers(features)
action_logits = self.action_head(fusion_output)
action_probs = F.softmax(action_logits, dim=1)
confidence = torch.sigmoid(self.confidence_head(fusion_output))
expected_return = torch.tanh(self.return_head(fusion_output))
return {
'action_probs': action_probs,
'confidence': confidence.squeeze(),
'expected_return': expected_return.squeeze()
}
class NeuralDecisionFusion:
"""Main NN-based decision fusion system"""
def __init__(self, training_mode: bool = True):
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
self.network = DecisionFusionNetwork().to(self.device)
self.training_mode = training_mode
self.registered_models = {}
self.last_predictions = {}
logger.info(f"🧠 Neural Decision Fusion initialized on {self.device}")
def register_model(self, model_name: str, model_type: str, prediction_format: str):
"""Register a model that will provide predictions"""
self.registered_models[model_name] = {
'type': model_type,
'format': prediction_format,
'prediction_count': 0
}
logger.info(f"Registered NN model: {model_name} ({model_type})")
def add_prediction(self, prediction: ModelPrediction):
"""Add a prediction from a registered model"""
self.last_predictions[prediction.model_name] = prediction
if prediction.model_name in self.registered_models:
self.registered_models[prediction.model_name]['prediction_count'] += 1
logger.debug(f"🔮 {prediction.model_name}: {prediction.value:.3f} "
f"(confidence: {prediction.confidence:.3f})")
def make_decision(self, symbol: str, market_context: MarketContext,
min_confidence: float = 0.25) -> Optional[FusionDecision]:
"""Make NN-driven trading decision"""
try:
if len(self.last_predictions) < 1:
logger.debug("No NN predictions available")
return None
# Prepare features
features = self._prepare_features(market_context)
if features is None:
return None
# Run NN inference
with torch.no_grad():
self.network.eval()
features_tensor = torch.tensor(features, dtype=torch.float32).unsqueeze(0).to(self.device)
outputs = self.network(features_tensor)
action_probs = outputs['action_probs'][0].cpu().numpy()
confidence = outputs['confidence'].cpu().item()
expected_return = outputs['expected_return'].cpu().item()
# Determine action
action_idx = np.argmax(action_probs)
actions = ['BUY', 'SELL', 'HOLD']
action = actions[action_idx]
# Check confidence threshold
if confidence < min_confidence:
action = 'HOLD'
logger.debug(f"Low NN confidence ({confidence:.3f}), defaulting to HOLD")
# Calculate position size
position_size = self._calculate_position_size(confidence, expected_return)
# Generate reasoning
reasoning = self._generate_reasoning(action, confidence, expected_return, action_probs)
# Calculate risk score and model contributions
risk_score = min(1.0, abs(expected_return) * 5 + (1 - confidence) * 0.5)
model_contributions = self._calculate_model_contributions()
decision = FusionDecision(
action=action,
confidence=confidence,
expected_return=expected_return,
risk_score=risk_score,
position_size=position_size,
reasoning=reasoning,
model_contributions=model_contributions,
timestamp=datetime.now()
)
logger.info(f"🧠 NN DECISION: {action} (conf: {confidence:.3f}, "
f"return: {expected_return:.3f}, size: {position_size:.4f})")
return decision
except Exception as e:
logger.error(f"Error in NN decision making: {e}")
return None
def _prepare_features(self, context: MarketContext) -> Optional[np.ndarray]:
"""Prepare feature vector for NN"""
try:
features = np.zeros(32)
# Model predictions (slots 0-15)
idx = 0
for model_name, prediction in self.last_predictions.items():
if idx < 14: # Leave room for other features
features[idx] = prediction.value
features[idx + 1] = prediction.confidence
idx += 2
# Market context (slots 16-31)
features[16] = np.tanh(context.price_change_1m * 100) # 1m change
features[17] = np.tanh(context.price_change_5m * 100) # 5m change
features[18] = np.tanh(context.volume_ratio - 1) # Volume ratio
features[19] = np.tanh(context.volatility * 100) # Volatility
features[20] = context.current_price / 10000.0 # Normalized price
# Time features
now = context.timestamp
features[21] = now.hour / 24.0
features[22] = now.weekday() / 7.0
# Model agreement features
if len(self.last_predictions) >= 2:
values = [p.value for p in self.last_predictions.values()]
features[23] = np.mean(values) # Average prediction
features[24] = np.std(values) # Prediction variance
features[25] = len(self.last_predictions) # Model count
return features
except Exception as e:
logger.error(f"Error preparing NN features: {e}")
return None
def _calculate_position_size(self, confidence: float, expected_return: float) -> float:
"""Calculate position size based on NN outputs"""
base_size = 0.01 # 0.01 ETH base
# Scale by confidence
confidence_multiplier = max(0.1, min(2.0, confidence * 1.5))
# Scale by expected return
return_multiplier = 1.0 + abs(expected_return) * 0.5
final_size = base_size * confidence_multiplier * return_multiplier
return max(0.001, min(0.05, final_size))
def _generate_reasoning(self, action: str, confidence: float,
expected_return: float, action_probs: np.ndarray) -> str:
"""Generate human-readable reasoning"""
reasons = []
if action == 'BUY':
reasons.append(f"NN suggests BUY ({action_probs[0]:.1%})")
elif action == 'SELL':
reasons.append(f"NN suggests SELL ({action_probs[1]:.1%})")
else:
reasons.append(f"NN suggests HOLD")
if confidence > 0.7:
reasons.append("High confidence")
elif confidence > 0.5:
reasons.append("Moderate confidence")
else:
reasons.append("Low confidence")
if abs(expected_return) > 0.01:
direction = "positive" if expected_return > 0 else "negative"
reasons.append(f"Expected {direction} return: {expected_return:.2%}")
reasons.append(f"Based on {len(self.last_predictions)} NN models")
return " | ".join(reasons)
def _calculate_model_contributions(self) -> Dict[str, float]:
"""Calculate how much each model contributed to the decision"""
contributions = {}
total_confidence = sum(p.confidence for p in self.last_predictions.values()) if self.last_predictions else 1.0
if total_confidence > 0:
for model_name, prediction in self.last_predictions.items():
contributions[model_name] = prediction.confidence / total_confidence
return contributions
def get_status(self) -> Dict[str, Any]:
"""Get NN fusion system status"""
return {
'device': str(self.device),
'training_mode': self.training_mode,
'registered_models': len(self.registered_models),
'recent_predictions': len(self.last_predictions),
'model_parameters': sum(p.numel() for p in self.network.parameters())
}

18
debug/README.md Normal file
View File

@ -0,0 +1,18 @@
# Debug Files
This folder contains debug scripts and utilities for troubleshooting various components of the trading system.
## Contents
- `debug_callback_simple.py` - Simple callback debugging
- `debug_dashboard.py` - Dashboard debugging utilities
- `debug_dashboard_500.py` - Dashboard 500 error debugging
- `debug_dashboard_issue.py` - Dashboard issue debugging
- `debug_mexc_auth.py` - MEXC authentication debugging
- `debug_orchestrator_methods.py` - Orchestrator method debugging
- `debug_simple_callback.py` - Simple callback testing
- `debug_trading_activity.py` - Trading activity debugging
## Usage
These files are used for debugging specific issues and should not be run in production. They contain diagnostic code and temporary fixes for troubleshooting purposes.

View File

@ -0,0 +1,186 @@
#!/usr/bin/env python3
"""
Trading Activity Diagnostic Script
Debug why no trades are happening after 6 hours
"""
import logging
import asyncio
from datetime import datetime, timedelta
import pandas as pd
import numpy as np
# Setup logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
async def diagnose_trading_system():
"""Comprehensive diagnosis of trading system"""
logger.info("=== TRADING SYSTEM DIAGNOSTIC ===")
try:
# Import core components
from core.config import get_config
from core.data_provider import DataProvider
from core.enhanced_orchestrator import EnhancedTradingOrchestrator
# Initialize components
config = get_config()
data_provider = DataProvider()
orchestrator = EnhancedTradingOrchestrator(
data_provider=data_provider,
symbols=['ETH/USDT', 'BTC/USDT'],
enhanced_rl_training=True
)
logger.info("✅ Components initialized successfully")
# 1. Check data availability
logger.info("\n=== DATA AVAILABILITY CHECK ===")
for symbol in ['ETH/USDT', 'BTC/USDT']:
for timeframe in ['1m', '5m', '1h']:
try:
data = data_provider.get_historical_data(symbol, timeframe, limit=10)
if data is not None and not data.empty:
logger.info(f"{symbol} {timeframe}: {len(data)} bars available")
logger.info(f" Last price: ${data['close'].iloc[-1]:.2f}")
else:
logger.error(f"{symbol} {timeframe}: NO DATA")
except Exception as e:
logger.error(f"{symbol} {timeframe}: ERROR - {e}")
# 2. Check model status
logger.info("\n=== MODEL STATUS CHECK ===")
model_status = orchestrator.get_loaded_models_status() if hasattr(orchestrator, 'get_loaded_models_status') else {}
logger.info(f"Loaded models: {model_status}")
# 3. Check confidence thresholds
logger.info("\n=== CONFIDENCE THRESHOLD CHECK ===")
logger.info(f"Entry threshold: {getattr(orchestrator, 'confidence_threshold_open', 'UNKNOWN')}")
logger.info(f"Exit threshold: {getattr(orchestrator, 'confidence_threshold_close', 'UNKNOWN')}")
logger.info(f"Config threshold: {config.orchestrator.get('confidence_threshold', 'UNKNOWN')}")
# 4. Test decision making
logger.info("\n=== DECISION MAKING TEST ===")
try:
decisions = await orchestrator.make_coordinated_decisions()
logger.info(f"Generated {len(decisions)} decisions")
for symbol, decision in decisions.items():
if decision:
logger.info(f"{symbol}: {decision.action} "
f"(confidence: {decision.confidence:.3f}, "
f"price: ${decision.price:.2f})")
else:
logger.warning(f"{symbol}: No decision generated")
except Exception as e:
logger.error(f"❌ Decision making failed: {e}")
# 5. Test cold start predictions
logger.info("\n=== COLD START PREDICTIONS TEST ===")
try:
await orchestrator.ensure_predictions_available()
logger.info("✅ Cold start predictions system working")
except Exception as e:
logger.error(f"❌ Cold start predictions failed: {e}")
# 6. Check cross-asset signals
logger.info("\n=== CROSS-ASSET SIGNALS TEST ===")
try:
from core.unified_data_stream import UniversalDataStream
# Create mock universal stream for testing
mock_stream = type('MockStream', (), {})()
mock_stream.get_latest_data = lambda symbol: {'price': 2500.0 if 'ETH' in symbol else 35000.0}
mock_stream.get_market_structure = lambda symbol: {'trend': 'NEUTRAL', 'strength': 0.5}
mock_stream.get_cob_data = lambda symbol: {'imbalance': 0.0, 'depth': 'BALANCED'}
btc_analysis = await orchestrator._analyze_btc_price_action(mock_stream)
logger.info(f"BTC analysis result: {btc_analysis}")
eth_decision = await orchestrator._make_eth_decision_from_btc_signals(
{'signal': 'NEUTRAL', 'strength': 0.5},
{'signal': 'NEUTRAL', 'imbalance': 0.0}
)
logger.info(f"ETH decision result: {eth_decision}")
except Exception as e:
logger.error(f"❌ Cross-asset signals failed: {e}")
# 7. Simulate trade with lower thresholds
logger.info("\n=== SIMULATED TRADE TEST ===")
try:
# Create mock prediction with low confidence
from core.enhanced_orchestrator import EnhancedPrediction
mock_prediction = EnhancedPrediction(
model_name="TEST",
timeframe="1m",
action="BUY",
confidence=0.30, # Lower confidence
overall_action="BUY",
overall_confidence=0.30,
timeframe_predictions=[],
reasoning="Test prediction"
)
# Test if this would generate a trade
current_price = 2500.0
quantity = 0.01
logger.info(f"Mock prediction: {mock_prediction.action} "
f"(confidence: {mock_prediction.confidence:.3f})")
if mock_prediction.confidence > 0.25: # Our new lower threshold
logger.info("✅ Would generate trade with new threshold")
else:
logger.warning("❌ Still below threshold")
except Exception as e:
logger.error(f"❌ Simulated trade test failed: {e}")
# 8. Check RL reward functions
logger.info("\n=== RL REWARD FUNCTION TEST ===")
try:
# Test reward calculation
mock_trade = {
'action': 'BUY',
'confidence': 0.75,
'price': 2500.0,
'timestamp': datetime.now()
}
mock_outcome = {
'net_pnl': 25.0, # $25 profit
'exit_price': 2525.0,
'duration': timedelta(minutes=15)
}
mock_market_data = {
'volatility': 0.03,
'order_flow_direction': 'bullish',
'order_flow_strength': 0.8
}
if hasattr(orchestrator, 'calculate_enhanced_pivot_reward'):
reward = orchestrator.calculate_enhanced_pivot_reward(
mock_trade, mock_market_data, mock_outcome
)
logger.info(f"✅ RL reward for profitable trade: {reward:.3f}")
else:
logger.warning("❌ Enhanced pivot reward function not available")
except Exception as e:
logger.error(f"❌ RL reward test failed: {e}")
logger.info("\n=== DIAGNOSTIC COMPLETE ===")
logger.info("Check results above to identify trading bottlenecks")
except Exception as e:
logger.error(f"Diagnostic failed: {e}")
import traceback
traceback.print_exc()
if __name__ == "__main__":
asyncio.run(diagnose_trading_system())

View File

@ -0,0 +1,268 @@
# Universal Data Stream Architecture Audit & Optimization Plan
## 📊 UNIVERSAL DATA FORMAT SPECIFICATION
Our trading system is built around **5 core timeseries streams** that provide a standardized data format to all models:
### Core Timeseries (The Sacred 5)
1. **ETH/USDT Ticks (1s)** - Primary trading pair real-time data
2. **ETH/USDT 1m** - Short-term price action and patterns
3. **ETH/USDT 1h** - Medium-term trends and momentum
4. **ETH/USDT 1d** - Long-term market structure
5. **BTC/USDT Ticks (1s)** - Reference asset for correlation analysis
### Data Format Structure
```python
@dataclass
class UniversalDataStream:
eth_ticks: np.ndarray # [timestamp, open, high, low, close, volume]
eth_1m: np.ndarray # [timestamp, open, high, low, close, volume]
eth_1h: np.ndarray # [timestamp, open, high, low, close, volume]
eth_1d: np.ndarray # [timestamp, open, high, low, close, volume]
btc_ticks: np.ndarray # [timestamp, open, high, low, close, volume]
timestamp: datetime
metadata: Dict[str, Any]
```
## 🏗️ CURRENT ARCHITECTURE COMPONENTS
### 1. Universal Data Adapter (`core/universal_data_adapter.py`)
- **Status**: ✅ Implemented
- **Purpose**: Converts any data source into universal 5-timeseries format
- **Key Features**:
- Format validation
- Data quality assessment
- Model-specific formatting (CNN, RL, Transformer)
- Window size management
- Missing data handling
### 2. Unified Data Stream (`core/unified_data_stream.py`)
- **Status**: ✅ Implemented with Subscriber Architecture
- **Purpose**: Central data distribution hub
- **Key Features**:
- Publisher-Subscriber pattern
- Consumer registration system
- Multi-consumer data distribution
- Performance tracking
- Data caching and buffering
### 3. Enhanced Orchestrator Integration
- **Status**: ✅ Implemented
- **Purpose**: Neural Decision Fusion using universal data
- **Key Features**:
- NN-driven decision making
- Model prediction fusion
- Market context preparation
- Cross-asset correlation analysis
## 📈 DATA FLOW MAPPING
### Current Data Flow
```
Data Provider (Binance API)
Universal Data Adapter
Unified Data Stream (Publisher)
┌─────────────────┬─────────────────┬─────────────────┐
│ Dashboard │ Orchestrator │ Models │
│ Subscriber │ Subscriber │ Subscriber │
└─────────────────┴─────────────────┴─────────────────┘
```
### Registered Consumers
1. **Trading Dashboard** - UI data updates (`ticks`, `ohlcv`, `ui_data`)
2. **Enhanced Orchestrator** - NN decision making (`training_data`, `ohlcv`)
3. **CNN Models** - Pattern recognition (formatted CNN data)
4. **RL Models** - Action learning (state vectors)
5. **COB Integration** - Order book analysis (microstructure data)
## 🔍 ARCHITECTURE AUDIT FINDINGS
### ✅ STRENGTHS
1. **Standardized Format**: All models receive consistent data structure
2. **Publisher-Subscriber**: Efficient one-to-many data distribution
3. **Performance Tracking**: Built-in metrics and monitoring
4. **Multi-Timeframe**: Comprehensive temporal view
5. **Real-time Processing**: Live data with proper buffering
### ⚠️ OPTIMIZATION OPPORTUNITIES
#### 1. **Memory Efficiency**
- **Issue**: Multiple data copies across consumers
- **Impact**: High memory usage with many subscribers
- **Solution**: Implement shared memory buffers with copy-on-write
#### 2. **Processing Latency**
- **Issue**: Sequential processing in some callbacks
- **Impact**: Delays in real-time decision making
- **Solution**: Parallel consumer notification with thread pools
#### 3. **Data Staleness**
- **Issue**: No real-time freshness validation
- **Impact**: Models might use outdated data
- **Solution**: Timestamp-based data validity checks
#### 4. **Network Optimization**
- **Issue**: Individual API calls for each timeframe
- **Impact**: Rate limiting and bandwidth waste
- **Solution**: Batch requests and intelligent caching
## 🚀 OPTIMIZATION IMPLEMENTATION PLAN
### Phase 1: Memory Optimization
```python
# Implement shared memory data structures
class SharedDataBuffer:
def __init__(self, max_size: int):
self.data = np.zeros((max_size, 6), dtype=np.float32) # OHLCV + timestamp
self.write_index = 0
self.readers = {} # Consumer ID -> last read index
def write(self, new_data: np.ndarray):
# Atomic write operation
self.data[self.write_index] = new_data
self.write_index = (self.write_index + 1) % len(self.data)
def read(self, consumer_id: str, count: int) -> np.ndarray:
# Return data since last read for this consumer
last_read = self.readers.get(consumer_id, 0)
data_slice = self._get_data_slice(last_read, count)
self.readers[consumer_id] = self.write_index
return data_slice
```
### Phase 2: Parallel Processing
```python
# Implement concurrent consumer notification
class ParallelDataDistributor:
def __init__(self, max_workers: int = 4):
self.executor = ThreadPoolExecutor(max_workers=max_workers)
def distribute_to_consumers(self, data_packet: Dict[str, Any]):
futures = []
for consumer in self.active_consumers:
future = self.executor.submit(self._notify_consumer, consumer, data_packet)
futures.append(future)
# Wait for all notifications to complete
for future in as_completed(futures, timeout=0.1):
try:
future.result()
except Exception as e:
logger.warning(f"Consumer notification failed: {e}")
```
### Phase 3: Intelligent Caching
```python
# Implement smart data caching with expiration
class SmartDataCache:
def __init__(self):
self.cache = {}
self.expiry_times = {}
self.hit_count = 0
self.miss_count = 0
def get_data(self, symbol: str, timeframe: str, force_refresh: bool = False) -> np.ndarray:
cache_key = f"{symbol}_{timeframe}"
current_time = time.time()
if not force_refresh and cache_key in self.cache:
if current_time < self.expiry_times[cache_key]:
self.hit_count += 1
return self.cache[cache_key]
# Cache miss - fetch fresh data
self.miss_count += 1
fresh_data = self._fetch_fresh_data(symbol, timeframe)
# Cache with appropriate expiration
self.cache[cache_key] = fresh_data
self.expiry_times[cache_key] = current_time + self._get_cache_duration(timeframe)
return fresh_data
```
## 📋 INTEGRATION CHECKLIST
### Dashboard Integration
- [ ] Verify `web/clean_dashboard.py` uses UnifiedDataStream
- [ ] Ensure proper subscriber registration
- [ ] Check data type requirements (`ui_data`, `ohlcv`)
- [ ] Validate real-time updates
### Model Integration
- [ ] CNN models receive formatted universal data
- [ ] RL models get proper state vectors
- [ ] Neural Decision Fusion uses all 5 timeseries
- [ ] COB integration processes microstructure data
### Performance Monitoring
- [ ] Stream statistics tracking
- [ ] Consumer performance metrics
- [ ] Data quality monitoring
- [ ] Memory usage optimization
## 🎯 IMMEDIATE ACTION ITEMS
### High Priority
1. **Audit Dashboard Subscriber** - Ensure `clean_dashboard.py` properly subscribes
2. **Verify Model Data Flow** - Check all models receive universal format
3. **Monitor Memory Usage** - Track memory consumption across consumers
4. **Performance Profiling** - Measure data distribution latency
### Medium Priority
1. **Implement Shared Buffers** - Reduce memory duplication
2. **Add Data Freshness Checks** - Prevent stale data usage
3. **Optimize Network Calls** - Batch API requests where possible
4. **Enhanced Error Handling** - Graceful degradation on data issues
### Low Priority
1. **Advanced Caching** - Predictive data pre-loading
2. **Compression** - Reduce data transfer overhead
3. **Distributed Processing** - Scale across multiple processes
4. **Real-time Analytics** - Live data quality metrics
## 🔧 IMPLEMENTATION STATUS
### ✅ Completed
- Universal Data Adapter with 5 timeseries
- Unified Data Stream with subscriber pattern
- Enhanced Orchestrator integration
- Neural Decision Fusion using universal data
### 🚧 In Progress
- Dashboard subscriber optimization
- Memory usage profiling
- Performance monitoring
### 📅 Planned
- Shared memory implementation
- Parallel consumer notification
- Advanced caching strategies
- Real-time quality monitoring
## 📊 SUCCESS METRICS
### Performance Targets
- **Data Latency**: < 10ms from source to consumer
- **Memory Efficiency**: < 500MB total for all consumers
- **Cache Hit Rate**: > 80% for historical data requests
- **Consumer Throughput**: > 100 updates/second per consumer
### Quality Targets
- **Data Completeness**: > 99.9% for all 5 timeseries
- **Timestamp Accuracy**: < 1ms deviation from source
- **Format Compliance**: 100% validation success
- **Error Rate**: < 0.1% failed distributions
---
## 🎯 CONCLUSION
The Universal Data Stream architecture is the **backbone** of our trading system. The 5 timeseries format ensures all models receive consistent, high-quality data. The subscriber architecture enables efficient distribution, but there are clear optimization opportunities for memory usage, processing latency, and caching.
**Next Steps**: Focus on implementing shared memory buffers and parallel consumer notification to improve performance while maintaining the integrity of our universal data format.
**Critical**: All optimization work must preserve the 5 timeseries structure as it's fundamental to our model training and decision making processes.

View File

@ -0,0 +1,233 @@
# Universal Data Stream Architecture Audit & Optimization Plan
## 📊 UNIVERSAL DATA FORMAT SPECIFICATION
Our trading system is built around **5 core timeseries streams** that provide a standardized data format to all models:
### Core Timeseries (The Sacred 5)
1. **ETH/USDT Ticks (1s)** - Primary trading pair real-time data
2. **ETH/USDT 1m** - Short-term price action and patterns
3. **ETH/USDT 1h** - Medium-term trends and momentum
4. **ETH/USDT 1d** - Long-term market structure
5. **BTC/USDT Ticks (1s)** - Reference asset for correlation analysis
### Data Format Structure
```python
@dataclass
class UniversalDataStream:
eth_ticks: np.ndarray # [timestamp, open, high, low, close, volume]
eth_1m: np.ndarray # [timestamp, open, high, low, close, volume]
eth_1h: np.ndarray # [timestamp, open, high, low, close, volume]
eth_1d: np.ndarray # [timestamp, open, high, low, close, volume]
btc_ticks: np.ndarray # [timestamp, open, high, low, close, volume]
timestamp: datetime
metadata: Dict[str, Any]
```
## 🏗️ CURRENT ARCHITECTURE COMPONENTS
### 1. Universal Data Adapter (`core/universal_data_adapter.py`)
- **Status**: ✅ Implemented
- **Purpose**: Converts any data source into universal 5-timeseries format
- **Key Features**:
- Format validation
- Data quality assessment
- Model-specific formatting (CNN, RL, Transformer)
- Window size management
- Missing data handling
### 2. Unified Data Stream (`core/unified_data_stream.py`)
- **Status**: ✅ Implemented with Subscriber Architecture
- **Purpose**: Central data distribution hub
- **Key Features**:
- Publisher-Subscriber pattern
- Consumer registration system
- Multi-consumer data distribution
- Performance tracking
- Data caching and buffering
### 3. Enhanced Orchestrator Integration
- **Status**: ✅ Implemented
- **Purpose**: Neural Decision Fusion using universal data
- **Key Features**:
- NN-driven decision making
- Model prediction fusion
- Market context preparation
- Cross-asset correlation analysis
## 📈 DATA FLOW MAPPING
### Current Data Flow
```
Data Provider (Binance API)
Universal Data Adapter
Unified Data Stream (Publisher)
┌─────────────────┬─────────────────┬─────────────────┐
│ Dashboard │ Orchestrator │ Models │
│ Subscriber │ Subscriber │ Subscriber │
└─────────────────┴─────────────────┴─────────────────┘
```
### Registered Consumers
1. **Trading Dashboard** - UI data updates (`ticks`, `ohlcv`, `ui_data`)
2. **Enhanced Orchestrator** - NN decision making (`training_data`, `ohlcv`)
3. **CNN Models** - Pattern recognition (formatted CNN data)
4. **RL Models** - Action learning (state vectors)
5. **COB Integration** - Order book analysis (microstructure data)
## 🔍 ARCHITECTURE AUDIT FINDINGS
### ✅ STRENGTHS
1. **Standardized Format**: All models receive consistent data structure
2. **Publisher-Subscriber**: Efficient one-to-many data distribution
3. **Performance Tracking**: Built-in metrics and monitoring
4. **Multi-Timeframe**: Comprehensive temporal view
5. **Real-time Processing**: Live data with proper buffering
### ⚠️ OPTIMIZATION OPPORTUNITIES
#### 1. **Memory Efficiency**
- **Issue**: Multiple data copies across consumers
- **Impact**: High memory usage with many subscribers
- **Solution**: Implement shared memory buffers with copy-on-write
#### 2. **Processing Latency**
- **Issue**: Sequential processing in some callbacks
- **Impact**: Delays in real-time decision making
- **Solution**: Parallel consumer notification with thread pools
#### 3. **Data Staleness**
- **Issue**: No real-time freshness validation
- **Impact**: Models might use outdated data
- **Solution**: Timestamp-based data validity checks
#### 4. **Network Optimization**
- **Issue**: Individual API calls for each timeframe
- **Impact**: Rate limiting and bandwidth waste
- **Solution**: Batch requests and intelligent caching
## 🚀 OPTIMIZATION IMPLEMENTATION PLAN
### Phase 1: Memory Optimization
```python
# Implement shared memory data structures
class SharedDataBuffer:
def __init__(self, max_size: int):
self.data = np.zeros((max_size, 6), dtype=np.float32) # OHLCV + timestamp
self.write_index = 0
self.readers = {} # Consumer ID -> last read index
def write(self, new_data: np.ndarray):
# Atomic write operation
self.data[self.write_index] = new_data
self.write_index = (self.write_index + 1) % len(self.data)
def read(self, consumer_id: str, count: int) -> np.ndarray:
# Return data since last read for this consumer
last_read = self.readers.get(consumer_id, 0)
data_slice = self._get_data_slice(last_read, count)
self.readers[consumer_id] = self.write_index
return data_slice
```
## 📋 INTEGRATION CHECKLIST
### Dashboard Integration
- [x] Verify `web/clean_dashboard.py` uses UnifiedDataStream ✅
- [x] Ensure proper subscriber registration ✅
- [x] Check data type requirements (`ui_data`, `ohlcv`) ✅
- [x] Validate real-time updates ✅
### Model Integration
- [x] CNN models receive formatted universal data ✅
- [x] RL models get proper state vectors ✅
- [x] Neural Decision Fusion uses all 5 timeseries ✅
- [x] COB integration processes microstructure data ✅
### Performance Monitoring
- [x] Stream statistics tracking ✅
- [x] Consumer performance metrics ✅
- [x] Data quality monitoring ✅
- [ ] Memory usage optimization
## 🧪 INTEGRATION TEST RESULTS
**Date**: 2025-06-25 10:54:55
**Status**: ✅ **PASSED**
### Test Results Summary:
- ✅ Universal Data Stream properly integrated
- ✅ Dashboard subscribes as consumer (ID: CleanTradingDashboard_1750837973)
- ✅ All 5 timeseries format validated:
- ETH ticks: 60 samples ✅
- ETH 1m: 60 candles ✅
- ETH 1h: 24 candles ✅
- ETH 1d: 30 candles ✅
- BTC ticks: 60 samples ✅
- ✅ Data callback processing works
- ✅ Universal Data Adapter functional
- ✅ Consumer registration: 1 active consumer
- ✅ Neural Decision Fusion initialized with 3 models
- ✅ COB integration with 2.5B parameter model active
### Key Metrics Achieved:
- **Consumers Registered**: 1/1 active
- **Data Format Compliance**: 100% validation passed
- **Model Integration**: 3 NN models registered
- **Real-time Processing**: Active with 200ms inference
- **Memory Footprint**: Efficient subscriber pattern
## 🎯 IMMEDIATE ACTION ITEMS
### High Priority - COMPLETED ✅
1. **Audit Dashboard Subscriber** - ✅ Verified `clean_dashboard.py` properly subscribes
2. **Verify Model Data Flow** - ✅ Confirmed all models receive universal format
3. **Monitor Memory Usage** - 🚧 Basic tracking active, optimization pending
4. **Performance Profiling** - ✅ Stream stats and consumer metrics working
### Medium Priority - IN PROGRESS 🚧
1. **Implement Shared Buffers** - 📅 Planned for Phase 1
2. **Add Data Freshness Checks** - ✅ Timestamp validation active
3. **Optimize Network Calls** - ✅ Binance API rate limiting handled
4. **Enhanced Error Handling** - ✅ Graceful degradation implemented
## 🔧 IMPLEMENTATION STATUS UPDATE
### ✅ Completed
- Universal Data Adapter with 5 timeseries ✅
- Unified Data Stream with subscriber pattern ✅
- Enhanced Orchestrator integration ✅
- Neural Decision Fusion using universal data ✅
- Dashboard subscriber integration ✅
- Format validation and quality checks ✅
- Real-time callback processing ✅
### 🚧 In Progress
- Memory usage optimization (shared buffers planned)
- Advanced caching strategies
- Performance profiling and monitoring
### 📅 Planned
- Parallel consumer notification
- Compression for data transfer
- Distributed processing capabilities
---
## 🎯 UPDATED CONCLUSION
**SUCCESS**: The Universal Data Stream architecture is **fully operational** and properly integrated across all components. The 5 timeseries format (ETH ticks/1m/1h/1d + BTC ticks) is successfully distributed to all consumers through the subscriber pattern.
**Key Achievements**:
- ✅ Clean Trading Dashboard properly subscribes and receives all 5 timeseries
- ✅ Enhanced Orchestrator uses Universal Data Adapter for standardized format
- ✅ Neural Decision Fusion processes data from all timeframes
- ✅ COB integration active with 2.5B parameter model
- ✅ Real-time processing with proper error handling
**Current Status**: Production-ready with optimization opportunities for memory and latency improvements.
**Critical**: The 5 timeseries structure is maintained and validated - fundamental architecture is solid and scalable.

View File

@ -0,0 +1,179 @@
# Universal Data Stream Implementation Summary
## 🎯 OVERVIEW
The **Universal Data Stream** is now fully implemented and operational as the central data backbone of our trading system. It provides a standardized 5 timeseries format to all models and components through an efficient subscriber architecture.
## 📊 THE SACRED 5 TIMESERIES
Our trading system is built around these core data streams:
1. **ETH/USDT Ticks (1s)** - Primary trading pair real-time tick data
2. **ETH/USDT 1m** - Short-term price action and patterns
3. **ETH/USDT 1h** - Medium-term trends and momentum
4. **ETH/USDT 1d** - Long-term market structure
5. **BTC/USDT Ticks (1s)** - Reference asset for correlation analysis
## 🏗️ ARCHITECTURE COMPONENTS
### Core Components ✅ IMPLEMENTED
1. **Universal Data Adapter** (`core/universal_data_adapter.py`)
- Converts any data source into universal 5-timeseries format
- Validates data quality and format compliance
- Provides model-specific formatting (CNN, RL, Transformer)
2. **Unified Data Stream** (`core/unified_data_stream.py`)
- Publisher-subscriber pattern for efficient data distribution
- Consumer registration and management
- Multi-timeframe data caching and buffering
- Performance tracking and monitoring
3. **Enhanced Orchestrator Integration** (`core/enhanced_orchestrator.py`)
- Neural Decision Fusion using universal data
- Cross-asset correlation analysis
- NN-driven decision making with all 5 timeseries
4. **Dashboard Integration** (`web/clean_dashboard.py`)
- Subscribes as consumer to universal stream
- Real-time UI updates from standardized data
- Proper callback handling for all data types
## 🔄 DATA FLOW ARCHITECTURE
```
Binance API (Data Source)
Universal Data Adapter (Format Standardization)
Unified Data Stream (Publisher)
┌─────────────────┬─────────────────┬─────────────────┐
│ Dashboard │ Orchestrator │ NN Models │
│ Consumer │ Consumer │ Consumer │
│ • UI Updates │ • NN Decisions │ • CNN Features │
│ • Price Display │ • Cross-Asset │ • RL States │
│ • Charts │ • Correlation │ • COB Analysis │
└─────────────────┴─────────────────┴─────────────────┘
```
## ✅ IMPLEMENTATION STATUS
### Fully Operational Components
1. **Universal Data Adapter**
- ✅ 5 timeseries format validated
- ✅ Data quality assessment working
- ✅ Format validation: 100% compliance
- ✅ Model-specific formatting available
2. **Unified Data Stream**
- ✅ Publisher-subscriber pattern active
- ✅ Consumer registration working
- ✅ Real-time data distribution
- ✅ Performance monitoring enabled
3. **Dashboard Integration**
- ✅ Subscriber registration: `CleanTradingDashboard_1750837973`
- ✅ Data callback processing functional
- ✅ Real-time updates working
- ✅ Multi-timeframe data display
4. **Enhanced Orchestrator**
- ✅ Universal Data Adapter initialized
- ✅ Neural Decision Fusion using all 5 timeseries
- ✅ Cross-asset correlation analysis
- ✅ NN-driven trading decisions
5. **Model Integration**
- ✅ Williams CNN: Pattern recognition from universal data
- ✅ DQN Agent: Action learning from state vectors
- ✅ COB RL: 2.5B parameter model processing microstructure
- ✅ Neural Decision Fusion: Central NN coordinator
## 📈 PERFORMANCE METRICS
### Test Results (2025-06-25 10:54:55)
- **Data Format Compliance**: 100% validation passed
- **Consumer Registration**: 1/1 active consumers
- **Model Integration**: 3 NN models registered and functional
- **Real-time Processing**: 200ms inference interval
- **Data Samples**: ETH(60 ticks, 60×1m, 24×1h, 30×1d) + BTC(60 ticks)
### Memory and Performance
- **Subscriber Pattern**: Efficient one-to-many distribution
- **Data Caching**: Multi-timeframe buffers with proper limits
- **Error Handling**: Graceful degradation on data issues
- **Quality Monitoring**: Real-time validation and scoring
## 🔧 KEY FEATURES IMPLEMENTED
### Data Distribution
- **Publisher-Subscriber Pattern**: Efficient one-to-many data sharing
- **Consumer Types**: `ticks`, `ohlcv`, `training_data`, `ui_data`
- **Real-time Updates**: Live data streaming with proper buffering
- **Format Validation**: Ensures all consumers receive valid data
### Model Integration
- **Standardized Format**: All models receive same data structure
- **Multi-Timeframe**: Comprehensive temporal analysis
- **Cross-Asset**: ETH trading with BTC correlation signals
- **Neural Fusion**: Central NN processes all model predictions
### Performance Optimization
- **Efficient Caching**: Time-aware data retention
- **Parallel Processing**: Non-blocking consumer notifications
- **Quality Monitoring**: Real-time data validation
- **Error Recovery**: Graceful handling of network/API issues
## 📋 INTEGRATION VALIDATION
### Dashboard Integration ✅
- [x] Universal Data Stream subscription active
- [x] Consumer callback processing working
- [x] Real-time price updates from universal data
- [x] Multi-timeframe chart integration
### Model Integration ✅
- [x] CNN models receive formatted universal data
- [x] RL models get proper state vectors
- [x] Neural Decision Fusion processes all 5 timeseries
- [x] COB integration with microstructure data
### Data Quality ✅
- [x] Format validation: 100% compliance
- [x] Timestamp accuracy maintained
- [x] Missing data handling implemented
- [x] Quality scoring and monitoring active
## 🚀 OPTIMIZATION OPPORTUNITIES
### Planned Improvements
1. **Memory Optimization**: Shared buffers to reduce duplication
2. **Parallel Processing**: Concurrent consumer notification
3. **Advanced Caching**: Intelligent pre-loading and compression
4. **Distributed Processing**: Scale across multiple processes
### Performance Targets
- **Data Latency**: < 10ms from source to consumer
- **Memory Efficiency**: < 500MB total for all consumers
- **Cache Hit Rate**: > 80% for historical requests
- **Consumer Throughput**: > 100 updates/second
## 🎯 CONCLUSION
**STATUS**: ✅ **FULLY OPERATIONAL**
The Universal Data Stream architecture is successfully implemented and provides the foundation for all trading operations. The 5 timeseries format ensures consistent, high-quality data across all models and components.
**Key Achievements**:
- ✅ Standardized data format across entire system
- ✅ Efficient subscriber architecture for data distribution
- ✅ Real-time processing with proper error handling
- ✅ Complete integration with dashboard and models
- ✅ Neural Decision Fusion using all timeseries
- ✅ Production-ready with monitoring and validation
**Next Steps**: Focus on memory optimization and advanced caching while maintaining the proven 5 timeseries structure that forms the backbone of our trading strategy.
**Critical Success Factor**: The Universal Data Stream ensures all models and components work with identical, validated data - eliminating inconsistencies and enabling reliable cross-component communication.

View File

@ -114,11 +114,26 @@ def start_clean_dashboard_with_training():
logger.info("CLEAN TRADING DASHBOARD + FULL TRAINING PIPELINE") logger.info("CLEAN TRADING DASHBOARD + FULL TRAINING PIPELINE")
logger.info("=" * 80) logger.info("=" * 80)
logger.info("Features: Real-time Training, COB Integration, Clean UI") logger.info("Features: Real-time Training, COB Integration, Clean UI")
logger.info("Universal Data Stream: ENABLED")
logger.info("Neural Decision Fusion: ENABLED")
logger.info("COB Integration: ENABLED")
logger.info("GPU Training: ENABLED") logger.info("GPU Training: ENABLED")
logger.info("Multi-symbol: ETH/USDT, BTC/USDT") logger.info("Multi-symbol: ETH/USDT, BTC/USDT")
logger.info("Dashboard: http://127.0.0.1:8051")
# Get port from environment or use default
dashboard_port = int(os.environ.get('DASHBOARD_PORT', '8051'))
logger.info(f"Dashboard: http://127.0.0.1:{dashboard_port}")
logger.info("=" * 80) logger.info("=" * 80)
# Check environment variables
enable_universal_stream = os.environ.get('ENABLE_UNIVERSAL_DATA_STREAM', '1') == '1'
enable_nn_fusion = os.environ.get('ENABLE_NN_DECISION_FUSION', '1') == '1'
enable_cob = os.environ.get('ENABLE_COB_INTEGRATION', '1') == '1'
logger.info(f"Universal Data Stream: {'ENABLED' if enable_universal_stream else 'DISABLED'}")
logger.info(f"Neural Decision Fusion: {'ENABLED' if enable_nn_fusion else 'DISABLED'}")
logger.info(f"COB Integration: {'ENABLED' if enable_cob else 'DISABLED'}")
# Get configuration # Get configuration
config = get_config() config = get_config()
@ -170,7 +185,7 @@ def start_clean_dashboard_with_training():
# Start dashboard server (this blocks) # Start dashboard server (this blocks)
logger.info("🚀 Starting Clean Dashboard Server...") logger.info("🚀 Starting Clean Dashboard Server...")
dashboard.run_server(host='127.0.0.1', port=8051, debug=False) dashboard.run_server(host='127.0.0.1', port=dashboard_port, debug=False)
except KeyboardInterrupt: except KeyboardInterrupt:
logger.info("System stopped by user") logger.info("System stopped by user")

View File

@ -1,121 +0,0 @@
#!/usr/bin/env python3
"""
Test the Fixed Scalping Dashboard
This script tests if the scalping dashboard is now returning proper JSON data
instead of HTTP 204 No Content responses.
"""
import requests
import json
import time
def test_scalping_dashboard_response():
"""Test if scalping dashboard returns proper JSON data"""
base_url = "http://127.0.0.1:8051"
print("Testing Scalping Dashboard Response...")
print(f"Base URL: {base_url}")
try:
# Test main dashboard page
print("\n1. Testing main dashboard page...")
response = requests.get(base_url, timeout=10)
print(f" Status: {response.status_code}")
print(f" Content Type: {response.headers.get('content-type', 'Unknown')}")
print(f" Response Size: {len(response.content)} bytes")
if response.status_code == 200:
print(" ✅ Main page loads successfully")
else:
print(f" ❌ Main page failed with status {response.status_code}")
# Test callback endpoint (simulating what the frontend does)
print("\n2. Testing dashboard callback endpoint...")
callback_url = f"{base_url}/_dash-update-component"
# Dash callback payload (this is what the frontend sends)
callback_data = {
"output": [
{"id": "current-balance", "property": "children"},
{"id": "session-duration", "property": "children"},
{"id": "open-positions", "property": "children"},
{"id": "live-pnl", "property": "children"},
{"id": "win-rate", "property": "children"},
{"id": "total-trades", "property": "children"},
{"id": "last-action", "property": "children"},
{"id": "eth-price", "property": "children"},
{"id": "btc-price", "property": "children"},
{"id": "main-eth-1s-chart", "property": "figure"},
{"id": "eth-1m-chart", "property": "figure"},
{"id": "eth-1h-chart", "property": "figure"},
{"id": "eth-1d-chart", "property": "figure"},
{"id": "btc-1s-chart", "property": "figure"},
{"id": "model-training-status", "property": "children"},
{"id": "orchestrator-status", "property": "children"},
{"id": "training-events-log", "property": "children"},
{"id": "actions-log", "property": "children"},
{"id": "debug-status", "property": "children"}
],
"inputs": [{"id": "ultra-fast-interval", "property": "n_intervals", "value": 1}],
"changedPropIds": ["ultra-fast-interval.n_intervals"]
}
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
# Wait a moment for the dashboard to initialize
print(" Waiting 3 seconds for dashboard initialization...")
time.sleep(3)
response = requests.post(callback_url, json=callback_data, headers=headers, timeout=15)
print(f" Status: {response.status_code}")
print(f" Content Type: {response.headers.get('content-type', 'Unknown')}")
print(f" Response Size: {len(response.content)} bytes")
if response.status_code == 200:
print(" ✅ Callback returns HTTP 200 (Success!)")
try:
response_json = response.json()
print(f" ✅ Response contains JSON data")
print(f" 📊 Number of data elements: {len(response_json.get('response', {}))}")
# Check if we have chart data
if 'response' in response_json:
resp_data = response_json['response']
# Count chart objects (they should be dictionaries with 'data' and 'layout')
chart_count = 0
for key, value in resp_data.items():
if isinstance(value, dict) and 'data' in value and 'layout' in value:
chart_count += 1
print(f" 📈 Chart objects found: {chart_count}")
if chart_count >= 5: # Should have 5 charts
print(" ✅ All expected charts are present!")
else:
print(f" ⚠️ Expected 5 charts, found {chart_count}")
else:
print(" ⚠️ No 'response' key in JSON data")
except json.JSONDecodeError:
print(" ❌ Response is not valid JSON")
print(f" Raw response: {response.text[:200]}...")
elif response.status_code == 204:
print(" ❌ Still returning HTTP 204 (No Content) - Issue not fixed")
else:
print(f" ❌ Unexpected status code: {response.status_code}")
except requests.exceptions.ConnectionError:
print(" ❌ Cannot connect to dashboard - is it running?")
except requests.exceptions.Timeout:
print(" ❌ Request timed out")
except Exception as e:
print(f" ❌ Error: {e}")
if __name__ == "__main__":
test_scalping_dashboard_response()

View File

@ -1,67 +0,0 @@
#!/usr/bin/env python3
"""
Minimal dashboard to test callback structure
"""
import dash
from dash import dcc, html, Input, Output
import plotly.graph_objects as go
from datetime import datetime
import logging
# Setup logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Create Dash app
app = dash.Dash(__name__)
# Simple layout
app.layout = html.Div([
html.H1("Simple Test Dashboard"),
html.Div(id="test-output"),
dcc.Graph(id="test-chart"),
dcc.Interval(id='test-interval', interval=2000, n_intervals=0)
])
# Simple callback
@app.callback(
Output('test-output', 'children'),
Output('test-chart', 'figure'),
Input('test-interval', 'n_intervals')
)
def update_dashboard(n_intervals):
"""Simple callback to test basic functionality"""
try:
logger.info(f"Callback triggered: {n_intervals}")
# Simple text output
text_output = f"Update #{n_intervals} at {datetime.now().strftime('%H:%M:%S')}"
# Simple chart
fig = go.Figure()
fig.add_trace(go.Scatter(
x=[1, 2, 3, 4, 5],
y=[n_intervals, n_intervals+1, n_intervals+2, n_intervals+1, n_intervals],
mode='lines',
name='Test Data'
))
fig.update_layout(
title=f"Test Chart - Update {n_intervals}",
template="plotly_dark"
)
logger.info(f"Returning: text='{text_output}', chart=<Figure>")
return text_output, fig
except Exception as e:
logger.error(f"Error in callback: {e}")
import traceback
logger.error(f"Traceback: {traceback.format_exc()}")
# Return safe fallback
return f"Error: {str(e)}", go.Figure()
if __name__ == "__main__":
logger.info("Starting simple test dashboard on port 8052...")
app.run(host='127.0.0.1', port=8052, debug=True)

View File

@ -1,50 +0,0 @@
from datetime import datetime, timedelta
from dataprovider_realtime import RealTimeChart
# Create a chart instance
chart = RealTimeChart('BTC/USDT')
# Add a BUY position from yesterday
yesterday = datetime.now() - timedelta(days=1)
chart.add_trade(
price=64950.25,
timestamp=yesterday,
amount=0.5,
pnl=None,
action='BUY'
)
print(f'Added BUY position from {yesterday}')
# Add a matching SELL position from yesterday (2 hours later)
yesterday_plus_2h = yesterday + timedelta(hours=2)
chart.add_trade(
price=65100.75,
timestamp=yesterday_plus_2h,
amount=0.5,
pnl=75.25,
action='SELL'
)
print(f'Added matching SELL position from {yesterday_plus_2h}')
# Add a trade from 2 days ago
two_days_ago = datetime.now() - timedelta(days=2)
chart.add_trade(
price=64800.50,
timestamp=two_days_ago,
amount=0.25,
pnl=None,
action='BUY'
)
print(f'Added BUY position from {two_days_ago}')
two_days_ago_plus_3h = two_days_ago + timedelta(hours=3)
chart.add_trade(
price=65000.75,
timestamp=two_days_ago_plus_3h,
amount=0.25,
pnl=50.06,
action='SELL'
)
print(f'Added matching SELL position from {two_days_ago_plus_3h}')
print('\nAll test trades added successfully!')

View File

@ -1,204 +0,0 @@
#!/usr/bin/env python3
"""
Test Training Integration with Dashboard
This script tests the enhanced dashboard's ability to:
1. Stream training data to CNN and DQN models
2. Display real-time training metrics and progress
3. Show model learning curves and performance
4. Integrate with the continuous training system
"""
import sys
import logging
import time
import asyncio
from datetime import datetime, timedelta
from pathlib import Path
# Setup logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def test_training_integration():
"""Test the training integration functionality"""
try:
print("="*60)
print("TESTING TRAINING INTEGRATION WITH DASHBOARD")
print("="*60)
# Import dashboard
from web.dashboard import TradingDashboard
from core.data_provider import DataProvider
from core.orchestrator import TradingOrchestrator
# Create components
data_provider = DataProvider()
orchestrator = TradingOrchestrator(data_provider)
dashboard = TradingDashboard(data_provider, orchestrator)
print(f"✓ Dashboard created with training integration")
print(f"✓ Continuous training active: {getattr(dashboard, 'training_active', False)}")
# Test 1: Simulate tick data for training
print("\n📊 TEST 1: Simulating Tick Data")
print("-" * 40)
# Add simulated tick data to cache
base_price = 3500.0
for i in range(1000):
tick_data = {
'timestamp': datetime.now() - timedelta(seconds=1000-i),
'price': base_price + (i % 100) * 0.1,
'volume': 100 + (i % 50),
'side': 'buy' if i % 2 == 0 else 'sell'
}
dashboard.tick_cache.append(tick_data)
print(f"✓ Added {len(dashboard.tick_cache)} ticks to cache")
# Test 2: Prepare training data
print("\n🔄 TEST 2: Preparing Training Data")
print("-" * 40)
training_data = dashboard._prepare_training_data()
if training_data:
print(f"✓ Training data prepared successfully")
print(f" - OHLCV bars: {len(training_data['ohlcv'])}")
print(f" - Features: {training_data['features']}")
print(f" - Symbol: {training_data['symbol']}")
else:
print("❌ Failed to prepare training data")
# Test 3: Format data for CNN
print("\n🧠 TEST 3: CNN Data Formatting")
print("-" * 40)
if training_data:
cnn_data = dashboard._format_data_for_cnn(training_data)
if cnn_data and 'sequences' in cnn_data:
print(f"✓ CNN data formatted successfully")
print(f" - Sequences shape: {cnn_data['sequences'].shape}")
print(f" - Targets shape: {cnn_data['targets'].shape}")
print(f" - Sequence length: {cnn_data['sequence_length']}")
else:
print("❌ Failed to format CNN data")
# Test 4: Format data for RL
print("\n🤖 TEST 4: RL Data Formatting")
print("-" * 40)
if training_data:
rl_experiences = dashboard._format_data_for_rl(training_data)
if rl_experiences:
print(f"✓ RL experiences formatted successfully")
print(f" - Number of experiences: {len(rl_experiences)}")
print(f" - Experience format: (state, action, reward, next_state, done)")
print(f" - Sample experience shapes: {[len(exp) for exp in rl_experiences[:3]]}")
else:
print("❌ Failed to format RL experiences")
# Test 5: Send training data to models
print("\n📤 TEST 5: Sending Training Data to Models")
print("-" * 40)
success = dashboard.send_training_data_to_models()
print(f"✓ Training data sent: {success}")
if hasattr(dashboard, 'training_stats'):
stats = dashboard.training_stats
print(f" - Total training sessions: {stats.get('total_training_sessions', 0)}")
print(f" - CNN training count: {stats.get('cnn_training_count', 0)}")
print(f" - RL training count: {stats.get('rl_training_count', 0)}")
print(f" - Training data points: {stats.get('training_data_points', 0)}")
# Test 6: Training metrics display
print("\n📈 TEST 6: Training Metrics Display")
print("-" * 40)
training_metrics = dashboard._create_training_metrics()
print(f"✓ Training metrics created: {len(training_metrics)} components")
# Test 7: Model training status
print("\n🔍 TEST 7: Model Training Status")
print("-" * 40)
training_status = dashboard._get_model_training_status()
print(f"✓ Training status retrieved")
print(f" - CNN status: {training_status['cnn']['status']}")
print(f" - CNN accuracy: {training_status['cnn']['accuracy']:.1%}")
print(f" - RL status: {training_status['rl']['status']}")
print(f" - RL win rate: {training_status['rl']['win_rate']:.1%}")
# Test 8: Training events log
print("\n📝 TEST 8: Training Events Log")
print("-" * 40)
training_events = dashboard._get_recent_training_events()
print(f"✓ Training events retrieved: {len(training_events)} events")
# Test 9: Mini training chart
print("\n📊 TEST 9: Mini Training Chart")
print("-" * 40)
try:
training_chart = dashboard._create_mini_training_chart(training_status)
print(f"✓ Mini training chart created")
print(f" - Chart type: {type(training_chart)}")
except Exception as e:
print(f"❌ Error creating training chart: {e}")
# Test 10: Continuous training loop
print("\n🔄 TEST 10: Continuous Training Loop")
print("-" * 40)
print(f"✓ Continuous training active: {getattr(dashboard, 'training_active', False)}")
if hasattr(dashboard, 'training_thread'):
print(f"✓ Training thread alive: {dashboard.training_thread.is_alive()}")
# Test 11: Integration with existing continuous training system
print("\n🔗 TEST 11: Integration with Continuous Training System")
print("-" * 40)
try:
# Check if we can get tick cache for external training
tick_cache = dashboard.get_tick_cache_for_training()
print(f"✓ Tick cache accessible: {len(tick_cache)} ticks")
# Check if we can get 1-second bars
one_second_bars = dashboard.get_one_second_bars()
print(f"✓ 1-second bars accessible: {len(one_second_bars)} bars")
except Exception as e:
print(f"❌ Error accessing training data: {e}")
print("\n" + "="*60)
print("TRAINING INTEGRATION TEST COMPLETED")
print("="*60)
# Summary
print("\n📋 SUMMARY:")
print(f"✓ Dashboard with training integration: WORKING")
print(f"✓ Training data preparation: WORKING")
print(f"✓ CNN data formatting: WORKING")
print(f"✓ RL data formatting: WORKING")
print(f"✓ Training metrics display: WORKING")
print(f"✓ Continuous training: ACTIVE")
print(f"✓ Model status tracking: WORKING")
print(f"✓ Training events logging: WORKING")
return True
except Exception as e:
logger.error(f"Training integration test failed: {e}")
import traceback
traceback.print_exc()
return False
if __name__ == "__main__":
success = test_training_integration()
if success:
print("\n🎉 All training integration tests passed!")
else:
print("\n❌ Some training integration tests failed!")
sys.exit(1)

Some files were not shown because too many files have changed in this diff Show More