mset's picture
Update read.me
91ac0d2 verified
# ๐Ÿค– Financial AI Predictor
An advanced machine learning system for stock price prediction using ensemble models and comprehensive feature engineering.
![Python](https://img.shields.io/badge/python-v3.8+-blue.svg)
![Scikit-learn](https://img.shields.io/badge/scikit--learn-v1.0+-orange.svg)
![License](https://img.shields.io/badge/license-Educational-green.svg)
![Status](https://img.shields.io/badge/status-Active-brightgreen.svg)
## ๐ŸŽฏ Overview
Financial AI Predictor combines state-of-the-art machine learning models with advanced financial feature engineering to predict stock prices. Built with robustness and educational value in mind, it demonstrates best practices for applying ML to financial markets.
### โœจ Key Features
- ๐Ÿง  **Ensemble ML Models**: Random Forest + Gradient Boosting + Ridge Regression
- ๐Ÿ“Š **Advanced Feature Engineering**: 50+ technical indicators and market features
- โฐ **Real-time Data**: Live market data from Yahoo Finance
- ๐Ÿ”ฌ **Robust Validation**: Time series cross-validation with walk-forward analysis
- ๐ŸŽฏ **Uncertainty Quantification**: Confidence intervals and model consensus
- ๐Ÿ“ˆ **Interactive Interface**: Professional Gradio web interface
- ๐Ÿ†“ **No API Keys**: 100% free public data sources
## ๐Ÿš€ Quick Start
### Installation
```bash
# Clone the repository
git clone https://github.com/yourusername/financial-ai-predictor.git
cd financial-ai-predictor
# Install dependencies
pip install -r requirements.txt
# Run the application
python financial_ai_predictor.py
```
### Docker Setup (Optional)
```bash
# Build Docker image
docker build -t financial-ai-predictor .
# Run container
docker run -p 7860:7860 financial-ai-predictor
```
### Basic Usage
1. **Launch the app**: `python financial_ai_predictor.py`
2. **Open browser**: Navigate to `http://localhost:7860`
3. **Enter stock symbol**: e.g., "AAPL", "GOOGL", "TSLA"
4. **Click "Run AI Analysis"**: Wait for model training and prediction
5. **Review results**: Analysis report, charts, and feature importance
## ๐Ÿ“Š How It Works
### 1. Data Collection
- Downloads 2 years of historical OHLCV data from Yahoo Finance
- Real-time price and volume information
- Automatic data quality checks and cleaning
### 2. Feature Engineering
```python
# Technical Indicators
- Price momentum (5, 20 day)
- Moving averages (5, 10, 20, 50 day)
- RSI, MACD, Bollinger Bands
- ADX, Stochastic, Williams %R
- Volume indicators (OBV, CMF, VWAP)
# Market Regime Features
- Volatility ratios and regimes
- Trend strength indicators
- Gap analysis and fill rates
- Price-volume relationships
```
### 3. Model Architecture
```python
# Ensemble Models
models = {
'random_forest': RandomForestRegressor(n_estimators=200),
'gradient_boost': GradientBoostingRegressor(n_estimators=150),
'ridge': Ridge(alpha=1.0)
}
# Time Series Validation
validation = TimeSeriesSplit(n_splits=5)
```
### 4. Prediction Pipeline
1. **Feature Extraction**: Calculate all technical indicators
2. **Model Inference**: Each model generates independent prediction
3. **Ensemble**: Performance-weighted average of predictions
4. **Uncertainty**: Confidence intervals from model disagreement
## ๐Ÿ“ˆ Supported Assets
- **US Stocks**: All NYSE, NASDAQ listed companies
- **International Stocks**: Major global exchanges
- **ETFs**: Index funds, sector ETFs, commodity ETFs
- **Cryptocurrencies**: BTC-USD, ETH-USD, and major coins
- **Market Indices**: S&P 500 (^GSPC), NASDAQ (^IXIC), Dow Jones
### Example Symbols
```
AAPL, GOOGL, MSFT, TSLA, NVDA, AMZN, META
SPY, QQQ, VTI, IWM, GLD, SLV
BTC-USD, ETH-USD, ADA-USD
^GSPC, ^IXIC, ^DJI, ^VIX
```
## ๐Ÿ”ฌ Technical Details
### Feature Engineering Pipeline
```python
def extract_features(data):
features = pd.DataFrame()
# Price features
features['returns'] = data['Close'].pct_change()
features['log_returns'] = np.log(data['Close'] / data['Close'].shift(1))
# Technical indicators
features['rsi'] = ta.momentum.rsi(data['Close'])
features['macd'] = ta.trend.macd_diff(data['Close'])
features['bb_position'] = calculate_bollinger_position(data['Close'])
# Volume features
features['volume_ratio'] = data['Volume'] / data['Volume'].rolling(20).mean()
features['obv'] = ta.volume.on_balance_volume(data['Close'], data['Volume'])
return features
```
### Model Training
```python
# Time series cross-validation
tscv = TimeSeriesSplit(n_splits=5)
for train_idx, val_idx in tscv.split(features):
X_train = features.iloc[train_idx]
y_train = targets.iloc[train_idx]
model.fit(X_train, y_train)
score = model.score(X_val, y_val)
```
### Performance Metrics
- **Rยฒ Score**: Coefficient of determination
- **MAE**: Mean Absolute Error
- **RMSE**: Root Mean Square Error
- **Directional Accuracy**: Percentage of correct direction predictions
## ๐Ÿ“Š Example Output
```
๐Ÿค– FINANCIAL AI PREDICTOR - AAPL
๐Ÿ“Š CURRENT STATUS
โ€ข Current Price: $182.50
โ€ข Analysis Date: 2025-01-15 14:30:00
โ€ข Recommendation: ๐ŸŸข BUY
๐ŸŽฏ ENSEMBLE PREDICTION
โ€ข Next Day Target: $184.20
โ€ข Expected Return: +0.93%
โ€ข Prediction Confidence: 73.5%
๐Ÿค– MODEL PERFORMANCE
โ€ข Random Forest: Rยฒ = 0.0847 ยฑ 0.0234
โ€ข Gradient Boost: Rยฒ = 0.0756 ยฑ 0.0198
โ€ข Ridge: Rยฒ = 0.0542 ยฑ 0.0156
๐Ÿง  TOP PREDICTIVE FEATURES
โ€ข rsi: 0.0234
โ€ข bb_position: 0.0198
โ€ข volume_ratio: 0.0176
โ€ข macd: 0.0145
```
## ๐ŸŽฏ Best Practices
### For Users
1. **Diversification**: Never rely on a single prediction
2. **Risk Management**: Use appropriate position sizing
3. **Validation**: Cross-check with fundamental analysis
4. **Timeframes**: Consider multiple prediction horizons
5. **Market Conditions**: Be aware of regime changes
### For Developers
1. **Feature Engineering**: Domain knowledge is crucial
2. **Validation**: Always use time series aware validation
3. **Overfitting**: Monitor out-of-sample performance
4. **Data Quality**: Clean and validate input data
5. **Model Updates**: Retrain periodically
## โš ๏ธ Important Disclaimers
### ๐ŸŽ“ Educational Purpose
This tool is designed for:
- Learning about machine learning in finance
- Understanding feature engineering techniques
- Demonstrating ensemble methods
- Exploring financial data analysis
### ๐Ÿ“‰ Risk Warnings
- **Not Financial Advice**: Predictions are for educational purposes only
- **Past Performance**: Does not guarantee future results
- **Model Limitations**: Cannot predict black swan events
- **Market Risk**: All investments carry risk of loss
- **Professional Advice**: Consult qualified financial advisors
### ๐Ÿ”ฌ Technical Limitations
- **Market Efficiency**: Predictable patterns may be arbitraged away
- **Regime Changes**: Models may fail during market shifts
- **Data Dependency**: Quality depends on input data
- **Overfitting**: Historical patterns may not persist
## ๐Ÿ› ๏ธ Configuration
### Environment Variables
```bash
# Optional: Set data cache directory
export YFINANCE_CACHE_DIR="/path/to/cache"
# Optional: Set log level
export LOG_LEVEL="INFO"
```
### Custom Configuration
```python
# Modify model parameters
MODELS_CONFIG = {
'random_forest': {
'n_estimators': 200,
'max_depth': 15,
'min_samples_split': 5
},
'gradient_boost': {
'n_estimators': 150,
'max_depth': 8,
'learning_rate': 0.1
}
}
# Modify feature engineering
FEATURE_CONFIG = {
'ma_periods': [5, 10, 20, 50],
'momentum_periods': [5, 10, 20],
'volatility_windows': [5, 20, 60]
}
```
## ๐Ÿงช Testing
```bash
# Run basic tests
python -m pytest tests/
# Test specific components
python -m pytest tests/test_features.py
python -m pytest tests/test_models.py
# Run with coverage
pytest --cov=financial_ai_predictor tests/
```
## ๐Ÿ“š Dependencies
### Core Requirements
- **Python**: 3.8+
- **NumPy**: Scientific computing
- **Pandas**: Data manipulation
- **Scikit-learn**: Machine learning models
- **yfinance**: Financial data
- **TA**: Technical analysis indicators
- **Gradio**: Web interface
- **Plotly**: Interactive visualizations
### Optional Enhancements
- **SciPy**: Advanced statistical functions
- **Statsmodels**: Econometric models
- **Jupyter**: Interactive development
## ๐Ÿค Contributing
1. **Fork** the repository
2. **Create** a feature branch (`git checkout -b feature/amazing-feature`)
3. **Commit** your changes (`git commit -m 'Add amazing feature'`)
4. **Push** to the branch (`git push origin feature/amazing-feature`)
5. **Open** a Pull Request
### Development Setup
```bash
# Clone for development
git clone https://github.com/yourusername/financial-ai-predictor.git
cd financial-ai-predictor
# Install development dependencies
pip install -r requirements-dev.txt
# Run in development mode
python financial_ai_predictor.py --debug
```
## ๐Ÿ“ License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
## ๐Ÿ™ Acknowledgments
- **Yahoo Finance**: For providing free financial data
- **TA-Lib**: Technical analysis library
- **Scikit-learn**: Machine learning framework
- **Gradio**: Easy-to-use ML interfaces
- **Plotly**: Interactive visualization library
## ๐Ÿ“ž Support
- **Issues**: [GitHub Issues](https://github.com/yourusername/financial-ai-predictor/issues)
- **Discussions**: [GitHub Discussions](https://github.com/yourusername/financial-ai-predictor/discussions)
- **Email**: your.email@example.com
## ๐Ÿ”ฎ Roadmap
### Version 2.0
- [ ] Deep learning models (LSTM, Transformer)
- [ ] Alternative data integration
- [ ] Multi-asset portfolio optimization
- [ ] Real-time streaming predictions
- [ ] Advanced risk metrics
### Version 1.5
- [ ] Options pricing models
- [ ] Sector rotation analysis
- [ ] Economic indicators integration
- [ ] Backtesting framework
- [ ] Performance attribution
---
**โญ Star this repository if you find it useful!**
**๐Ÿ› Found a bug? Please open an issue.**
**๐Ÿ’ก Have an idea? Start a discussion.**
---
*Built with โค๏ธ for the financial ML community*