A comprehensive machine learning experiment to train and evaluate multiple AI models for predicting Bitcoin (BTC) price movements on short-term (hourly) timeframes.
This project implements a complete experimental framework for training and comparing different AI models and strategies to predict BTC price movements. It includes:
- Multiple AI Model Types: LSTM, GRU, Transformer, Random Forest, XGBoost
- Rich Feature Engineering: Technical indicators, momentum, volatility, volume, and time features
- Comprehensive Evaluation: Regression and classification metrics, backtesting capabilities
- Advanced Visualization: Price charts, predictions, model comparisons, feature importance
- Multiple data sources (Yahoo Finance, Binance via CCXT)
- Configurable timeframes (1m, 5m, 15m, 1h, 4h, 1d)
- Historical data with OHLCV (Open, High, Low, Close, Volume)
- Price Features: Returns, log returns, price changes, ratios
- Moving Averages: SMA, EMA with multiple windows (5, 10, 20, 50)
- Momentum Indicators: RSI, Stochastic Oscillator, MACD
- Volatility Indicators: Bollinger Bands, ATR, historical volatility
- Volume Indicators: OBV, volume changes, volume ratios
- Time Features: Cyclical encoding for hour, day, week, month
- Lag Features: Historical values at different time lags
-
LSTM (Long Short-Term Memory)
- Recurrent neural network for time series
- Captures long-term dependencies
- Multiple layers with dropout
-
GRU (Gated Recurrent Unit)
- Simplified RNN architecture
- Faster training than LSTM
- Effective for sequential patterns
-
Transformer
- Multi-head attention mechanism
- Parallel processing of sequences
- State-of-the-art architecture
-
Random Forest
- Ensemble of decision trees
- Robust to overfitting
- Feature importance analysis
-
XGBoost
- Gradient boosting framework
- High performance on tabular data
- Advanced regularization
Regression Task (Predicting Returns):
- RMSE (Root Mean Squared Error)
- MAE (Mean Absolute Error)
- R² (Coefficient of Determination)
- MAPE (Mean Absolute Percentage Error)
- Directional Accuracy
Classification Task (Predicting Direction):
- Accuracy
- Precision
- Recall
- F1 Score
- Confusion Matrix
- Clone the repository:
git clone https://github.com/gelimorto2/AAITI-Roots.git
cd AAITI-Roots- Create a virtual environment (recommended):
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate- Install dependencies:
pip install -r requirements.txtRun the complete experiment with default settings:
python experiment.pyThis will:
- Fetch 90 days of hourly BTC data from Yahoo Finance
- Engineer features with technical indicators
- Train 5 different AI models (LSTM, GRU, Transformer, Random Forest, XGBoost)
- Evaluate models on test data
- Generate visualizations and save results
Edit the configuration in experiment.py:
# Configuration
DATA_SOURCE = 'yfinance' # 'yfinance' or 'binance'
DAYS_BACK = 90 # Days of historical data
INTERVAL = '1h' # Data interval: '1m', '5m', '15m', '1h', '4h', '1d'
TARGET_HORIZON = 1 # Hours ahead to predict
TASK = 'regression' # 'regression' or 'classification'from data_fetcher import BTCDataFetcher
from datetime import datetime, timedelta
fetcher = BTCDataFetcher()
# Fetch from Yahoo Finance
df = fetcher.fetch_data(
source='yfinance',
start_date=(datetime.now() - timedelta(days=30)).strftime('%Y-%m-%d'),
end_date=datetime.now().strftime('%Y-%m-%d'),
interval='1h'
)
# Or fetch from Binance
df = fetcher.fetch_data(
source='binance',
days_back=30,
timeframe='1h'
)from feature_engineering import FeatureEngineer
engineer = FeatureEngineer(df)
df_features = engineer.build_all_features(target_horizon=1)
# Or add features individually
engineer = FeatureEngineer(df)
engineer.add_price_features()
engineer.add_moving_averages(windows=[5, 10, 20])
engineer.add_momentum_indicators()
engineer.add_volatility_indicators()
engineer.create_target(horizon=1)from models import LSTMModel, RandomForestModel
# LSTM Model
lstm = LSTMModel(
input_shape=(24, 50), # (sequence_length, n_features)
units=[64, 32],
dropout=0.2,
task='regression'
)
lstm.fit(X_train, y_train, X_val, y_val, epochs=50)
predictions = lstm.predict(X_test)
# Random Forest Model
rf = RandomForestModel(n_estimators=100, max_depth=10, task='regression')
rf.fit(X_train, y_train)
predictions = rf.predict(X_test)from evaluation import ModelEvaluator
evaluator = ModelEvaluator(task='regression')
metrics = evaluator.evaluate(y_true, y_pred, model_name='LSTM')
evaluator.print_results()
# Compare models
best_model = evaluator.get_best_model(metric='rmse')from visualization import Visualizer
viz = Visualizer()
# Plot price history
viz.plot_price_history(df, save_path='price_history.png')
# Plot predictions vs actual
viz.plot_predictions_vs_actual(y_true, y_pred, model_name='LSTM',
save_path='predictions.png')
# Compare models
viz.plot_model_comparison(results_df, metric='rmse',
save_path='comparison.png')
# Feature importance
viz.plot_feature_importance(feature_names, importance_scores,
save_path='features.png')After running the experiment, the following outputs are generated:
AAITI-Roots/
├── data/
│ ├── btc_raw_data.csv # Raw OHLCV data
│ ├── btc_features.csv # Engineered features
│ └── model_results.csv # Evaluation metrics
├── plots/
│ ├── 01_price_history.png # Historical price chart
│ ├── 02_model_comparison.png # Model performance comparison
│ ├── 03_*_predictions.png # Predictions vs actual for each model
│ ├── 04_*_training.png # Training history for deep learning models
│ └── 05_*_features.png # Feature importance for tree models
└── experiment.py
The experiment evaluates models on:
- Prediction Accuracy: How well models predict future prices/returns
- Directional Accuracy: Percentage of correct up/down predictions
- Model Comparison: Ranking models by performance metrics
- Feature Importance: Which features contribute most to predictions
Example output:
================================================================================
MODEL EVALUATION RESULTS
================================================================================
Regression Metrics:
--------------------------------------------------------------------------------
LSTM:
RMSE: 0.002345
MAE: 0.001876
R²: 0.453210
MAPE: 12.34%
Directional Accuracy: 56.78%
XGBoost:
RMSE: 0.002123
MAE: 0.001654
R²: 0.487652
MAPE: 11.23%
Directional Accuracy: 58.92%
Best model by RMSE: XGBoost
The experiment implements multiple prediction strategies:
- Time Horizon Strategies: 1-hour, 4-hour, 1-day predictions
- Feature Set Strategies:
- Minimal features (price only)
- Technical indicators
- Full feature set
- Model Strategies:
- Deep learning (LSTM, GRU, Transformer)
- Tree-based (Random Forest, XGBoost)
- Target Strategies:
- Regression: Predict exact returns
- Classification: Predict price direction
The experiment typically shows:
- Tree-based models (XGBoost, Random Forest) perform well on tabular features
- Deep learning models capture sequential patterns effectively
- Feature engineering significantly improves prediction accuracy
- Short-term predictions (1-hour) are more accurate than longer horizons
- Directional accuracy is often more reliable than exact price prediction
- Past performance doesn't guarantee future results
- Market conditions change (concept drift)
- High-frequency trading requires more sophisticated infrastructure
- Transaction costs and slippage not fully modeled
- Not financial advice - for educational purposes only
Potential improvements:
- Ensemble methods combining multiple models
- Real-time data streaming and predictions
- Additional data sources (news sentiment, social media)
- More advanced architectures (LSTM with attention, etc.)
- Reinforcement learning for trading strategies
- Cross-cryptocurrency analysis
- Market regime detection
- Risk management integration
- Python 3.7+
- TensorFlow 2.13.0
- scikit-learn 1.3.0
- XGBoost 1.7.6
- pandas, numpy, matplotlib, seaborn
- yfinance, ccxt
- ta (technical analysis library)
See requirements.txt for complete dependencies.
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.
This software is for educational and research purposes only. It is not financial advice. Cryptocurrency trading carries significant risk. Always do your own research and consult with financial professionals before making investment decisions.
For questions or feedback, please open an issue on GitHub.
Note: This is an experimental framework for learning and research. Use at your own risk.