This project implements a multi-factor stock selection and backtesting system, integrating multi-factor models, market timing strategies, and backtest analysis to optimize investment decisions. Using Python and Tushare API, the system retrieves financial market data, evaluates stock investment value through factor analysis, and integrates timing signals to optimize trading decisions. The system allows users to test different factor combinations and assess their effectiveness in historical market conditions.
multi-factor-stock-selection/
├── data/ # Raw market data (retrieved via Tushare API)
├── output/ # Backtest results & analysis reports
├── factors/ # Factor computation & evaluation
├── factor_graveyard/ # Deprecated factors & logs
├── strategy/ # Stock selection, market timing, backtesting
├── utils/ # Helper functions
├── visualization/ # Performance visualization
├── config.py # Strategy parameter settings
├── main.py # Main execution script
├── README.md # Project documentation
├── requirements.txt # Python dependencies
├── Dockerfile # Docker containerization support (optional)
- Uses Tushare API to fetch daily market prices, financial data, suspension records, dividend adjustments.
- Cleans and preprocesses data (outlier removal, standardization, missing data handling).
- Computes future N-day returns for factor evaluation.
- Fundamental Factors: PE, PB, ROE, debt ratio, revenue growth.
- Technical Factors: Momentum (5-day, 10-day returns), moving averages (MA5, MA20, MA60), volume trends.
- Sentiment Factors: News sentiment analysis, capital inflow tracking.
- Computes Factor IC (Information Coefficient) to assess predictive power.
- Calculates ICIR (IC Stability) for factor robustness analysis.
- Implements Factor Removal Mechanism (ICIR < 0.3 for 3 consecutive months).
- Factor-weighted scoring method assigns stock rankings.
- Selects Top N stocks for final portfolio.
- Dynamic rebalancing based on factor performance.
- Moving average signals (e.g., MA20 vs MA60 for trend confirmation).
- Market breadth indicators (advancing stock percentage threshold).
- Volume trend signals (increased/decreased trading volume).
- Combines stock selection & timing signals for backtesting.
- Handles suspension adjustments, dividend reinvestments.
- Computes daily portfolio value and generates performance reports.
- Calculates key performance metrics:
- Annual Return, Max Drawdown, Sharpe Ratio, Calmar Ratio, Sortino Ratio.
- Information Ratio (vs Benchmark), Profit-Loss Ratio, Win Rate, Turnover Rate.
- Portfolio Performance vs Benchmark Index.
- Factor IC Time-Series Analysis.
- Annual Return Distribution.
- Cumulative Excess Returns.
Ensure you have Python 3.8+ installed and install required dependencies:
pip install -r requirements.txt
To execute the full pipeline:
python main.py
This script will fetch data, compute factors, select stocks, generate timing signals, run backtests, and visualize performance.
output/
├── portfolio_value.csv # Daily portfolio value
├── positions.csv # Daily stock positions
├── return_statistics.csv # Performance metrics
├── ic_summary.csv # Factor IC statistics
├── timing_signals.csv # Market timing signals
├── portfolio_performance.png # Portfolio performance vs Index
├── annual_returns.png # Annual return bar chart
├── excess_returns.png # Cumulative excess return curve
- Python: Core development language
- Pandas, NumPy: Data processing & analysis
- Matplotlib, Seaborn: Data visualization
- Tushare API: Market data acquisition
- Scikit-learn: Factor normalization, scoring
- Git & GitHub: Version control & collaboration
- Real-time market data tracking for live trading signals.
- Automated backtest scheduling (daily updates, performance reports).
- Dynamic factor weighting based on IC performance.
- Integration with trading platforms (e.g., Alpaca, Interactive Brokers).
Contributions are welcome! If you'd like to improve the system, feel free to open an Issue or submit a Pull Request on GitHub.
GitHub Repository: [https://github.com/1984tkr/multi-factor-stock-selection]