A comprehensive trading system for analyzing financial data, predicting trading opportunities, and testing various strategies including machine learning models and traditional technical approaches. The system is inspired by the hydraulic jump phenomenon in fluid dynamics, which has striking parallels to market behavior.
- Multiple Strategies: Support for ML models and traditional strategies like Moving Average (MA)
- Alpha Factor Generation: Over 200+ behavioral finance and technical alpha factors
- Signal Generation: Smart detection of trading events based on price patterns
- Advanced Backtesting: Test different strategies with detailed performance analytics
- Real-time Simulation: Simulate trading in real-time environments
- Visualization Tools: Generate intuitive charts and comprehensive performance reports
- Model Versioning: Track and compare different model versions
- Feature Analysis: SHAP values and feature importance analysis
- Comprehensive Metrics: Financial metrics including Sharpe, Sortino, Calmar ratios
This project was inspired by the hydraulic jump phenomenon in fluid dynamics, which has striking parallels to market behavior.
A hydraulic jump occurs when a fast-flowing fluid (supercritical flow) suddenly slows down and rises in height, converting to subcritical flow. This phenomenon is observable in rivers, spillways, and open channels.

Figure 1: Illustration of a hydraulic jump in an open channel.
In financial markets, similar behavior can be observed:
- Slow Price Decline: Analogous to the gradual flow before the critical point
- Rapid Price Decline: Corresponds to the high-velocity flow
- Hydraulic Jump: The point where the market suddenly reverses direction after a rapid decline

