- Introduced a new `strategies` package containing the core structure for trading strategies, including `BaseStrategy`, `StrategyFactory`, and various strategy implementations (EMA, RSI, MACD). - Added utility functions for signal detection and validation in `strategies/utils.py`, enhancing modularity and maintainability. - Updated `pyproject.toml` to include the new `strategies` package in the build configuration. - Implemented comprehensive unit tests for the strategy foundation components, ensuring reliability and adherence to project standards. These changes establish a solid foundation for the strategy engine, aligning with project goals for modularity, performance, and maintainability.
10 KiB
10 KiB
Relevant Files
strategies/__init__.py- Strategy package initialization and exportsstrategies/base.py- BaseStrategy abstract class following BaseIndicator patternstrategies/factory.py- Strategy factory/registry system for dynamic strategy loadingstrategies/manager.py- StrategyManager class for user-defined strategies (mirrors IndicatorManager)strategies/implementations/__init__.py- Strategy implementations package initializationstrategies/implementations/ema_crossover.py- EMA Crossover strategy implementationstrategies/implementations/rsi.py- RSI-based momentum strategy implementationstrategies/implementations/macd.py- MACD trend following strategy implementationstrategies/utils.py- Strategy utility functions and helpersstrategies/data_types.py- Strategy-specific data types and signal definitionsconfig/strategies/templates/- Directory for JSON strategy templatesconfig/strategies/user_strategies/- Directory for user-defined strategy configurationsconfig/strategies/config_utils.py- Strategy configuration utilities and validationdatabase/models.py- Updated to include strategy signals table definitiondatabase/repositories/strategy_repository.py- Strategy signals repository following repository patterndatabase/operations.py- Updated to include strategy operations accessdatabase/migrations/versions/add_strategy_signals_table.py- Alembic migration for strategy signals tablecomponents/charts/layers/strategy_signals.py- Strategy signal chart layer for visualizationcomponents/charts/data_integration.py- Updated to include strategy data integrationtests/strategies/test_base_strategy.py- Unit tests for BaseStrategy abstract classtests/strategies/test_strategy_factory.py- Unit tests for strategy factory systemtests/strategies/test_strategy_manager.py- Unit tests for StrategyManager classtests/strategies/implementations/test_ema_crossover.py- Unit tests for EMA Crossover strategytests/strategies/implementations/test_rsi.py- Unit tests for RSI strategytests/strategies/implementations/test_macd.py- Unit tests for MACD strategytests/database/test_strategy_repository.py- Unit tests for strategy repository
Notes
- Strict Adherence to Indicator Patterns: The strategy engine components (BaseStrategy, StrategyFactory, StrategyManager, Strategy implementations, and configurations) MUST strictly mirror the existing
data/common/indicators/module's structure, factory approach, and configuration management. This ensures consistency and simplifies development. - Database Segregation for Signals: The newly created
strategy_signalstable is exclusively for strategy analysis and backtesting results, distinct from the existingsignalstable which is for live bot trading operations. Maintain this clear separation. - Initial Full Recalculation: For real-time strategy execution, strategies will initially recalculate completely on each new candle, similar to how technical indicators currently operate. Optimizations for incremental updates can be considered in a later phase.
- Multi-timeframe Support: Strategies should be designed to support and utilize market data from multiple timeframes, following the pattern established by indicators that can consume data from different timeframes.
- Exclusive Use of Repository Pattern: All database interactions, including storing and retrieving strategy signals and run data, must be performed exclusively through the
StrategyRepositoryand other existing repositories. Avoid raw SQL queries. - JSON-based Configuration: Strategy parameters and configurations are to be managed via JSON files within
config/strategies/, aligning with the existing configuration system for indicators and other components. - Layered Chart Integration: Strategy signals and performance visualizations will be integrated into the dashboard as a new chart layer, utilizing the existing modular chart system.
- Comprehensive Testing: Ensure that all new classes, functions, and modules within the strategy engine have corresponding unit tests placed in the
tests/strategies/directory, following established testing conventions.
Decisions
1. Vectorized vs. Iterative Calculation
- Decision: Refactored strategy signal detection to use vectorized Pandas operations (e.g.,
shift(), boolean indexing) instead of iterative Python loops. - Reasoning: Significantly improves performance for signal generation, especially with large datasets, while maintaining identical results as verified by dedicated tests.
- Impact: All core strategy implementations (EMA Crossover, RSI, MACD) now leverage vectorized functions for their primary signal detection logic.
2. Indicator Key Generation Consistency
- Decision: Centralized the
_create_indicator_keylogic into a shared utility functioncreate_indicator_key()instrategies/utils.py. - Reasoning: Eliminates code duplication in
StrategyFactoryand individual strategy implementations, ensuring consistent key generation and easier maintenance if indicator naming conventions change. - Impact:
StrategyFactoryand all strategy implementations now use this shared utility for generating unique indicator keys.
Tasks
-
1.0 Core Strategy Foundation Setup
- 1.1 Create
strategies/directory structure following indicators pattern - 1.2 Implement
BaseStrategyabstract class instrategies/base.pywithcalculate()andget_required_indicators()methods - 1.3 Create
strategies/data_types.pywithStrategySignal,SignalType, andStrategyResultclasses - 1.4 Implement
StrategyFactoryclass instrategies/factory.pyfor dynamic strategy loading and registration - 1.5 Create strategy implementations directory
strategies/implementations/ - 1.6 Implement
EMAStrategyinstrategies/implementations/ema_crossover.pyas reference implementation - 1.7 Implement
RSIStrategyinstrategies/implementations/rsi.pyfor momentum-based signals - 1.8 Implement
MACDStrategyinstrategies/implementations/macd.pyfor trend-following signals - 1.9 Create
strategies/utils.pywith helper functions for signal validation and processing - 1.10 Create comprehensive unit tests for all strategy foundation components
- 1.1 Create
-
2.0 Strategy Configuration System
- 2.1 Create
config/strategies/directory structure mirroring indicators configuration - 2.2 Implement
config/strategies/config_utils.pywith configuration validation and loading functions - 2.3 Create JSON schema definitions for strategy parameters and validation rules
- 2.4 Create strategy templates in
config/strategies/templates/for common strategy configurations - 2.5 Implement
StrategyManagerclass instrategies/manager.pyfollowingIndicatorManagerpattern - 2.6 Add strategy configuration loading and saving functionality with file-based storage
- 2.7 Create user strategies directory
config/strategies/user_strategies/for custom configurations - 2.8 Implement strategy parameter validation and default value handling
- 2.9 Add configuration export/import functionality for strategy sharing
- 2.1 Create
-
3.0 Database Schema and Repository Layer
- 3.1 Create new
strategy_signalstable migration (separate from existingsignalstable for bot operations) - 3.2 Design
strategy_signalstable with fields: strategy_name, strategy_config, symbol, timeframe, timestamp, signal_type, price, confidence, metadata, run_id - 3.3 Create
strategy_runstable to track strategy execution sessions for backtesting and analysis - 3.4 Implement
StrategyRepositoryclass indatabase/repositories/strategy_repository.pyfollowing repository pattern - 3.5 Add strategy repository methods for signal storage, retrieval, and batch operations
- 3.6 Update
database/operations.pyto include strategy operations access - 3.7 Create database indexes for optimal strategy signal queries (strategy_name, symbol, timeframe, timestamp)
- 3.8 Add data retention policies for strategy signals (configurable cleanup of old analysis data)
- 3.9 Implement strategy signal aggregation queries for performance analysis
- 3.1 Create new
-
4.0 Strategy Data Integration
- 4.1 Create
StrategyDataIntegratorclass in newstrategies/data_integration.pymodule - 4.2 Implement data loading interface that leverages existing
TechnicalIndicatorsclass for indicator dependencies - 4.3 Add multi-timeframe data handling for strategies that require indicators from different timeframes
- 4.4 Implement strategy calculation orchestration with proper indicator dependency resolution
- 4.5 Create caching layer for computed indicator results to avoid recalculation across strategies
- 4.6 Add strategy signal generation and validation pipeline
- 4.7 Implement batch processing capabilities for backtesting large datasets
- 4.8 Create real-time strategy execution pipeline that integrates with existing chart data refresh
- 4.9 Add error handling and recovery mechanisms for strategy calculation failures
- 4.1 Create
-
5.0 Chart Integration and Visualization
- 5.1 Create
StrategySignalLayerclass incomponents/charts/layers/strategy_signals.py - 5.2 Implement strategy signal visualization with different markers for entry/exit/hold signals
- 5.3 Add strategy signal layer configuration following existing chart layer patterns
- 5.4 Update
components/charts/data_integration.pyto include strategy data loading for charts - 5.5 Create strategy selection controls in dashboard for chart overlay
- 5.6 Implement real-time strategy signal updates in chart refresh cycle
- 5.7 Add strategy performance metrics display (win rate, signal accuracy, etc.)
- 5.8 Create strategy signal filtering and display options (signal types, confidence thresholds)
- 5.9 Implement strategy comparison visualization for multiple strategies on same chart
- 5.1 Create