anubhav0907/Nifty50-LSTM
1
Nifty 50 Multi-Model Stock Price Prediction
A deep learning project for Nifty 50 forecasting using multiple neural architectures (LSTM, GRU, Transformer, and CNN+LSTM), with a Streamlit dashboard for model comparison, forecasting, and interactive technical analysis.
Project Structure
Stock Prediction/
└── Data collection/
├── data_fetch.ipynb # Data download & feature engineering
├── model_train.ipynb # Multi-model training & evaluation
├── predict.ipynb # Multi-model inference + best-model forecasting
├── app.py # Streamlit dashboard
├── data/
│ ├── nifty50_raw.csv # Raw OHLCV data (2018–2024)
│ ├── processed_data.pkl # Scaled sequences + scalers (dict format)
│ └── forecast_30days.csv # 30-day forecast output
└── models/
├── lstm_model.h5 # LSTM checkpoint
├── gru_model.h5 # GRU checkpoint
├── transformer_model.keras # Transformer checkpoint
└── cnn_lstm_model.keras # CNN+LSTM checkpointFeatures
- 11 input features: Close, Open, High, Low, Volume, MA20, MA50, RSI, Bollinger Bands (upper/lower), Returns
- 4 trained architectures: LSTM, GRU, Transformer, CNN+LSTM
- Chronological validation with
shuffle=Falsefor time-series-safe training - Automatic model leaderboard (RMSE, MAE, MAPE) in prediction notebook and dashboard
- Best-model-driven forecasting for 7 to 60 business days
- Apple-inspired Streamlit UI (clean light theme, SF-style typography, premium card layout)
- Model benchmark sections with rolling MAPE, metric bars, pairwise model-distance heatmap, and baseline comparisons
- Streamlit dashboard with model selection, comparison overlays, forecast download, and technical indicators
Model Architecture Summary
- LSTM: 2 stacked LSTM layers (48 -> 24), Dropout, Dense head
- GRU: 2 stacked GRU layers (48 -> 24), Dropout, Dense head
- Transformer: MultiHeadAttention + feed-forward block + dense regression head
- CNN+LSTM: stacked Conv1D blocks + deep LSTM stack + dense head
Common setup:
- Loss: Huber
- Optimizers: Adam with gradient clipping (
clipnorm=1.0) - Window size: 60 days
- Train/Test split: chronological (time-series-safe)
Performance
Performance is model-dependent and can change with retraining. Use:
predict.ipynbfor a sorted model leaderboard (RMSE,MAE,MAPE)- Streamlit dashboard tab Model Performance for active model diagnostics and full-model leaderboard
Setup & Usage
1. Install dependencies
pip install tensorflow scikit-learn pandas numpy matplotlib yfinance streamlit plotly2. Fetch data & build features
Run all cells in data_fetch.ipynb — saves data/processed_data.pkl.
3. Train models
Run all cells in model_train.ipynb — saves model checkpoints in models/:
lstm_model.h5gru_model.h5transformer_model.kerascnn_lstm_model.keras
4. Run prediction workflow
Run predict.ipynb to:
- Load all available trained models
- Evaluate each model on test data
- Auto-select the best model (lowest MAPE)
- Generate and save
data/forecast_30days.csvwith model name metadata
5. Run the Streamlit dashboard
python -m streamlit run app.pyThe dashboard now:
- Loads all available models from
Data collection/models - Lets you choose the active model in the sidebar
- Supports optional prediction overlay for all models
- Uses selected model for metrics and forecast download
- Includes a dedicated Model Benchmarks tab for model-vs-model analysis
6. Deploy on Hugging Face Spaces (Docker)
- Create a new Hugging Face Space and choose
Dockeras the SDK. - Point the Space to this GitHub repository.
- Hugging Face builds from
Dockerfileand runs rootapp.py, which forwards toData collection/app.py. - After each push to
main, the Space rebuilds and redeploys.
Tech Stack
- Python 3.12
- TensorFlow / Keras
- scikit-learn
- pandas, NumPy
- Streamlit
- Plotly
- yfinance