Figure 2: Example of a market "hydraulic jump" where a rapid price decline is followed by a sudden reversal.
This natural phenomenon provides the theoretical foundation for our trading strategy, especially for identifying potential reversal points after rapid market declines.
- Python 3.8 or higher
- pip package manager
-
Clone the repository:
git clone https://github.com/WenyuChiou/Event-Driven-Strategy.git cd Event-Driven-Strategy -
Create a virtual environment (recommended):
python -m venv venv # On Windows venv\Scripts\activate # On Linux/Mac source venv/bin/activate
-
Install dependencies:
pip install -r requirements.txt
-
Install TA-Lib (optional, for technical analysis):
- Windows: Download wheel file from here and install:
pip install TA_Libβ0.4.24βcp39βcp39βwin_amd64.whl
- Linux/Mac: Follow instructions at TA-Lib documentation
- Windows: Download wheel file from here and install:
-
Verify installation:
python -c "import pandas, numpy, sklearn, lightgbm; print('Installation successful!')"
pip install -e .Place your data files in the data/raw/ directory. Required format:
- Excel (.xlsx) or CSV files
- Required columns:
date,open,high,low,close,volume - Date column should be in datetime format
Example data structure:
date,open,high,low,close,volume
2023-01-01 08:45:00,100.0,100.5,99.5,100.2,10000
2023-01-01 08:46:00,100.2,100.8,100.0,100.5,12000
...
Train a model:
python main.py --mode train --data data/raw/TX00_training.xlsx --model lightgbm --trials 100Run backtesting:
python main.py --mode backtest --validation-data data/raw/TX00_validation.xlsxRun MA strategy:
python main.py --mode ma_strategy --validation-data data/raw/TX00_validation.xlsx --ma-period 5Run all modes (train + backtest):
python main.py --mode allstreamlit run app.pyThen open your browser to http://localhost:8501
project/
β
βββ src/ # Source code directory
β βββ __init__.py
β βββ config.py # Configuration management
β βββ logger.py # Logging system
β βββ event_detection.py # Event detection module
β βββ feature_engineering.py # Feature engineering module
β βββ feature_pipeline.py # Modular feature pipeline
β βββ feature_analysis.py # Feature importance analysis (SHAP)
β βββ alpha_selector.py # Alpha factor selection
β βββ model.py # Model training and evaluation
β βββ model_registry.py # Model versioning
β βββ backtesting.py # Backtesting module
β βββ evaluation.py # Financial metrics
β βββ visualization.py # Visualization tools
β βββ ma_strategy.py # MA strategy implementation
β βββ utils.py # Helper functions
β
βββ package/ # External functionality packages
β βββ alpha_eric.py # Alpha factor calculations (200+ factors)
β βββ FE.py # Feature engineering tools
β βββ ModelLoader.py # Model loading utilities
β βββ FuturesFilter.py # Futures data filtering
β
βββ examples/ # Example scripts
β βββ basic_example.py # Basic usage example
β βββ model_training.py # Model training example
β βββ backtesting_example.py # Backtesting example
β
βββ tests/ # Test suite
β βββ unit/ # Unit tests
β βββ integration/ # Integration tests
β βββ fixtures/ # Test data
β
βββ data/ # Data directory
β βββ raw/ # Raw data
β βββ processed/ # Processed data
β
βββ models/ # Saved trained models
β βββ registry/ # Model registry
β
βββ results/ # Results output directory
β βββ backtests/ # Backtest results
β βββ visualization/ # Chart outputs
β βββ summaries/ # Analysis summaries
β
βββ docs/ # Documentation
β βββ USER_GUIDE.md # User guide
β βββ API.md # API documentation
β
βββ Fig/ # Image directory
β βββ HJ.png # Hydraulic jump diagram
β βββ example.png # Market example
β
βββ main.py # Main program entry point
βββ app.py # Streamlit web application
βββ imports.py # Streamlit dependencies
βββ setup.py # Package installation script
βββ requirements.txt # Project dependencies
βββ README.md # This file
-
Machine Learning Models: Train prediction models using historical data
- RandomForest, GradientBoosting, XGBoost, LightGBM
- Bayesian hyperparameter optimization
- Model versioning and tracking
-
Moving Average Strategy: Classic MA crossover strategy with optimization
- Multiple MA periods
- Parameter optimization
- Performance analytics
-
Signal Generator: Generate trading signals based on price patterns
- Probability-based thresholds
- Time-based filtering
- Position management
-
Feature Engineering: Advanced feature creation and selection
- 200+ alpha factors based on behavioral finance
- Technical indicators (TA-Lib)
- Feature selection using Lasso and variance threshold
- Modular feature pipeline with caching
-
Alpha Factor Selection:
- Information Coefficient (IC) analysis
- Statistical significance testing
- Alpha decay analysis
- Feature ranking
-
Backtesting Engine: Evaluate strategies with detailed metrics
- Separate long/short analysis
- Parameter optimization
- Strategy comparison
- Walk-forward validation
-
Evaluation Metrics:
- Sharpe Ratio, Sortino Ratio, Calmar Ratio
- Information Ratio, Treynor Ratio
- Maximum drawdown analysis
- Trade statistics
-
Visualization Tools: Generate various charts and performance reports
- Cumulative returns charts
- Trade comparison visualizations
- Feature importance plots
- Performance heatmaps
python main.py --mode train \
--data data/raw/training_data.xlsx \
--model lightgbm \
--trials 100Available models: randomforest, gradientboosting, xgboost, lightgbm
python main.py --mode backtest \
--validation-data data/raw/validation_data.xlsx \
--model-path models/lightgbm.joblib \
--feature-path models/features.xlsx \
--scaler-path models/scaler.joblibpython main.py --mode comprehensive \
--data data/raw/training_data.xlsx \
--validation-data data/raw/validation_data.xlsxpython main.py --mode simulate \
--validation-data data/raw/validation_data.xlsx \
--long-threshold 0.0026 \
--short-threshold 0.0026python main.py --mode ma_strategy \
--validation-data data/raw/validation_data.xlsx \
--ma-period 5 \
--commission 0.0MA Strategy Parameters:
--ma-period: Moving average period (default: 5)--commission: Trading commission (default: 0.0)--price-col: Price column name (default: 'Close')--min-hold-periods: Minimum holding periods (default: 5)--optimize: Run parameter optimization
The system supports multiple machine learning algorithms:
- RandomForest: Ensemble learning method using multiple decision trees
- GradientBoosting: Sequential ensemble technique that combines weak learners
- XGBoost: Optimized gradient boosting implementation
- LightGBM: High-performance gradient boosting framework by Microsoft
Model Features:
- Bayesian hyperparameter optimization using Optuna
- Time series cross-validation
- Feature importance analysis
- Model versioning and registry
- Performance tracking
The system includes over 200 alpha factors based on behavioral finance theories:
- Prospect Theory: Factors based on risk aversion and reference-dependent preferences
- Herding Behavior: Factors capturing market herding and sentiment shifts
- Anchoring Effect: Factors analyzing anchoring biases in trading behavior
- Market Microstructure: Factors based on trading volume, volatility, and price momentum
- Momentum-based: Cross-asset momentum, relative strength
- Volatility-based: GARCH-based volatility, realized volatility ratios
- Liquidity-based: Bid-ask spread factors, volume-price relationships
Alpha Selection Tools:
- Information Coefficient (IC) calculation
- Statistical significance testing
- Alpha decay analysis
- Feature ranking and selection
The system calculates comprehensive performance metrics:
- Total Return
- Annualized Return
- Volatility
- Sharpe Ratio: Risk-adjusted return using standard deviation
- Sortino Ratio: Risk-adjusted return using downside deviation only
- Calmar Ratio: Annual return / maximum drawdown
- Information Ratio: Active return / tracking error
- Treynor Ratio: Excess return / beta
- Maximum Drawdown
- Drawdown Duration
- Recovery Time
- Win Rate
- Profit Factor
- Average Win/Loss
- Largest Win/Loss
- Win/Loss Streaks
- Average Trade Duration
Raw Data β Feature Calculation β Event Detection β Feature Engineering
β Model Training β Backtesting β Performance Evaluation
- Event Detection (
src/event_detection.py): Detects trading events based on price patterns - Feature Engineering (
src/feature_engineering.py): Creates and selects features - Model Training (
src/model.py): Trains ML models with hyperparameter optimization - Backtesting (
src/backtesting.py): Evaluates strategies on historical data - Evaluation (
src/evaluation.py): Calculates financial performance metrics - Visualization (
src/visualization.py): Creates charts and reports
Run tests using pytest:
# Run all tests
pytest
# Run unit tests only
pytest tests/unit/
# Run with coverage
pytest --cov=src --cov-report=html- User Guide: See
docs/USER_GUIDE.mdfor detailed usage instructions - API Documentation: See
docs/API.mdfor function and class references - Examples: Check
examples/directory for code examples
Configuration is managed through src/config.py. Key settings include:
- Paths: All directory paths are automatically configured
- Event Detection Parameters: Profit/loss windows, thresholds
- Feature Engineering Parameters: Variance thresholds, correlation thresholds
- Model Parameters: Default hyperparameters for each model type
- Trading Strategy Parameters: Thresholds, holding periods, excluded times
-
Import Errors: Make sure all dependencies are installed:
pip install -r requirements.txt
-
TA-Lib Installation: See installation guide above for platform-specific instructions
-
Memory Issues: For large datasets, consider:
- Using data sampling
- Processing data in chunks
- Reducing feature count
-
Model Loading Errors: Ensure model files exist in
models/directory
Contributions are welcome! Please follow these guidelines:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Make your changes
- Add tests for new functionality
- Ensure all tests pass (
pytest) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is for learning and research purposes only and does not constitute investment advice.
- This project is for learning and research purposes only
- Does not constitute investment advice
- Please carefully evaluate backtest results and consider model limitations
- Thoroughly test and evaluate strategies before using actual funds for trading
- Past performance does not guarantee future results
For issues, questions, or contributions, please open an issue on the GitHub repository.
- Add more advanced alpha factors
- Implement deep learning models (LSTM, Transformer)
- Add ensemble methods
- Improve parallel processing
- Enhanced visualization dashboard
- Real-time data integration
- Paper trading interface
If you use this project in your research, please cite:
Trading Event Detection System based on Hydraulic Jump Concept
https://github.com/WenyuChiou/Event-Driven-Strategy