Skip to content

AuthRan/SPP-LSTM

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Stock Market Prediction System 📈

LSTM, GRU and Transformer-based stock price prediction system for Nifty 50 stocks with an interactive Streamlit dashboard.

Python 3.11 TensorFlow 2.15 Streamlit License: MIT

🚀 Live Demo

https://spp-lstm-ashu.streamlit.app/


Features

  • 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

Screenshots

Main Dashboard

Dashboard

Model Training

Training

Predictions & Confidence Intervals

Predictions

Models Comparison

Gemini_Generated_Image_uhw0f5uhw0f5uhw0

Tech Stack

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

Model Architectures

LSTM (Long Short-Term Memory)

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

GRU (Gated Recurrent Unit)

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

Transformer Architecture

Standard Vanilla Transformer as per 2017 "Attention is all you need paper"

Baseline Models

  • Moving Average: Predicts based on recent average price
  • Naive: Random walk hypothesis (tomorrow = today)

Installation

Prerequisites

  • Python 3.11+
  • Virtual environment (recommended)

Setup

# 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.txt

Usage

Start the Dashboard

streamlit run app.py

Workflow

  1. Select a Stock: Choose from Nifty 50 tickers in the sidebar
  2. Load Data: Click "Load Data" to fetch 5 years of historical data
  3. Select Model: Choose LSTM, GRU, Transformer or baseline models
  4. Train Model: Click "Train Model" to train the selected architecture
  5. Get Prediction: Click "Get Prediction" to see future price forecasts
  6. Compare Models: Use "Compare All Models" for side-by-side analysis

Configuration Options

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

Project Structure

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

Nifty 50 Stocks Included

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


Key Results

Performance Metrics

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

Example: RELIANCE.NS Prediction

  • Direction Accuracy: 78% (LSTM correctly predicted price movement)
  • 10-day forecast: Price trend identified within ±5% error margin

Metrics Explained

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

API Reference

Core Functions

# 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'}

Deployment

Streamlit Cloud

The app is deployed on Streamlit Cloud:

  1. Connect your GitHub repository
  2. Set Python version to 3.11
  3. Deploy from main branch

Local Deployment with Docker

docker build -t stock-prediction .
docker run -p 8501:8501 stock-prediction

Limitations & Disclaimer

⚠️ For Educational Purposes Only

Stock 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.


Future Enhancements

  • 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

License

MIT License - See LICENSE for details.


Contact

For questions or collaboration, reach out via GitHub issues.


Built with ❤️ for educational purposes

About

This is a Transformer, LSTM and GRU based stock price predictor model with simple streamlit frontend.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages