A machine learning model that analyzes real-time market data from Alpaca API to predict stock market trends.
- Overview
- Features
- Prerequisites
- Installation
- Usage
- Project Structure
- Technical Details
- Configuration
- Results and Visualization
- Paper Trading vs. Live Trading
- Limitations
- Future Improvements
- Contributing
- License
MarketPulse is a Python-based machine learning project that uses the Alpaca API to fetch real-time and historical stock market data, process it with various technical indicators, and make predictions about future price movements. The project is designed to run in Google Colab, making it accessible without the need for local setup or configuration.
- Real-time Data Integration: Connects to Alpaca API for live market data
- Technical Analysis: Calculates key indicators like Moving Averages, MACD, RSI
- Machine Learning Models: Uses Random Forest to predict market direction
- Backtesting: Tests prediction accuracy against historical data
- Visualization: Displays buy/sell signals and model performance
- Paper Trading Mode: Test strategies without risking real money
- Google account (for Google Colab)
- Alpaca account with API keys
- Basic understanding of Python and machine learning concepts
This project is designed to run in Google Colab, so there's no local installation required.
- Open the notebook in Google Colab
- Run the first cell to install required packages:
!pip install alpaca-trade-api pandas numpy scikit-learn matplotlib
-
API Setup:
- Create an Alpaca account at app.alpaca.markets
- Enable 2FA for your account (required for API access)
- Generate API keys from the dashboard
- Add your API keys to the notebook:
API_KEY = "YOUR_API_KEY" SECRET_KEY = "YOUR_SECRET_KEY" BASE_URL = "https://paper-api.alpaca.markets"
-
Data Collection:
symbols = ['AAPL', 'MSFT', 'AMZN', 'GOOGL', 'TSLA'] data_dict = get_historical_data(symbols, timeframe='1D')
-
Train Model:
features = ['return', 'range', 'daily_volatility', 'ma_5', 'ma_10', 'ma_20', 'ma_50', 'ma_200', 'macd', 'macd_hist', 'rsi'] model, X_test, y_test = train_model(data_dict['AAPL'], features)
-
Make Predictions:
predictions = predict_market_direction(model, symbols, features) print(predictions)
- Data Collection:
get_historical_data()function fetches and processes market data - Feature Engineering: Technical indicators calculated from raw price data
- Model Training:
train_model()function creates and evaluates the predictive model - Prediction:
predict_market_direction()generates forecasts for selected symbols - Visualization: Plotting functions display model signals and performance
-
Technical Indicators:
- Moving Averages (5, 10, 20, 50, 200 day)
- MACD (Moving Average Convergence Divergence)
- RSI (Relative Strength Index)
- Volatility metrics
-
Machine Learning Model:
- Random Forest Classifier
- Binary classification (price up or down)
- Feature importance analysis
- Performance metrics (accuracy, precision, recall)
You can customize the model by modifying:
- Symbols: Change the list of stocks to analyze
- Timeframe: Adjust data granularity ('1D', '1H', '15Min', etc.)
- Features: Add or remove technical indicators
- Model Parameters: Tune the Random Forest hyperparameters
- Date Range: Set the historical data period for training
The notebook generates:
-
Model Performance Metrics:
- Accuracy score
- Classification report (precision, recall, F1-score)
- Feature importance ranking
-
Visual Analysis:
- Price chart with buy/sell signals
- Correct and incorrect predictions highlighted
- Performance comparison with baseline strategies
The project uses Alpaca's paper trading environment by default:
BASE_URL = "https://paper-api.alpaca.markets"For live trading (use with caution):
BASE_URL = "https://api.alpaca.markets"- Market prediction is inherently difficult and uncertain
- The model does not account for fundamental factors or news events
- Performance may vary across different market conditions
- Past performance does not guarantee future results
- Add sentiment analysis from news and social media
- Implement more advanced models (LSTM, Transformer)
- Include portfolio optimization strategies
- Add risk management features
- Expand to other asset classes (options, crypto)
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.