renames
This commit is contained in:
713
.kiro/specs/1.multi-modal-trading-system/design.md
Normal file
713
.kiro/specs/1.multi-modal-trading-system/design.md
Normal file
@@ -0,0 +1,713 @@
|
||||
# Multi-Modal Trading System Design Document
|
||||
|
||||
## Overview
|
||||
|
||||
The Multi-Modal Trading System is designed as an advanced algorithmic trading platform that combines Convolutional Neural Networks (CNN) and Reinforcement Learning (RL) models orchestrated by a decision-making module. The system processes multi-timeframe and multi-symbol market data (primarily ETH and BTC) to generate trading actions.
|
||||
|
||||
This design document outlines the architecture, components, data flow, and implementation details for the system based on the requirements and existing codebase.
|
||||
|
||||
## Architecture
|
||||
|
||||
The system follows a modular architecture with clear separation of concerns:
|
||||
|
||||
```mermaid
|
||||
graph TD
|
||||
A[Data Provider] --> B[Data Processor] (calculates pivot points)
|
||||
B --> C[CNN Model]
|
||||
B --> D[RL(DQN) Model]
|
||||
C --> E[Orchestrator]
|
||||
D --> E
|
||||
E --> F[Trading Executor]
|
||||
E --> G[Dashboard]
|
||||
F --> G
|
||||
H[Risk Manager] --> F
|
||||
H --> G
|
||||
```
|
||||
|
||||
### Key Components
|
||||
|
||||
1. **Data Provider**: Centralized component responsible for collecting, processing, and distributing market data from multiple sources.
|
||||
2. **Data Processor**: Processes raw market data, calculates technical indicators, and identifies pivot points.
|
||||
3. **CNN Model**: Analyzes patterns in market data and predicts pivot points across multiple timeframes.
|
||||
4. **RL Model**: Learns optimal trading strategies based on market data and CNN predictions.
|
||||
5. **Orchestrator**: Makes final trading decisions based on inputs from both CNN and RL models.
|
||||
6. **Trading Executor**: Executes trading actions through brokerage APIs.
|
||||
7. **Risk Manager**: Implements risk management features like stop-loss and position sizing.
|
||||
8. **Dashboard**: Provides a user interface for monitoring and controlling the system.
|
||||
|
||||
## Components and Interfaces
|
||||
|
||||
### 1. Data Provider
|
||||
|
||||
The Data Provider is the foundation of the system, responsible for collecting, processing, and distributing market data to all other components.
|
||||
|
||||
#### Key Classes and Interfaces
|
||||
|
||||
- **DataProvider**: Central class that manages data collection, processing, and distribution.
|
||||
- **MarketTick**: Data structure for standardized market tick data.
|
||||
- **DataSubscriber**: Interface for components that subscribe to market data.
|
||||
- **PivotBounds**: Data structure for pivot-based normalization bounds.
|
||||
|
||||
#### Implementation Details
|
||||
|
||||
The DataProvider class will:
|
||||
- Collect data from multiple sources (Binance, MEXC)
|
||||
- Support multiple timeframes (1s, 1m, 1h, 1d)
|
||||
- Support multiple symbols (ETH, BTC)
|
||||
- Calculate technical indicators
|
||||
- Identify pivot points
|
||||
- Normalize data
|
||||
- Distribute data to subscribers
|
||||
- Calculate any other algoritmic manipulations/calculations on the data
|
||||
- Cache up to 3x the model inputs (300 ticks OHLCV, etc) data so we can do a proper backtesting in up to 2x time in the future
|
||||
|
||||
Based on the existing implementation in `core/data_provider.py`, we'll enhance it to:
|
||||
- Improve pivot point calculation using reccursive Williams Market Structure
|
||||
- Optimize data caching for better performance
|
||||
- Enhance real-time data streaming
|
||||
- Implement better error handling and fallback mechanisms
|
||||
|
||||
### BASE FOR ALL MODELS ###
|
||||
- ***INPUTS***: COB+OHCLV data frame as described:
|
||||
- OHCLV: 300 frames of (1s, 1m, 1h, 1d) ETH + 300s of 1s BTC
|
||||
- COB: for each 1s OHCLV we have +- 20 buckets of COB ammounts in USD
|
||||
- 1,5,15 and 60s MA of the COB imbalance counting +- 5 COB buckets
|
||||
- ***OUTPUTS***:
|
||||
- suggested trade action (BUY/SELL/HOLD). Paired with confidence
|
||||
- immediate price movement drection vector (-1: vertical down, 1: vertical up, 0: horizontal) - linear; with it's own confidence
|
||||
|
||||
# Standardized input for all models:
|
||||
{
|
||||
'primary_symbol': 'ETH/USDT',
|
||||
'reference_symbol': 'BTC/USDT',
|
||||
'eth_data': {'ETH_1s': df, 'ETH_1m': df, 'ETH_1h': df, 'ETH_1d': df},
|
||||
'btc_data': {'BTC_1s': df},
|
||||
'current_prices': {'ETH': price, 'BTC': price},
|
||||
'data_completeness': {...}
|
||||
}
|
||||
|
||||
### 2. CNN Model
|
||||
|
||||
The CNN Model is responsible for analyzing patterns in market data and predicting pivot points across multiple timeframes.
|
||||
|
||||
#### Key Classes and Interfaces
|
||||
|
||||
- **CNNModel**: Main class for the CNN model.
|
||||
- **PivotPointPredictor**: Interface for predicting pivot points.
|
||||
- **CNNTrainer**: Class for training the CNN model.
|
||||
- ***INPUTS***: COB+OHCLV+Old Pivots (5 levels of pivots)
|
||||
- ***OUTPUTS***: next pivot point for each level as price-time vector. (can be plotted as trend line) + suggested trade action (BUY/SELL)
|
||||
|
||||
#### Implementation Details
|
||||
|
||||
The CNN Model will:
|
||||
- Accept multi-timeframe and multi-symbol data as input
|
||||
- Output predicted pivot points for each timeframe (1s, 1m, 1h, 1d)
|
||||
- Provide confidence scores for each prediction
|
||||
- Make hidden layer states available for the RL model
|
||||
|
||||
Architecture:
|
||||
- Input layer: Multi-channel input for different timeframes and symbols
|
||||
- Convolutional layers: Extract patterns from time series data
|
||||
- LSTM/GRU layers: Capture temporal dependencies
|
||||
- Attention mechanism: Focus on relevant parts of the input
|
||||
- Output layer: Predict pivot points and confidence scores
|
||||
|
||||
Training:
|
||||
- Use programmatically calculated pivot points as ground truth
|
||||
- Train on historical data
|
||||
- Update model when new pivot points are detected
|
||||
- Use backpropagation to optimize weights
|
||||
|
||||
### 3. RL Model
|
||||
|
||||
The RL Model is responsible for learning optimal trading strategies based on market data and CNN predictions.
|
||||
|
||||
#### Key Classes and Interfaces
|
||||
|
||||
- **RLModel**: Main class for the RL model.
|
||||
- **TradingActionGenerator**: Interface for generating trading actions.
|
||||
- **RLTrainer**: Class for training the RL model.
|
||||
|
||||
#### Implementation Details
|
||||
|
||||
The RL Model will:
|
||||
- Accept market data, CNN model predictions (output), and CNN hidden layer states as input
|
||||
- Output trading action recommendations (buy/sell)
|
||||
- Provide confidence scores for each action
|
||||
- Learn from past experiences to adapt to the current market environment
|
||||
|
||||
Architecture:
|
||||
- State representation: Market data, CNN model predictions (output), CNN hidden layer states
|
||||
- Action space: Buy, Sell
|
||||
- Reward function: PnL, risk-adjusted returns
|
||||
- Policy network: Deep neural network
|
||||
- Value network: Estimate expected returns
|
||||
|
||||
Training:
|
||||
- Use reinforcement learning algorithms (DQN, PPO, A3C)
|
||||
- Train on historical data
|
||||
- Update model based on trading outcomes
|
||||
- Use experience replay to improve sample efficiency
|
||||
|
||||
### 4. Orchestrator
|
||||
|
||||
The Orchestrator serves as the central coordination hub of the multi-modal trading system, responsible for data subscription management, model inference coordination, output storage, training pipeline orchestration, and inference-training feedback loop management.
|
||||
|
||||
#### Key Classes and Interfaces
|
||||
|
||||
- **Orchestrator**: Main class for the orchestrator.
|
||||
- **DataSubscriptionManager**: Manages subscriptions to multiple data streams with different refresh rates.
|
||||
- **ModelInferenceCoordinator**: Coordinates inference across all models.
|
||||
- **ModelOutputStore**: Stores and manages model outputs for cross-model feeding.
|
||||
- **TrainingPipelineManager**: Manages training pipelines for all models.
|
||||
- **DecisionMaker**: Interface for making trading decisions.
|
||||
- **MoEGateway**: Mixture of Experts gateway for model integration.
|
||||
|
||||
#### Core Responsibilities
|
||||
|
||||
##### 1. Data Subscription and Management
|
||||
|
||||
The Orchestrator subscribes to the Data Provider and manages multiple data streams with varying refresh rates:
|
||||
|
||||
- **10Hz COB (Cumulative Order Book) Data**: High-frequency order book updates for real-time market depth analysis
|
||||
- **OHLCV Data**: Traditional candlestick data at multiple timeframes (1s, 1m, 1h, 1d)
|
||||
- **Market Tick Data**: Individual trade executions and price movements
|
||||
- **Technical Indicators**: Calculated indicators that update at different frequencies
|
||||
- **Pivot Points**: Market structure analysis data
|
||||
|
||||
**Data Stream Management**:
|
||||
- Maintains separate buffers for each data type with appropriate retention policies
|
||||
- Ensures thread-safe access to data streams from multiple models
|
||||
- Implements intelligent caching to serve "last updated" data efficiently
|
||||
- Maintains full base dataframe that stays current for any model requesting data
|
||||
- Handles data synchronization across different refresh rates
|
||||
|
||||
**Enhanced 1s Timeseries Data Combination**:
|
||||
- Combines OHLCV data with COB (Cumulative Order Book) data for 1s timeframes
|
||||
- Implements price bucket aggregation: ±20 buckets around current price
|
||||
- ETH: $1 bucket size (e.g., $3000-$3040 range = 40 buckets) when current price is 3020
|
||||
- BTC: $10 bucket size (e.g., $50000-$50400 range = 40 buckets) when price is 50200
|
||||
- Creates unified base data input that includes:
|
||||
- Traditional OHLCV metrics (Open, High, Low, Close, Volume)
|
||||
- Order book depth and liquidity at each price level
|
||||
- Bid/ask imbalances for the +-5 buckets with Moving Averages for 5,15, and 60s
|
||||
- Volume-weighted average prices within buckets
|
||||
- Order flow dynamics and market microstructure data
|
||||
|
||||
##### 2. Model Inference Coordination
|
||||
|
||||
The Orchestrator coordinates inference across all models in the system:
|
||||
|
||||
**Inference Pipeline**:
|
||||
- Triggers model inference when relevant data updates occur
|
||||
- Manages inference scheduling based on data availability and model requirements
|
||||
- Coordinates parallel inference execution for independent models
|
||||
- Handles model dependencies (e.g., RL model waiting for CNN hidden states)
|
||||
|
||||
**Model Input Management**:
|
||||
- Assembles appropriate input data for each model based on their requirements
|
||||
- Ensures models receive the most current data available at inference time
|
||||
- Manages feature engineering and data preprocessing for each model
|
||||
- Handles different input formats and requirements across models
|
||||
|
||||
##### 3. Model Output Storage and Cross-Feeding
|
||||
|
||||
The Orchestrator maintains a centralized store for all model outputs and manages cross-model data feeding:
|
||||
|
||||
**Output Storage**:
|
||||
- Stores CNN predictions, confidence scores, and hidden layer states
|
||||
- Stores RL action recommendations and value estimates
|
||||
- Stores outputs from all models in extensible format supporting future models (LSTM, Transformer, etc.)
|
||||
- Maintains historical output sequences for temporal analysis
|
||||
- Implements efficient retrieval mechanisms for real-time access
|
||||
- Uses standardized ModelOutput format for easy extension and cross-model compatibility
|
||||
|
||||
**Cross-Model Feeding**:
|
||||
- Feeds CNN hidden layer states into RL model inputs
|
||||
- Provides CNN predictions as context for RL decision-making
|
||||
- Includes "last predictions" from each available model as part of base data input
|
||||
- Stores model outputs that become inputs for subsequent inference cycles
|
||||
- Manages circular dependencies and feedback loops between models
|
||||
- Supports dynamic model addition without requiring system architecture changes
|
||||
|
||||
##### 4. Training Pipeline Management
|
||||
|
||||
The Orchestrator coordinates training for all models by managing the prediction-result feedback loop:
|
||||
|
||||
**Training Coordination**:
|
||||
- Calls each model's training pipeline when new inference results are available
|
||||
- Provides previous predictions alongside new results for supervised learning
|
||||
- Manages training data collection and labeling
|
||||
- Coordinates online learning updates based on real-time performance
|
||||
|
||||
**Training Data Management**:
|
||||
- Maintains training datasets with prediction-result pairs
|
||||
- Implements data quality checks and filtering
|
||||
- Manages training data retention and archival policies
|
||||
- Provides training data statistics and monitoring
|
||||
|
||||
**Performance Tracking**:
|
||||
- Tracks prediction accuracy for each model over time
|
||||
- Monitors model performance degradation and triggers retraining
|
||||
- Maintains performance metrics for model comparison and selection
|
||||
|
||||
**Training progress and checkpoints persistance**
|
||||
- it uses the checkpoint manager to store check points of each model over time as training progresses and we have improvements
|
||||
- checkpoint manager has capability to ensure only top 5 to 10 best checkpoints are stored for each model deleting the least performant ones. it stores metadata along the CPs to decide the performance
|
||||
- we automatically load the best CP at startup if we have stored ones
|
||||
|
||||
##### 5. Inference Data Validation and Storage
|
||||
|
||||
The Orchestrator implements comprehensive inference data validation and persistent storage:
|
||||
|
||||
**Input Data Validation**:
|
||||
- Validates complete OHLCV dataframes for all required timeframes before inference
|
||||
- Checks input data dimensions against model requirements
|
||||
- Logs missing components and prevents prediction on incomplete data
|
||||
- Raises validation errors with specific details about expected vs actual dimensions
|
||||
|
||||
**Inference History Storage**:
|
||||
- Stores complete input data packages with each prediction in persistent storage
|
||||
- Includes timestamp, symbol, input features, prediction outputs, confidence scores, and model internal states
|
||||
- Maintains compressed storage to minimize footprint while preserving accessibility
|
||||
- Implements efficient query mechanisms by symbol, timeframe, and date range
|
||||
|
||||
**Storage Management**:
|
||||
- Applies configurable retention policies to manage storage limits
|
||||
- Archives or removes oldest entries when limits are reached
|
||||
- Prioritizes keeping most recent and valuable training examples during storage pressure
|
||||
- Provides data completeness metrics and validation results in logs
|
||||
|
||||
##### 6. Inference-Training Feedback Loop
|
||||
|
||||
The Orchestrator manages the continuous learning cycle through inference-training feedback:
|
||||
|
||||
**Prediction Outcome Evaluation**:
|
||||
- Evaluates prediction accuracy against actual price movements after sufficient time has passed
|
||||
- Creates training examples using stored inference data paired with actual market outcomes
|
||||
- Feeds prediction-result pairs back to respective models for learning
|
||||
|
||||
**Adaptive Learning Signals**:
|
||||
- Provides positive reinforcement signals for accurate predictions
|
||||
- Delivers corrective training signals for inaccurate predictions to help models learn from mistakes
|
||||
- Retrieves last inference data for each model to compare predictions against actual outcomes
|
||||
|
||||
**Continuous Improvement Tracking**:
|
||||
- Tracks and reports accuracy improvements or degradations over time
|
||||
- Monitors model learning progress through the feedback loop
|
||||
- Alerts administrators when data flow issues are detected with specific error details and remediation suggestions
|
||||
|
||||
##### 5. Decision Making and Trading Actions
|
||||
|
||||
Beyond coordination, the Orchestrator makes final trading decisions:
|
||||
|
||||
**Decision Integration**:
|
||||
- Combines outputs from CNN and RL models using Mixture of Experts approach
|
||||
- Applies confidence-based filtering to avoid uncertain trades
|
||||
- Implements configurable thresholds for buy/sell decisions
|
||||
- Considers market conditions and risk parameters
|
||||
|
||||
#### Implementation Details
|
||||
|
||||
**Architecture**:
|
||||
```python
|
||||
class Orchestrator:
|
||||
def __init__(self):
|
||||
self.data_subscription_manager = DataSubscriptionManager()
|
||||
self.model_inference_coordinator = ModelInferenceCoordinator()
|
||||
self.model_output_store = ModelOutputStore()
|
||||
self.training_pipeline_manager = TrainingPipelineManager()
|
||||
self.decision_maker = DecisionMaker()
|
||||
self.moe_gateway = MoEGateway()
|
||||
|
||||
async def run(self):
|
||||
# Subscribe to data streams
|
||||
await self.data_subscription_manager.subscribe_to_data_provider()
|
||||
|
||||
# Start inference coordination loop
|
||||
await self.model_inference_coordinator.start()
|
||||
|
||||
# Start training pipeline management
|
||||
await self.training_pipeline_manager.start()
|
||||
```
|
||||
|
||||
**Data Flow Management**:
|
||||
- Implements event-driven architecture for data updates
|
||||
- Uses async/await patterns for non-blocking operations
|
||||
- Maintains data freshness timestamps for each stream
|
||||
- Implements backpressure handling for high-frequency data
|
||||
|
||||
**Model Coordination**:
|
||||
- Manages model lifecycle (loading, inference, training, updating)
|
||||
- Implements model versioning and rollback capabilities
|
||||
- Handles model failures and fallback mechanisms
|
||||
- Provides model performance monitoring and alerting
|
||||
|
||||
**Training Integration**:
|
||||
- Implements incremental learning strategies
|
||||
- Manages training batch composition and scheduling
|
||||
- Provides training progress monitoring and control
|
||||
- Handles training failures and recovery
|
||||
|
||||
### 5. Trading Executor
|
||||
|
||||
The Trading Executor is responsible for executing trading actions through brokerage APIs.
|
||||
|
||||
#### Key Classes and Interfaces
|
||||
|
||||
- **TradingExecutor**: Main class for the trading executor.
|
||||
- **BrokerageAPI**: Interface for interacting with brokerages.
|
||||
- **OrderManager**: Class for managing orders.
|
||||
|
||||
#### Implementation Details
|
||||
|
||||
The Trading Executor will:
|
||||
- Accept trading actions from the orchestrator
|
||||
- Execute orders through brokerage APIs
|
||||
- Manage order lifecycle
|
||||
- Handle errors and retries
|
||||
- Provide feedback on order execution
|
||||
|
||||
Supported brokerages:
|
||||
- MEXC
|
||||
- Binance
|
||||
- Bybit (future extension)
|
||||
|
||||
Order types:
|
||||
- Market orders
|
||||
- Limit orders
|
||||
- Stop-loss orders
|
||||
|
||||
### 6. Risk Manager
|
||||
|
||||
The Risk Manager is responsible for implementing risk management features like stop-loss and position sizing.
|
||||
|
||||
#### Key Classes and Interfaces
|
||||
|
||||
- **RiskManager**: Main class for the risk manager.
|
||||
- **StopLossManager**: Class for managing stop-loss orders.
|
||||
- **PositionSizer**: Class for determining position sizes.
|
||||
|
||||
#### Implementation Details
|
||||
|
||||
The Risk Manager will:
|
||||
- Implement configurable stop-loss functionality
|
||||
- Implement configurable position sizing based on risk parameters
|
||||
- Implement configurable maximum drawdown limits
|
||||
- Provide real-time risk metrics
|
||||
- Provide alerts for high-risk situations
|
||||
|
||||
Risk parameters:
|
||||
- Maximum position size
|
||||
- Maximum drawdown
|
||||
- Risk per trade
|
||||
- Maximum leverage
|
||||
|
||||
### 7. Dashboard
|
||||
|
||||
The Dashboard provides a user interface for monitoring and controlling the system.
|
||||
|
||||
#### Key Classes and Interfaces
|
||||
|
||||
- **Dashboard**: Main class for the dashboard.
|
||||
- **ChartManager**: Class for managing charts.
|
||||
- **ControlPanel**: Class for managing controls.
|
||||
|
||||
#### Implementation Details
|
||||
|
||||
The Dashboard will:
|
||||
- Display real-time market data for all symbols and timeframes
|
||||
- Display OHLCV charts for all timeframes
|
||||
- Display CNN pivot point predictions and confidence levels
|
||||
- Display RL and orchestrator trading actions and confidence levels
|
||||
- Display system status and model performance metrics
|
||||
- Provide start/stop toggles for all system processes
|
||||
- Provide sliders to adjust buy/sell thresholds for the orchestrator
|
||||
|
||||
Implementation:
|
||||
- Web-based dashboard using Flask/Dash
|
||||
- Real-time updates using WebSockets
|
||||
- Interactive charts using Plotly
|
||||
- Server-side processing for all models
|
||||
|
||||
## Data Models
|
||||
|
||||
### Market Data
|
||||
|
||||
```python
|
||||
@dataclass
|
||||
class MarketTick:
|
||||
symbol: str
|
||||
timestamp: datetime
|
||||
price: float
|
||||
volume: float
|
||||
quantity: float
|
||||
side: str # 'buy' or 'sell'
|
||||
trade_id: str
|
||||
is_buyer_maker: bool
|
||||
raw_data: Dict[str, Any] = field(default_factory=dict)
|
||||
```
|
||||
|
||||
### OHLCV Data
|
||||
|
||||
```python
|
||||
@dataclass
|
||||
class OHLCVBar:
|
||||
symbol: str
|
||||
timestamp: datetime
|
||||
open: float
|
||||
high: float
|
||||
low: float
|
||||
close: float
|
||||
volume: float
|
||||
timeframe: str
|
||||
indicators: Dict[str, float] = field(default_factory=dict)
|
||||
```
|
||||
|
||||
### Pivot Points
|
||||
|
||||
```python
|
||||
@dataclass
|
||||
class PivotPoint:
|
||||
symbol: str
|
||||
timestamp: datetime
|
||||
price: float
|
||||
type: str # 'high' or 'low'
|
||||
level: int # Pivot level (1, 2, 3, etc.)
|
||||
confidence: float = 1.0
|
||||
```
|
||||
|
||||
### Trading Actions
|
||||
|
||||
```python
|
||||
@dataclass
|
||||
class TradingAction:
|
||||
symbol: str
|
||||
timestamp: datetime
|
||||
action: str # 'buy' or 'sell'
|
||||
confidence: float
|
||||
source: str # 'rl', 'cnn', 'orchestrator'
|
||||
price: Optional[float] = None
|
||||
quantity: Optional[float] = None
|
||||
reason: Optional[str] = None
|
||||
```
|
||||
|
||||
### Model Predictions
|
||||
|
||||
```python
|
||||
@dataclass
|
||||
class ModelOutput:
|
||||
"""Extensible model output format supporting all model types"""
|
||||
model_type: str # 'cnn', 'rl', 'lstm', 'transformer', 'orchestrator'
|
||||
model_name: str # Specific model identifier
|
||||
symbol: str
|
||||
timestamp: datetime
|
||||
confidence: float
|
||||
predictions: Dict[str, Any] # Model-specific predictions
|
||||
hidden_states: Optional[Dict[str, Any]] = None # For cross-model feeding
|
||||
metadata: Dict[str, Any] = field(default_factory=dict) # Additional info
|
||||
```
|
||||
|
||||
```python
|
||||
@dataclass
|
||||
class CNNPrediction:
|
||||
symbol: str
|
||||
timestamp: datetime
|
||||
pivot_points: List[PivotPoint]
|
||||
hidden_states: Dict[str, Any]
|
||||
confidence: float
|
||||
```
|
||||
|
||||
```python
|
||||
@dataclass
|
||||
class RLPrediction:
|
||||
symbol: str
|
||||
timestamp: datetime
|
||||
action: str # 'buy' or 'sell'
|
||||
confidence: float
|
||||
expected_reward: float
|
||||
```
|
||||
|
||||
### Enhanced Base Data Input
|
||||
|
||||
```python
|
||||
@dataclass
|
||||
class BaseDataInput:
|
||||
"""Unified base data input for all models"""
|
||||
symbol: str
|
||||
timestamp: datetime
|
||||
ohlcv_data: Dict[str, OHLCVBar] # Multi-timeframe OHLCV
|
||||
cob_data: Optional[Dict[str, float]] = None # COB buckets for 1s timeframe
|
||||
technical_indicators: Dict[str, float] = field(default_factory=dict)
|
||||
pivot_points: List[PivotPoint] = field(default_factory=list)
|
||||
last_predictions: Dict[str, ModelOutput] = field(default_factory=dict) # From all models
|
||||
market_microstructure: Dict[str, Any] = field(default_factory=dict) # Order flow, etc.
|
||||
```
|
||||
|
||||
### COB Data Structure
|
||||
|
||||
```python
|
||||
@dataclass
|
||||
class COBData:
|
||||
"""Cumulative Order Book data for price buckets"""
|
||||
symbol: str
|
||||
timestamp: datetime
|
||||
current_price: float
|
||||
bucket_size: float # $1 for ETH, $10 for BTC
|
||||
price_buckets: Dict[float, Dict[str, float]] # price -> {bid_volume, ask_volume, etc.}
|
||||
bid_ask_imbalance: Dict[float, float] # price -> imbalance ratio
|
||||
volume_weighted_prices: Dict[float, float] # price -> VWAP within bucket
|
||||
order_flow_metrics: Dict[str, float] # Various order flow indicators
|
||||
```
|
||||
|
||||
### Data Collection Errors
|
||||
|
||||
- Implement retry mechanisms for API failures
|
||||
- Use fallback data sources when primary sources are unavailable
|
||||
- Log all errors with detailed information
|
||||
- Notify users through the dashboard
|
||||
|
||||
### Model Errors
|
||||
|
||||
- Implement model validation before deployment
|
||||
- Use fallback models when primary models fail
|
||||
- Log all errors with detailed information
|
||||
- Notify users through the dashboard
|
||||
|
||||
### Trading Errors
|
||||
|
||||
- Implement order validation before submission
|
||||
- Use retry mechanisms for order failures
|
||||
- Implement circuit breakers for extreme market conditions
|
||||
- Log all errors with detailed information
|
||||
- Notify users through the dashboard
|
||||
|
||||
## Testing Strategy
|
||||
|
||||
### Unit Testing
|
||||
|
||||
- Test individual components in isolation
|
||||
- Use mock objects for dependencies
|
||||
- Focus on edge cases and error handling
|
||||
|
||||
### Integration Testing
|
||||
|
||||
- Test interactions between components
|
||||
- Use real data for testing
|
||||
- Focus on data flow and error propagation
|
||||
|
||||
### System Testing
|
||||
|
||||
- Test the entire system end-to-end
|
||||
- Use real data for testing
|
||||
- Focus on performance and reliability
|
||||
|
||||
### Backtesting
|
||||
|
||||
- Test trading strategies on historical data
|
||||
- Measure performance metrics (PnL, Sharpe ratio, etc.)
|
||||
- Compare against benchmarks
|
||||
|
||||
### Live Testing
|
||||
|
||||
- Test the system in a live environment with small position sizes
|
||||
- Monitor performance and stability
|
||||
- Gradually increase position sizes as confidence grows
|
||||
|
||||
## Implementation Plan
|
||||
|
||||
The implementation will follow a phased approach:
|
||||
|
||||
1. **Phase 1: Data Provider**
|
||||
- Implement the enhanced data provider
|
||||
- Implement pivot point calculation
|
||||
- Implement technical indicator calculation
|
||||
- Implement data normalization
|
||||
|
||||
2. **Phase 2: CNN Model**
|
||||
- Implement the CNN model architecture
|
||||
- Implement the training pipeline
|
||||
- Implement the inference pipeline
|
||||
- Implement the pivot point prediction
|
||||
|
||||
3. **Phase 3: RL Model**
|
||||
- Implement the RL model architecture
|
||||
- Implement the training pipeline
|
||||
- Implement the inference pipeline
|
||||
- Implement the trading action generation
|
||||
|
||||
4. **Phase 4: Orchestrator**
|
||||
- Implement the orchestrator architecture
|
||||
- Implement the decision-making logic
|
||||
- Implement the MoE gateway
|
||||
- Implement the confidence-based filtering
|
||||
|
||||
5. **Phase 5: Trading Executor**
|
||||
- Implement the trading executor
|
||||
- Implement the brokerage API integrations
|
||||
- Implement the order management
|
||||
- Implement the error handling
|
||||
|
||||
6. **Phase 6: Risk Manager**
|
||||
- Implement the risk manager
|
||||
- Implement the stop-loss functionality
|
||||
- Implement the position sizing
|
||||
- Implement the risk metrics
|
||||
|
||||
7. **Phase 7: Dashboard**
|
||||
- Implement the dashboard UI
|
||||
- Implement the chart management
|
||||
- Implement the control panel
|
||||
- Implement the real-time updates
|
||||
|
||||
8. **Phase 8: Integration and Testing**
|
||||
- Integrate all components
|
||||
- Implement comprehensive testing
|
||||
- Fix bugs and optimize performance
|
||||
- Deploy to production
|
||||
|
||||
## Monitoring and Visualization
|
||||
|
||||
### TensorBoard Integration (Future Enhancement)
|
||||
|
||||
A comprehensive TensorBoard integration has been designed to provide detailed training visualization and monitoring capabilities:
|
||||
|
||||
#### Features
|
||||
- **Training Metrics Visualization**: Real-time tracking of model losses, rewards, and performance metrics
|
||||
- **Feature Distribution Analysis**: Histograms and statistics of input features to validate data quality
|
||||
- **State Quality Monitoring**: Tracking of comprehensive state building (13,400 features) success rates
|
||||
- **Reward Component Analysis**: Detailed breakdown of reward calculations including PnL, confidence, volatility, and order flow
|
||||
- **Model Performance Comparison**: Side-by-side comparison of CNN, RL, and orchestrator performance
|
||||
|
||||
#### Implementation Status
|
||||
- **Completed**: TensorBoardLogger utility class with comprehensive logging methods
|
||||
- **Completed**: Integration points in enhanced_rl_training_integration.py
|
||||
- **Completed**: Enhanced run_tensorboard.py with improved visualization options
|
||||
- **Status**: Ready for deployment when system stability is achieved
|
||||
|
||||
#### Usage
|
||||
```bash
|
||||
# Start TensorBoard dashboard
|
||||
python run_tensorboard.py
|
||||
|
||||
# Access at http://localhost:6006
|
||||
# View training metrics, feature distributions, and model performance
|
||||
```
|
||||
|
||||
#### Benefits
|
||||
- Real-time validation of training process
|
||||
- Early detection of training issues
|
||||
- Feature importance analysis
|
||||
- Model performance comparison
|
||||
- Historical training progress tracking
|
||||
|
||||
**Note**: TensorBoard integration is currently deprioritized in favor of system stability and core model improvements. It will be activated once the core training system is stable and performing optimally.
|
||||
|
||||
## Conclusion
|
||||
|
||||
This design document outlines the architecture, components, data flow, and implementation details for the Multi-Modal Trading System. The system is designed to be modular, extensible, and robust, with a focus on performance, reliability, and user experience.
|
||||
|
||||
The implementation will follow a phased approach, with each phase building on the previous one. The system will be thoroughly tested at each phase to ensure that it meets the requirements and performs as expected.
|
||||
|
||||
The final system will provide traders with a powerful tool for analyzing market data, identifying trading opportunities, and executing trades with confidence.
|
||||
175
.kiro/specs/1.multi-modal-trading-system/requirements.md
Normal file
175
.kiro/specs/1.multi-modal-trading-system/requirements.md
Normal file
@@ -0,0 +1,175 @@
|
||||
# Requirements Document
|
||||
|
||||
## Introduction
|
||||
|
||||
The Multi-Modal Trading System is an advanced algorithmic trading platform that combines Convolutional Neural Networks (CNN) and Reinforcement Learning (RL) models orchestrated by a decision-making module. The system processes multi-timeframe and multi-symbol market data (primarily ETH and BTC) to generate trading actions. The system is designed to adapt to current market conditions through continuous learning from past experiences, with the CNN module trained on historical data to predict pivot points and the RL module optimizing trading decisions based on these predictions and market data.
|
||||
|
||||
## Requirements
|
||||
|
||||
### Requirement 1: Data Collection and Processing
|
||||
|
||||
**User Story:** As a trader, I want the system to collect and process multi-timeframe and multi-symbol market data, so that the models have comprehensive market information for making accurate trading decisions.
|
||||
|
||||
#### Acceptance Criteria
|
||||
|
||||
0. NEVER USE GENERATED/SYNTHETIC DATA or mock implementations and UI. If somethings is not implemented yet, it should be obvious.
|
||||
1. WHEN the system starts THEN it SHALL collect and process data for both ETH and BTC symbols.
|
||||
2. WHEN collecting data THEN the system SHALL store the following for the primary symbol (ETH):
|
||||
- 300 seconds of raw tick data - price and COB snapshot for all prices +- 1% on fine reslolution buckets (1$ for ETH, 10$ for BTC)
|
||||
- 300 seconds of 1-second OHLCV data + 1s aggregated COB data
|
||||
- 300 bars of OHLCV + indicators for each timeframe (1s, 1m, 1h, 1d)
|
||||
3. WHEN collecting data THEN the system SHALL store similar data for the reference symbol (BTC).
|
||||
4. WHEN processing data THEN the system SHALL calculate standard technical indicators for all timeframes.
|
||||
5. WHEN processing data THEN the system SHALL calculate pivot points for all timeframes according to the specified methodology.
|
||||
6. WHEN new data arrives THEN the system SHALL update its data cache in real-time.
|
||||
7. IF tick data is not available THEN the system SHALL substitute with the lowest available timeframe data.
|
||||
8. WHEN normalizing data THEN the system SHALL normalize to the max and min of the highest timeframe to maintain relationships between different timeframes.
|
||||
9. data is cached for longer (let's start with double the model inputs so 600 bars) to support performing backtesting when we know the current predictions outcomes so we can generate test cases.
|
||||
10. In general all models have access to the whole data we collect in a central data provider implementation. only some are specialized. All models should also take as input the last output of evey other model (also cached in the data provider). there should be a room for adding more models in the other models data input so we can extend the system without having to loose existing models and trained W&B
|
||||
|
||||
### Requirement 2: CNN Model Implementation
|
||||
|
||||
**User Story:** As a trader, I want the system to implement a CNN model that can identify patterns and predict pivot points across multiple timeframes, so that I can anticipate market direction changes.
|
||||
|
||||
#### Acceptance Criteria
|
||||
|
||||
1. WHEN the CNN model is initialized THEN it SHALL accept multi-timeframe and multi-symbol data as input.
|
||||
2. WHEN processing input data THEN the CNN model SHALL output predicted pivot points for each timeframe (1s, 1m, 1h, 1d).
|
||||
3. WHEN predicting pivot points THEN the CNN model SHALL provide both the predicted pivot point value and the timestamp when it is expected to occur.
|
||||
4. WHEN a pivot point is detected THEN the system SHALL trigger a training round for the CNN model using historical data.
|
||||
5. WHEN training the CNN model THEN the system SHALL use programmatically calculated pivot points from historical data as ground truth.
|
||||
6. WHEN outputting predictions THEN the CNN model SHALL include a confidence score for each prediction.
|
||||
7. WHEN calculating pivot points THEN the system SHALL implement both standard pivot points and the recursive Williams market structure pivot points as described.
|
||||
8. WHEN processing data THEN the CNN model SHALL make available its hidden layer states for use by the RL model.
|
||||
|
||||
### Requirement 3: RL Model Implementation
|
||||
|
||||
**User Story:** As a trader, I want the system to implement an RL model that can learn optimal trading strategies based on market data and CNN predictions, so that the system can adapt to changing market conditions.
|
||||
|
||||
#### Acceptance Criteria
|
||||
|
||||
1. WHEN the RL model is initialized THEN it SHALL accept market data, CNN predictions, and CNN hidden layer states as input.
|
||||
2. WHEN processing input data THEN the RL model SHALL output trading action recommendations (buy/sell).
|
||||
3. WHEN evaluating trading actions THEN the RL model SHALL learn from past experiences to adapt to the current market environment.
|
||||
4. WHEN making decisions THEN the RL model SHALL consider the confidence levels of CNN predictions.
|
||||
5. WHEN uncertain about market direction THEN the RL model SHALL learn to avoid entering positions.
|
||||
6. WHEN training the RL model THEN the system SHALL use a reward function that incentivizes high risk/reward setups.
|
||||
7. WHEN outputting trading actions THEN the RL model SHALL provide a confidence score for each action.
|
||||
8. WHEN a trading action is executed THEN the system SHALL store the input data for future training.
|
||||
|
||||
### Requirement 4: Orchestrator Implementation
|
||||
|
||||
**User Story:** As a trader, I want the system to implement an orchestrator that can make final trading decisions based on inputs from both CNN and RL models, so that the system can make more balanced and informed trading decisions.
|
||||
|
||||
#### Acceptance Criteria
|
||||
|
||||
1. WHEN the orchestrator is initialized THEN it SHALL accept inputs from both CNN and RL models.
|
||||
2. WHEN processing model inputs THEN the orchestrator SHALL output final trading actions (buy/sell).
|
||||
3. WHEN making decisions THEN the orchestrator SHALL consider the confidence levels of both CNN and RL models.
|
||||
4. WHEN uncertain about market direction THEN the orchestrator SHALL learn to avoid entering positions.
|
||||
5. WHEN implementing the orchestrator THEN the system SHALL use a Mixture of Experts (MoE) approach to allow for future model integration.
|
||||
6. WHEN outputting trading actions THEN the orchestrator SHALL provide a confidence score for each action.
|
||||
7. WHEN a trading action is executed THEN the system SHALL store the input data for future training.
|
||||
8. WHEN implementing the orchestrator THEN the system SHALL allow for configurable thresholds for entering and exiting positions.
|
||||
|
||||
### Requirement 5: Training Pipeline
|
||||
|
||||
**User Story:** As a developer, I want the system to implement a unified training pipeline for both CNN and RL models, so that the models can be trained efficiently and consistently.
|
||||
|
||||
#### Acceptance Criteria
|
||||
|
||||
1. WHEN training models THEN the system SHALL use a unified data provider to prepare data for all models.
|
||||
2. WHEN a pivot point is detected THEN the system SHALL trigger a training round for the CNN model.
|
||||
3. WHEN training the CNN model THEN the system SHALL use programmatically calculated pivot points from historical data as ground truth.
|
||||
4. WHEN training the RL model THEN the system SHALL use a reward function that incentivizes high risk/reward setups.
|
||||
5. WHEN training models THEN the system SHALL run the training process on the server without requiring the dashboard to be open.
|
||||
6. WHEN training models THEN the system SHALL provide real-time feedback on training progress through the dashboard.
|
||||
7. WHEN training models THEN the system SHALL store model checkpoints for future use.
|
||||
8. WHEN training models THEN the system SHALL provide metrics on model performance.
|
||||
|
||||
### Requirement 6: Dashboard Implementation
|
||||
|
||||
**User Story:** As a trader, I want the system to implement a comprehensive dashboard that displays real-time data, model predictions, and trading actions, so that I can monitor the system's performance and make informed decisions.
|
||||
|
||||
#### Acceptance Criteria
|
||||
|
||||
1. WHEN the dashboard is initialized THEN it SHALL display real-time market data for all symbols and timeframes.
|
||||
2. WHEN displaying market data THEN the dashboard SHALL show OHLCV charts for all timeframes.
|
||||
3. WHEN displaying model predictions THEN the dashboard SHALL show CNN pivot point predictions and confidence levels.
|
||||
4. WHEN displaying trading actions THEN the dashboard SHALL show RL and orchestrator trading actions and confidence levels.
|
||||
5. WHEN displaying system status THEN the dashboard SHALL show training progress and model performance metrics.
|
||||
6. WHEN implementing controls THEN the dashboard SHALL provide start/stop toggles for all system processes.
|
||||
7. WHEN implementing controls THEN the dashboard SHALL provide sliders to adjust buy/sell thresholds for the orchestrator.
|
||||
8. WHEN implementing the dashboard THEN the system SHALL ensure all processes run on the server without requiring the dashboard to be open.
|
||||
|
||||
### Requirement 7: Risk Management
|
||||
|
||||
**User Story:** As a trader, I want the system to implement risk management features, so that I can protect my capital from significant losses.
|
||||
|
||||
#### Acceptance Criteria
|
||||
|
||||
1. WHEN implementing risk management THEN the system SHALL provide configurable stop-loss functionality.
|
||||
2. WHEN a stop-loss is triggered THEN the system SHALL automatically close the position.
|
||||
3. WHEN implementing risk management THEN the system SHALL provide configurable position sizing based on risk parameters.
|
||||
4. WHEN implementing risk management THEN the system SHALL provide configurable maximum drawdown limits.
|
||||
5. WHEN maximum drawdown limits are reached THEN the system SHALL automatically stop trading.
|
||||
6. WHEN implementing risk management THEN the system SHALL provide real-time risk metrics through the dashboard.
|
||||
7. WHEN implementing risk management THEN the system SHALL allow for different risk parameters for different market conditions.
|
||||
8. WHEN implementing risk management THEN the system SHALL provide alerts for high-risk situations.
|
||||
|
||||
### Requirement 8: System Architecture and Integration
|
||||
|
||||
**User Story:** As a developer, I want the system to implement a clean and modular architecture, so that the system is easy to maintain and extend.
|
||||
|
||||
#### Acceptance Criteria
|
||||
|
||||
1. WHEN implementing the system architecture THEN the system SHALL use a unified data provider to prepare data for all models.
|
||||
2. WHEN implementing the system architecture THEN the system SHALL use a modular approach to allow for easy extension.
|
||||
3. WHEN implementing the system architecture THEN the system SHALL use a clean separation of concerns between data collection, model training, and trading execution.
|
||||
4. WHEN implementing the system architecture THEN the system SHALL use a unified interface for all models.
|
||||
5. WHEN implementing the system architecture THEN the system SHALL use a unified interface for all data providers.
|
||||
6. WHEN implementing the system architecture THEN the system SHALL use a unified interface for all trading executors.
|
||||
7. WHEN implementing the system architecture THEN the system SHALL use a unified interface for all risk management components.
|
||||
8. WHEN implementing the system architecture THEN the system SHALL use a unified interface for all dashboard components.
|
||||
|
||||
### Requirement 9: Model Inference Data Validation and Storage
|
||||
|
||||
**User Story:** As a trading system developer, I want to ensure that all model predictions include complete input data validation and persistent storage, so that I can verify models receive correct inputs and track their performance over time.
|
||||
|
||||
#### Acceptance Criteria
|
||||
|
||||
1. WHEN a model makes a prediction THEN the system SHALL validate that the input data contains complete OHLCV dataframes for all required timeframes
|
||||
2. WHEN input data is incomplete THEN the system SHALL log the missing components and SHALL NOT proceed with prediction
|
||||
3. WHEN input validation passes THEN the system SHALL store the complete input data package with the prediction in persistent storage
|
||||
4. IF input data dimensions are incorrect THEN the system SHALL raise a validation error with specific details about expected vs actual dimensions
|
||||
5. WHEN a model completes inference THEN the system SHALL store the complete input data, model outputs, confidence scores, and metadata in a persistent inference history
|
||||
6. WHEN storing inference data THEN the system SHALL include timestamp, symbol, input features, prediction outputs, and model internal states
|
||||
7. IF inference history storage fails THEN the system SHALL log the error and continue operation without breaking the prediction flow
|
||||
|
||||
### Requirement 10: Inference-Training Feedback Loop
|
||||
|
||||
**User Story:** As a machine learning engineer, I want the system to automatically train models using their previous inference data compared to actual market outcomes, so that models continuously improve their accuracy through real-world feedback.
|
||||
|
||||
#### Acceptance Criteria
|
||||
|
||||
1. WHEN sufficient time has passed after a prediction THEN the system SHALL evaluate the prediction accuracy against actual price movements
|
||||
2. WHEN a prediction outcome is determined THEN the system SHALL create a training example using the stored inference data and actual outcome
|
||||
3. WHEN training examples are created THEN the system SHALL feed them back to the respective models for learning
|
||||
4. IF the prediction was accurate THEN the system SHALL reinforce the model's decision pathway through positive training signals
|
||||
5. IF the prediction was inaccurate THEN the system SHALL provide corrective training signals to help the model learn from mistakes
|
||||
6. WHEN the system needs training data THEN it SHALL retrieve the last inference data for each model to compare predictions against actual market outcomes
|
||||
7. WHEN models are trained on inference feedback THEN the system SHALL track and report accuracy improvements or degradations over time
|
||||
|
||||
### Requirement 11: Inference History Management and Monitoring
|
||||
|
||||
**User Story:** As a system administrator, I want comprehensive logging and monitoring of the inference-training feedback loop with configurable retention policies, so that I can track model learning progress and manage storage efficiently.
|
||||
|
||||
#### Acceptance Criteria
|
||||
|
||||
1. WHEN inference data is stored THEN the system SHALL log the storage operation with data completeness metrics and validation results
|
||||
2. WHEN training occurs based on previous inference THEN the system SHALL log the training outcome and model performance changes
|
||||
3. WHEN the system detects data flow issues THEN it SHALL alert administrators with specific error details and suggested remediation
|
||||
4. WHEN inference history reaches configured limits THEN the system SHALL archive or remove oldest entries based on retention policy
|
||||
5. WHEN storing inference data THEN the system SHALL compress data to minimize storage footprint while maintaining accessibility
|
||||
6. WHEN retrieving historical inference data THEN the system SHALL provide efficient query mechanisms by symbol, timeframe, and date range
|
||||
7. IF storage space is critically low THEN the system SHALL prioritize keeping the most recent and most valuable training examples
|
||||
382
.kiro/specs/1.multi-modal-trading-system/tasks.md
Normal file
382
.kiro/specs/1.multi-modal-trading-system/tasks.md
Normal file
@@ -0,0 +1,382 @@
|
||||
# Implementation Plan
|
||||
|
||||
## Enhanced Data Provider and COB Integration
|
||||
|
||||
- [ ] 1. Enhance the existing DataProvider class with standardized model inputs
|
||||
- Extend the current implementation in core/data_provider.py
|
||||
- Implement standardized COB+OHLCV data frame for all models
|
||||
- Create unified input format: 300 frames OHLCV (1s, 1m, 1h, 1d) ETH + 300s of 1s BTC
|
||||
- Integrate with existing multi_exchange_cob_provider.py for COB data
|
||||
- _Requirements: 1.1, 1.2, 1.3, 1.6_
|
||||
|
||||
- [ ] 1.1. Implement standardized COB+OHLCV data frame for all models
|
||||
- Create BaseDataInput class with standardized format for all models
|
||||
- Implement OHLCV: 300 frames of (1s, 1m, 1h, 1d) ETH + 300s of 1s BTC
|
||||
- Add COB: ±20 buckets of COB amounts in USD for each 1s OHLCV
|
||||
- Include 1s, 5s, 15s, and 60s MA of COB imbalance counting ±5 COB buckets
|
||||
- Ensure all models receive identical input format for consistency
|
||||
- _Requirements: 1.2, 1.3, 8.1_
|
||||
|
||||
- [ ] 1.2. Implement extensible model output storage
|
||||
- Create standardized ModelOutput data structure
|
||||
- Support CNN, RL, LSTM, Transformer, and future model types
|
||||
- Include model-specific predictions and cross-model hidden states
|
||||
- Add metadata support for extensible model information
|
||||
- _Requirements: 1.10, 8.2_
|
||||
|
||||
- [ ] 1.3. Enhance Williams Market Structure pivot point calculation
|
||||
- Extend existing williams_market_structure.py implementation
|
||||
- Improve recursive pivot point calculation accuracy
|
||||
- Add unit tests to verify pivot point detection
|
||||
- Integrate with COB data for enhanced pivot detection
|
||||
- _Requirements: 1.5, 2.7_
|
||||
|
||||
- [-] 1.4. Optimize real-time data streaming with COB integration
|
||||
- Enhance existing WebSocket connections in enhanced_cob_websocket.py
|
||||
- Implement 10Hz COB data streaming alongside OHLCV data
|
||||
- Add data synchronization across different refresh rates
|
||||
- Ensure thread-safe access to multi-rate data streams
|
||||
- _Requirements: 1.6, 8.5_
|
||||
|
||||
- [ ] 1.5. Fix WebSocket COB data processing errors
|
||||
- Fix 'NoneType' object has no attribute 'append' errors in COB data processing
|
||||
- Ensure proper initialization of data structures in MultiExchangeCOBProvider
|
||||
- Add validation and defensive checks before accessing data structures
|
||||
- Implement proper error handling for WebSocket data processing
|
||||
- _Requirements: 1.1, 1.6, 8.5_
|
||||
|
||||
- [ ] 1.6. Enhance error handling in COB data processing
|
||||
- Add validation for incoming WebSocket data
|
||||
- Implement reconnection logic with exponential backoff
|
||||
- Add detailed logging for debugging COB data issues
|
||||
- Ensure system continues operation with last valid data during failures
|
||||
- _Requirements: 1.6, 8.5_
|
||||
|
||||
## Enhanced CNN Model Implementation
|
||||
|
||||
- [ ] 2. Enhance the existing CNN model with standardized inputs/outputs
|
||||
- Extend the current implementation in NN/models/enhanced_cnn.py
|
||||
- Accept standardized COB+OHLCV data frame: 300 frames (1s,1m,1h,1d) ETH + 300s 1s BTC
|
||||
- Include COB ±20 buckets and MA (1s,5s,15s,60s) of COB imbalance ±5 buckets
|
||||
- Output BUY/SELL trading action with confidence scores - _Requirements: 2.1, 2.2, 2.8, 1.10_
|
||||
|
||||
- [x] 2.1. Implement CNN inference with standardized input format
|
||||
- Accept BaseDataInput with standardized COB+OHLCV format
|
||||
- Process 300 frames of multi-timeframe data with COB buckets
|
||||
- Output BUY/SELL recommendations with confidence scores
|
||||
- Make hidden layer states available for cross-model feeding
|
||||
- Optimize inference performance for real-time processing
|
||||
- _Requirements: 2.2, 2.6, 2.8, 4.3_
|
||||
|
||||
- [x] 2.2. Enhance CNN training pipeline with checkpoint management
|
||||
- Integrate with checkpoint manager for training progress persistence
|
||||
- Store top 5-10 best checkpoints based on performance metrics
|
||||
- Automatically load best checkpoint at startup
|
||||
- Implement training triggers based on orchestrator feedback
|
||||
- Store metadata with checkpoints for performance tracking
|
||||
- _Requirements: 2.4, 2.5, 5.2, 5.3, 5.7_
|
||||
|
||||
- [ ] 2.3. Implement CNN model evaluation and checkpoint optimization
|
||||
- Create evaluation methods using standardized input/output format
|
||||
- Implement performance metrics for checkpoint ranking
|
||||
- Add validation against historical trading outcomes
|
||||
- Support automatic checkpoint cleanup (keep only top performers)
|
||||
- Track model improvement over time through checkpoint metadata
|
||||
- _Requirements: 2.5, 5.8, 4.4_
|
||||
|
||||
## Enhanced RL Model Implementation
|
||||
|
||||
- [ ] 3. Enhance the existing RL model with standardized inputs/outputs
|
||||
- Extend the current implementation in NN/models/dqn_agent.py
|
||||
- Accept standardized COB+OHLCV data frame: 300 frames (1s,1m,1h,1d) ETH + 300s 1s BTC
|
||||
- Include COB ±20 buckets and MA (1s,5s,15s,60s) of COB imbalance ±5 buckets
|
||||
- Output BUY/SELL trading action with confidence scores
|
||||
- _Requirements: 3.1, 3.2, 3.7, 1.10_
|
||||
|
||||
- [ ] 3.1. Implement RL inference with standardized input format
|
||||
- Accept BaseDataInput with standardized COB+OHLCV format
|
||||
- Process CNN hidden states and predictions as part of state input
|
||||
- Output BUY/SELL recommendations with confidence scores
|
||||
- Include expected rewards and value estimates in output
|
||||
- Optimize inference performance for real-time processing
|
||||
- _Requirements: 3.2, 3.7, 4.3_
|
||||
|
||||
- [ ] 3.2. Enhance RL training pipeline with checkpoint management
|
||||
- Integrate with checkpoint manager for training progress persistence
|
||||
- Store top 5-10 best checkpoints based on trading performance metrics
|
||||
- Automatically load best checkpoint at startup
|
||||
- Implement experience replay with profitability-based prioritization
|
||||
- Store metadata with checkpoints for performance tracking
|
||||
- _Requirements: 3.3, 3.5, 5.4, 5.7, 4.4_
|
||||
|
||||
- [ ] 3.3. Implement RL model evaluation and checkpoint optimization
|
||||
- Create evaluation methods using standardized input/output format
|
||||
- Implement trading performance metrics for checkpoint ranking
|
||||
- Add validation against historical trading opportunities
|
||||
- Support automatic checkpoint cleanup (keep only top performers)
|
||||
- Track model improvement over time through checkpoint metadata
|
||||
- _Requirements: 3.3, 5.8, 4.4_
|
||||
|
||||
## Enhanced Orchestrator Implementation
|
||||
|
||||
- [ ] 4. Enhance the existing orchestrator with centralized coordination
|
||||
- Extend the current implementation in core/orchestrator.py
|
||||
- Implement DataSubscriptionManager for multi-rate data streams
|
||||
- Add ModelInferenceCoordinator for cross-model coordination
|
||||
- Create ModelOutputStore for extensible model output management
|
||||
- Add TrainingPipelineManager for continuous learning coordination
|
||||
- _Requirements: 4.1, 4.2, 4.5, 8.1_
|
||||
|
||||
- [ ] 4.1. Implement data subscription and management system
|
||||
- Create DataSubscriptionManager class
|
||||
- Subscribe to 10Hz COB data, OHLCV, market ticks, and technical indicators
|
||||
- Implement intelligent caching for "last updated" data serving
|
||||
- Maintain synchronized base dataframe across different refresh rates
|
||||
- Add thread-safe access to multi-rate data streams
|
||||
- _Requirements: 4.1, 1.6, 8.5_
|
||||
|
||||
|
||||
|
||||
|
||||
- [ ] 4.2. Implement model inference coordination
|
||||
- Create ModelInferenceCoordinator class
|
||||
- Trigger model inference based on data availability and requirements
|
||||
- Coordinate parallel inference execution for independent models
|
||||
- Handle model dependencies (e.g., RL waiting for CNN hidden states)
|
||||
- Assemble appropriate input data for each model type
|
||||
- _Requirements: 4.2, 3.1, 2.1_
|
||||
|
||||
- [ ] 4.3. Implement model output storage and cross-feeding
|
||||
- Create ModelOutputStore class using standardized ModelOutput format
|
||||
- Store CNN predictions, confidence scores, and hidden layer states
|
||||
- Store RL action recommendations and value estimates
|
||||
- Support extensible storage for LSTM, Transformer, and future models
|
||||
- Implement cross-model feeding of hidden states and predictions
|
||||
- Include "last predictions" from all models in base data input
|
||||
- _Requirements: 4.3, 1.10, 8.2_
|
||||
|
||||
- [ ] 4.4. Implement training pipeline management
|
||||
- Create TrainingPipelineManager class
|
||||
- Call each model's training pipeline with prediction-result pairs
|
||||
- Manage training data collection and labeling
|
||||
- Coordinate online learning updates based on real-time performance
|
||||
- Track prediction accuracy and trigger retraining when needed
|
||||
- _Requirements: 4.4, 5.2, 5.4, 5.7_
|
||||
|
||||
- [ ] 4.5. Implement enhanced decision-making with MoE
|
||||
- Create enhanced DecisionMaker class
|
||||
- Implement Mixture of Experts approach for model integration
|
||||
- Apply confidence-based filtering to avoid uncertain trades
|
||||
- Support configurable thresholds for buy/sell decisions
|
||||
- Consider market conditions and risk parameters in decisions
|
||||
- _Requirements: 4.5, 4.8, 6.7_
|
||||
|
||||
- [ ] 4.6. Implement extensible model integration architecture
|
||||
- Create MoEGateway class supporting dynamic model addition
|
||||
- Support CNN, RL, LSTM, Transformer model types without architecture changes
|
||||
- Implement model versioning and rollback capabilities
|
||||
- Handle model failures and fallback mechanisms
|
||||
- Provide model performance monitoring and alerting
|
||||
- _Requirements: 4.6, 8.2, 8.3_
|
||||
|
||||
## Model Inference Data Validation and Storage
|
||||
|
||||
- [x] 5. Implement comprehensive inference data validation system
|
||||
|
||||
- Create InferenceDataValidator class for input validation
|
||||
- Validate complete OHLCV dataframes for all required timeframes
|
||||
- Check input data dimensions against model requirements
|
||||
- Log missing components and prevent prediction on incomplete data
|
||||
- _Requirements: 9.1, 9.2, 9.3, 9.4_
|
||||
|
||||
- [ ] 5.1. Implement input data validation for all models
|
||||
- Create validation methods for CNN, RL, and future model inputs
|
||||
- Validate OHLCV data completeness (300 frames for 1s, 1m, 1h, 1d)
|
||||
- Validate COB data structure (±20 buckets, MA calculations)
|
||||
- Raise specific validation errors with expected vs actual dimensions
|
||||
- Ensure validation occurs before any model inference
|
||||
- _Requirements: 9.1, 9.4_
|
||||
|
||||
- [x] 5.2. Implement persistent inference history storage
|
||||
|
||||
|
||||
- Create InferenceHistoryStore class for persistent storage
|
||||
- Store complete input data packages with each prediction
|
||||
- Include timestamp, symbol, input features, prediction outputs, confidence scores
|
||||
- Store model internal states for cross-model feeding
|
||||
- Implement compressed storage to minimize footprint
|
||||
- _Requirements: 9.5, 9.6_
|
||||
|
||||
- [x] 5.3. Implement inference history query and retrieval system
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
- Create efficient query mechanisms by symbol, timeframe, and date range
|
||||
- Implement data retrieval for training pipeline consumption
|
||||
- Add data completeness metrics and validation results in storage
|
||||
- Handle storage failures gracefully without breaking prediction flow
|
||||
- _Requirements: 9.7, 11.6_
|
||||
|
||||
## Inference-Training Feedback Loop Implementation
|
||||
|
||||
- [ ] 6. Implement prediction outcome evaluation system
|
||||
- Create PredictionOutcomeEvaluator class
|
||||
- Evaluate prediction accuracy against actual price movements
|
||||
- Create training examples using stored inference data and actual outcomes
|
||||
- Feed prediction-result pairs back to respective models
|
||||
- _Requirements: 10.1, 10.2, 10.3_
|
||||
|
||||
- [ ] 6.1. Implement adaptive learning signal generation
|
||||
- Create positive reinforcement signals for accurate predictions
|
||||
- Generate corrective training signals for inaccurate predictions
|
||||
- Retrieve last inference data for each model for outcome comparison
|
||||
- Implement model-specific learning signal formats
|
||||
- _Requirements: 10.4, 10.5, 10.6_
|
||||
|
||||
- [ ] 6.2. Implement continuous improvement tracking
|
||||
- Track and report accuracy improvements/degradations over time
|
||||
- Monitor model learning progress through feedback loop
|
||||
- Create performance metrics for inference-training effectiveness
|
||||
- Generate alerts for learning regression or stagnation
|
||||
- _Requirements: 10.7_
|
||||
|
||||
## Inference History Management and Monitoring
|
||||
|
||||
- [ ] 7. Implement comprehensive inference logging and monitoring
|
||||
- Create InferenceMonitor class for logging and alerting
|
||||
- Log inference data storage operations with completeness metrics
|
||||
- Log training outcomes and model performance changes
|
||||
- Alert administrators on data flow issues with specific error details
|
||||
- _Requirements: 11.1, 11.2, 11.3_
|
||||
|
||||
- [ ] 7.1. Implement configurable retention policies
|
||||
- Create RetentionPolicyManager class
|
||||
- Archive or remove oldest entries when limits are reached
|
||||
- Prioritize keeping most recent and valuable training examples
|
||||
- Implement storage space monitoring and alerts
|
||||
- _Requirements: 11.4, 11.7_
|
||||
|
||||
- [ ] 7.2. Implement efficient historical data management
|
||||
- Compress inference data to minimize storage footprint
|
||||
- Maintain accessibility for training and analysis
|
||||
- Implement efficient query mechanisms for historical analysis
|
||||
- Add data archival and restoration capabilities
|
||||
- _Requirements: 11.5, 11.6_
|
||||
|
||||
## Trading Executor Implementation
|
||||
|
||||
- [ ] 5. Design and implement the trading executor
|
||||
- Create a TradingExecutor class that accepts trading actions from the orchestrator
|
||||
- Implement order execution through brokerage APIs
|
||||
- Add order lifecycle management
|
||||
- _Requirements: 7.1, 7.2, 8.6_
|
||||
|
||||
- [ ] 5.1. Implement brokerage API integrations
|
||||
- Create a BrokerageAPI interface
|
||||
- Implement concrete classes for MEXC and Binance
|
||||
- Add error handling and retry mechanisms
|
||||
- _Requirements: 7.1, 7.2, 8.6_
|
||||
|
||||
- [ ] 5.2. Implement order management
|
||||
- Create an OrderManager class
|
||||
- Implement methods for creating, updating, and canceling orders
|
||||
- Add order tracking and status updates
|
||||
- _Requirements: 7.1, 7.2, 8.6_
|
||||
|
||||
- [ ] 5.3. Implement error handling
|
||||
- Add comprehensive error handling for API failures
|
||||
- Implement circuit breakers for extreme market conditions
|
||||
- Add logging and notification mechanisms
|
||||
- _Requirements: 7.1, 7.2, 8.6_
|
||||
|
||||
## Risk Manager Implementation
|
||||
|
||||
- [ ] 6. Design and implement the risk manager
|
||||
- Create a RiskManager class
|
||||
- Implement risk parameter management
|
||||
- Add risk metric calculation
|
||||
- _Requirements: 7.1, 7.3, 7.4_
|
||||
|
||||
- [ ] 6.1. Implement stop-loss functionality
|
||||
- Create a StopLossManager class
|
||||
- Implement methods for creating and managing stop-loss orders
|
||||
- Add mechanisms to automatically close positions when stop-loss is triggered
|
||||
- _Requirements: 7.1, 7.2_
|
||||
|
||||
- [ ] 6.2. Implement position sizing
|
||||
- Create a PositionSizer class
|
||||
- Implement methods for calculating position sizes based on risk parameters
|
||||
- Add validation to ensure position sizes are within limits
|
||||
- _Requirements: 7.3, 7.7_
|
||||
|
||||
- [ ] 6.3. Implement risk metrics
|
||||
- Add methods to calculate risk metrics (drawdown, VaR, etc.)
|
||||
- Implement real-time risk monitoring
|
||||
- Add alerts for high-risk situations
|
||||
- _Requirements: 7.4, 7.5, 7.6, 7.8_
|
||||
|
||||
## Dashboard Implementation
|
||||
|
||||
- [ ] 7. Design and implement the dashboard UI
|
||||
- Create a Dashboard class
|
||||
- Implement the web-based UI using Flask/Dash
|
||||
- Add real-time updates using WebSockets
|
||||
- _Requirements: 6.1, 6.8_
|
||||
|
||||
- [ ] 7.1. Implement chart management
|
||||
- Create a ChartManager class
|
||||
- Implement methods for creating and updating charts
|
||||
- Add interactive features (zoom, pan, etc.)
|
||||
- _Requirements: 6.1, 6.2_
|
||||
|
||||
- [ ] 7.2. Implement control panel
|
||||
- Create a ControlPanel class
|
||||
- Implement start/stop toggles for system processes
|
||||
- Add sliders for adjusting buy/sell thresholds
|
||||
- _Requirements: 6.6, 6.7_
|
||||
|
||||
- [ ] 7.3. Implement system status display
|
||||
- Add methods to display training progress
|
||||
- Implement model performance metrics visualization
|
||||
- Add real-time system status updates
|
||||
- _Requirements: 6.5, 5.6_
|
||||
|
||||
- [ ] 7.4. Implement server-side processing
|
||||
- Ensure all processes run on the server without requiring the dashboard to be open
|
||||
- Implement background tasks for model training and inference
|
||||
- Add mechanisms to persist system state
|
||||
- _Requirements: 6.8, 5.5_
|
||||
|
||||
## Integration and Testing
|
||||
|
||||
- [ ] 8. Integrate all components
|
||||
- Connect the data provider to the CNN and RL models
|
||||
- Connect the CNN and RL models to the orchestrator
|
||||
- Connect the orchestrator to the trading executor
|
||||
- _Requirements: 8.1, 8.2, 8.3_
|
||||
|
||||
- [ ] 8.1. Implement comprehensive unit tests
|
||||
- Create unit tests for each component
|
||||
- Implement test fixtures and mocks
|
||||
- Add test coverage reporting
|
||||
- _Requirements: 8.1, 8.2, 8.3_
|
||||
|
||||
- [ ] 8.2. Implement integration tests
|
||||
- Create tests for component interactions
|
||||
- Implement end-to-end tests
|
||||
- Add performance benchmarks
|
||||
- _Requirements: 8.1, 8.2, 8.3_
|
||||
|
||||
- [ ] 8.3. Implement backtesting framework
|
||||
- Create a backtesting environment
|
||||
- Implement methods to replay historical data
|
||||
- Add performance metrics calculation
|
||||
- _Requirements: 5.8, 8.1_
|
||||
|
||||
- [ ] 8.4. Optimize performance
|
||||
- Profile the system to identify bottlenecks
|
||||
- Implement optimizations for critical paths
|
||||
- Add caching and parallelization where appropriate
|
||||
- _Requirements: 8.1, 8.2, 8.3_
|
||||
Reference in New Issue
Block a user