A comprehensive cryptocurrency trading analysis project that combines AI-powered price prediction with hybrid trading strategies for Bitcoin (BTC-USD).
This project consists of two main components:
- AI Predictor (
prediction.py) - Uses XGBoost machine learning to predict cryptocurrency price movements - Hybrid Trading Strategy (
strategy.py) - Implements a sophisticated trading strategy combining technical indicators
- Machine Learning Model: XGBoost Regressor with early stopping for robust predictions
- Feature Engineering: Creates lagged return features for time series prediction
- Model Evaluation: Calculates directional accuracy and generates prediction vs actual price plots
- Visualization: Generates comprehensive charts showing predicted vs actual prices
- Multi-Indicator Approach: Combines Moving Averages, Bollinger Bands, and RSI
- Trend Following: Uses 200-day MA for macro trend identification
- Mean Reversion: Bollinger Bands for entry/exit signals
- Momentum Confirmation: RSI for trade confirmation
- Risk Management: Built-in stop-loss and profit-taking mechanisms
- Lookback Period: 14 days of historical returns
- Target: Next day's return prediction
- Model: XGBoost with early stopping (200 estimators, learning rate 0.05)
- Moving Average: 200-day MA for trend identification
- Bollinger Bands: 20-period with 1.9 standard deviations
- RSI: 14-period with buy/sell levels at 45/55
- Position Sizing: Full portfolio allocation
- Entry: Price > MA200 AND Price hits Lower Bollinger Band AND RSI < 45
- Exit: Price hits Middle Bollinger Band (profit) OR Price breaks below MA200 (stop-loss)
- Entry: Price < MA200 AND Price hits Upper Bollinger Band AND RSI > 55
- Exit: Price hits Middle Bollinger Band (profit) OR Price breaks above MA200 (stop-loss)
The strategy provides comprehensive performance analysis including:
- Total Return
- Sharpe Ratio (Annualized)
- Maximum Drawdown
- Daily Win Rate
- Portfolio Value Tracking
- Trade Distribution Analysis
- Early Stopping: Prevents overfitting with validation-based stopping
- Directional Accuracy: Measures how often the model correctly predicts price direction
- Visual Analysis: Side-by-side comparison of predicted vs actual prices
- Regime-Aware: Different strategies for bull/bear markets
- Risk Management: Automatic stop-losses and profit-taking
- Comprehensive Visualization: 6-panel analysis dashboard
- Trade Tracking: Detailed trade log with entry/exit reasons
pip install pandas numpy matplotlib yfinance xgboost scikit-learnRequired packages:
pandas- Data manipulationnumpy- Numerical computationsmatplotlib- Plotting and visualizationyfinance- Yahoo Finance dataxgboost- Machine learning modelscikit-learn- Model evaluation metrics
from prediction import AIPredictor
# Initialize with default parameters
predictor = AIPredictor(
symbol='BTC-USD',
lookback_period=14,
test_size=0.2
)
# Run complete analysis
predictor.run_full_process()from strategy import HybridStrategy
# Initialize strategy
strategy = HybridStrategy(
symbol='BTC-USD',
initial_capital=10000,
ma_slow=200,
bb_period=20,
bb_std=1.9,
rsi_period=14,
rsi_buy_level=45,
rsi_sell_level=55
)
# Run complete analysis
results = strategy.run_complete_analysis()The project generates several output files:
predicted_vs_actual_price_xgboost.png- AI prediction visualizationfinal_strategy_analysis.png- Comprehensive trading strategy dashboard
symbol: Cryptocurrency symbol (default: 'BTC-USD')lookback_period: Number of historical days for features (default: 14)test_size: Fraction of data for testing (default: 0.2)
initial_capital: Starting capital (default: $10,000)ma_slow: Moving average period (default: 200)bb_period: Bollinger Bands period (default: 20)bb_std: Bollinger Bands standard deviation (default: 1.9)rsi_period: RSI calculation period (default: 14)rsi_buy_level: RSI threshold for buy signals (default: 45)rsi_sell_level: RSI threshold for sell signals (default: 55)
Both components are highly customizable:
- Change cryptocurrency symbols
- Adjust technical indicator parameters
- Modify risk management rules
- Customize visualization settings
The project provides:
- Performance Metrics: Returns, Sharpe ratio, drawdown analysis
- Trade Analysis: Entry/exit points, trade frequency, win rates
- Visual Dashboards: Comprehensive 6-panel analysis charts
- Risk Assessment: Maximum drawdown and volatility metrics
This project is for educational and research purposes only. Cryptocurrency trading involves significant risk, and past performance does not guarantee future results. Always conduct your own research and consider consulting with financial advisors before making investment decisions.
- The AI predictor uses early stopping to prevent overfitting
- The trading strategy includes both long and short positions
- All visualizations are saved as high-resolution PNG files
- The code includes comprehensive error handling and data validation