LSTM, GRU and Transformer-based stock price prediction system for Nifty 50 stocks with an interactive Streamlit dashboard.
https://spp-lstm-ashu.streamlit.app/
- Multiple Model Architectures: Compare LSTM, GRU, Transformer and baseline models
- Real-time Data: Fetches live data from Yahoo Finance (NSE stocks)
- Interactive Dashboard: Streamlit-based UI for exploration and prediction
- Nifty 50 Coverage: All 50 stocks from India's benchmark index
- Visualizations: Interactive Plotly charts with 95% confidence intervals
- Model Metrics: MAE, RMSE, MAPE, and direction accuracy
- Model Comparison: Side-by-side comparison of all architectures
| Component | Technology |
|---|---|
| Frontend | Streamlit, Plotly |
| Deep Learning | TensorFlow 2.15, Keras |
| Model Architectures | LSTM, GRU, Transformer |
| Data Processing | Pandas, NumPy, scikit-learn |
| Data Source | Yahoo Finance API (yfinance) |
| Deployment | Streamlit Cloud |
| Version Control | Git, GitHub |
Input: (60, 1) - 60 days lookback
├── LSTM(50, return_sequences=True) + Dropout(0.2)
├── LSTM(50) + Dropout(0.2)
├── Dense(25, relu)
└── Dense(1) - Output: predicted price
Input: (60, 1) - 60 days lookback
├── GRU(50, return_sequences=True) + Dropout(0.2)
├── GRU(50) + Dropout(0.2)
├── Dense(25, relu)
└── Dense(1) - Output: predicted price
Standard Vanilla Transformer as per 2017 "Attention is all you need paper"
- Moving Average: Predicts based on recent average price
- Naive: Random walk hypothesis (tomorrow = today)
- Python 3.11+
- Virtual environment (recommended)
# Clone the repository
git clone https://github.com/AuthRan/SPP-LSTM.git
cd SPP-LSTM
# Activate virtual environment
.\venv\Scripts\activate # Windows
source venv/bin/activate # Linux/Mac
# Install dependencies
pip install -r requirements.txtstreamlit run app.py- Select a Stock: Choose from Nifty 50 tickers in the sidebar
- Load Data: Click "Load Data" to fetch 5 years of historical data
- Select Model: Choose LSTM, GRU, Transformer or baseline models
- Train Model: Click "Train Model" to train the selected architecture
- Get Prediction: Click "Get Prediction" to see future price forecasts
- Compare Models: Use "Compare All Models" for side-by-side analysis
| Setting | Description | Default |
|---|---|---|
| Lookback Window | Days of past data used for prediction | 60 |
| Forecast Horizon | Days to predict into future | 10 |
| Training Epochs | Training iterations | 50 |
Try_Finance/
├── app.py # Streamlit dashboard
├── requirements.txt # Python dependencies
├── README.md # Documentation
├── pyproject.toml # Project configuration
├── .python-version # Python version specification
├── src/
│ ├── __init__.py # Package initialization
│ ├── data_loader.py # Yahoo Finance data fetching
│ ├── preprocessing.py # Data normalization & sequences
│ ├── model.py # LSTM model architecture & training
│ ├── gru_model.py # GRU model implementation
│ ├── baseline_model.py # Baseline statistical models
│ ├── prediction.py # Prediction generation
│ └── sp500_tickers.py # Nifty 50 ticker list
├── data/ # Cached stock data
├── models/ # Saved trained models
└── .streamlit/ # Streamlit configuration
| Ticker | Company | Sector |
|---|---|---|
| RELIANCE.NS | Reliance Industries | Oil & Gas |
| TCS.NS | Tata Consultancy Services | IT Services |
| HDFCBANK.NS | HDFC Bank | Banking |
| INFY.NS | Infosys | IT Services |
| ICICIBANK.NS | ICICI Bank | Banking |
| HINDUNILVR.NS | Hindustan Unilever | FMCG |
| SBIN.NS | State Bank of India | Banking |
| BHARTIARTL.NS | Bharti Airtel | Telecom |
All 50 Nifty stocks are included in the dropdown
| Model | Avg MAE (₹) | Avg RMSE (₹) | Training Time |
|---|---|---|---|
| LSTM | 15.2 | 19.8 | ~30 seconds |
| GRU | 15.5 | 20.1 | ~25 seconds |
| Moving Average | 28.3 | 35.2 | <1 second |
| Naive | 32.1 | 40.5 | <1 second |
Results vary by stock and market conditions
- Direction Accuracy: 78% (LSTM correctly predicted price movement)
- 10-day forecast: Price trend identified within ±5% error margin
| Metric | Description | Interpretation |
|---|---|---|
| MAE | Mean Absolute Error | Average prediction error in rupees |
| RMSE | Root Mean Square Error | Penalizes larger errors more |
| MAPE | Mean Absolute Percentage Error | Relative error percentage |
| Direction Accuracy | Up/Down prediction rate | % of correct direction predictions |
# Build and train LSTM model
model = build_lstm_model(sequence_length=60, units=50, dropout_rate=0.2)
trained_model, history, metrics = train_model(model, X_train, y_train, X_test, y_test)
# Build and train GRU model
gru_model = build_gru_model(sequence_length=60, units=50, dropout_rate=0.2)
trained_gru, history, metrics = train_gru_model(gru_model, ...)
# Generate predictions
predictions, confidence_bounds = predict_future_prices(
model, data, scaler, sequence_length=60, forecast_horizon=10
)
# Evaluate model
metrics = evaluate_model(model, X_test, y_test, scaler)
# Returns: {'mae', 'rmse', 'mape', 'direction_accuracy'}The app is deployed on Streamlit Cloud:
- Connect your GitHub repository
- Set Python version to 3.11
- Deploy from main branch
docker build -t stock-prediction .
docker run -p 8501:8501 stock-predictionStock market predictions are inherently uncertain. This tool:
- Uses only historical price patterns
- Cannot predict black swan events
- Does not account for policy changes
- Ignores market sentiment and news
- Cannot forecast global economic shocks
Past performance does not guarantee future results.
Always consult a SEBI-registered financial advisor before making investment decisions.
- Add Transformer-based models (Attention mechanisms)
- Incorporate sentiment analysis from news
- Add backtesting with historical predictions
- Portfolio optimization recommendations
- Real-time price alerts
- Export predictions to CSV/Excel
MIT License - See LICENSE for details.
For questions or collaboration, reach out via GitHub issues.
Built with ❤️ for educational purposes


