A sophisticated stock market prediction system that combines CNN and LSTM models to predict stock price movements. The model has been tested on various stocks and shows promising results in backtesting.
- Ensemble Model Architecture: Combines CNN and Bidirectional LSTM for robust predictions
- Real-time Predictions: Uses Alpha Vantage API for live stock data
- Smart Caching: Implements efficient data caching to handle API rate limits
- Backtesting Engine: Complete backtesting system with visualization
- Adaptive Trading Strategy: Dynamic thresholds based on model confidence
The above plot shows a successful trade where the model:
- Bought at ₹2,710.35 when it predicted a relatively small drop (-4.382%)
- Sold at ₹2,896.15 when it hit the take-profit target (+6.86%)
- Generated a total return of 6.50% in a short period
- Clone the repository
git clone https://github.com/yourusername/stock-market-prediction.git
cd stock-market-prediction- Install dependencies
pip install -r requirements.txt- Set up your environment
- Copy
.env.sampleto.env - Get your API key from Alpha Vantage
- Add your API key to
.env
- Train the model
python train.pyThis will:
- Download historical data for your chosen stock
- Train the CNN and LSTM models
- Create an ensemble model
- Save the trained model in
models/ensemble_model.pth
Note: Each time you run training, it will create a fresh model optimized for your chosen stock and recent market conditions. This ensures your predictions are based on up-to-date patterns.
- Run backtests
python backtest.py- Make predictions
python main.py --symbol RELIANCE.BSEThe system uses an ensemble of two models:
- CNN Model
- 1D Convolutions for pattern recognition
- LeakyReLU activation
- Batch normalization
- Dropout for regularization
- LSTM Model
- Bidirectional LSTM for temporal patterns
- Sequence-to-one architecture
- Batch normalization
- Dropout for regularization
- Ensemble Combination
- Weighted average of CNN and LSTM predictions
- Learnable weights with softmax normalization
- Small positive bias to counter bearish tendencies
- Choose Your Stock: The model performs best when trained on the specific stock you want to predict
- Data Range: Use at least 6-12 months of historical data for training
- Validation: Always check the backtesting results before using predictions
- Retraining: Consider retraining periodically to adapt to changing market conditions
The backtester implements a flexible trading strategy:
- Buy when predicted drop is less than threshold (-4.5%)
- Sell when predicted drop exceeds threshold (-6.5%)
- Stop loss at -1%
- Take profit at +1.5%
- Uses Alpha Vantage API for reliable stock data
- Implements smart caching to handle rate limits
- Normalizes features using percentage changes
- Handles missing data and outliers
Feel free to:
- Fork the repository
- Create a feature branch
- Submit pull requests
MIT License - feel free to use this for your own projects!
This is an experimental project. The predictions should not be used as financial advice. Always do your own research before making investment decisions.
