Skip to content

jitendra-ky/NextPrice

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

41 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ“ˆ NextPrice โ€” Stock Price Predictor

Python TensorFlow Django Scikit-learn Docker CI Open in Colab License: MIT

A production-grade LSTM-based stock price forecasting system โ€” real-time data ingestion, deep learning inference, async task queuing, REST API, and a Telegram bot interface โ€” all containerized and CI/CD ready.


๐Ÿš€ Try It Instantly โ€” Interactive Colab Notebook

Open in Colab

The Colab notebook walks through the complete ML pipeline end-to-end โ€” no local setup required:

  • ๐Ÿ“ฅ Fetching 10 years of real stock data via yfinance
  • โš™๏ธ MinMax scaling + 100-timestep sliding window construction
  • ๐Ÿง  Stacked LSTM training (128 โ†’ 64 units) with Adam optimizer
  • ๐Ÿ“Š Evaluation: MSE, RMSE, Rยฒ on held-out data
  • ๐Ÿ“ˆ Actual vs. Predicted price visualization
  • ๐Ÿ”ฎ Next-day price prediction

๐Ÿ† Model Performance โ€” Real Benchmark Results

Evaluated on 10 years of daily OHLCV data (2,454 training samples per ticker) using the trained LSTM model.

Ticker Rยฒ Score RMSE MSE Next-Day Prediction
AAPL 0.9984 $3.08 9.49 $303.45
MSFT 0.9985 $5.27 27.78 $414.81
TSLA 0.9956 $9.08 82.50 $403.84

Rยฒ > 0.995 across all tickers โ€” the model explains >99.5% of price variance on unseen trading days.


๐Ÿง  Machine Learning Core

Model Architecture

Input  โ†’  (batch, 100 timesteps, 1 feature)
   โ”‚
   โ–ผ
LSTM(128 units, return_sequences=True, activation=tanh)
   โ”‚
   โ–ผ
LSTM(64 units, activation=tanh)
   โ”‚
   โ–ผ
Dense(25, activation=linear)
   โ”‚
   โ–ผ
Dense(1, activation=linear)   โ† next-day price (inverse-scaled to USD)
Hyperparameter Value
Architecture Stacked LSTM โ€” 2 recurrent layers
Input window 100 trading days (timesteps)
Input features Closing price (MinMax scaled โ†’ [0, 1])
Output Next-day closing price
Optimizer Adam (lr=0.001, ฮฒโ‚=0.9, ฮฒโ‚‚=0.999)
Loss function Mean Squared Error (MSE)
Training samples 2,454 sequences per ticker (10 years)
Model size 1.4 MB (.keras format)
Framework TensorFlow 2.19 / Keras

ML Data Pipeline

Yahoo Finance API  โ†’  10 years OHLCV data  (period="10y")
        โ”‚
        โ–ผ
  Extract Closing Price  โ†’  shape: (N, 1)
        โ”‚
        โ–ผ
  MinMaxScaler  โ†’  values โˆˆ [0, 1]
        โ”‚
        โ–ผ
  Sliding Window (size=100)  โ†’  X: (2454, 100, 1)  |  y: (2454, 1)
        โ”‚
        โ–ผ
  Stacked LSTM Forward Pass
        โ”‚
        โ–ผ
  Inverse Transform  โ†’  price in USD
        โ”‚
        โ–ผ
  Metrics: MSE ยท RMSE ยท Rยฒ  +  Matplotlib plots saved to /media/

๐Ÿ—๏ธ System Architecture

                      โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                      โ”‚          Client Interfaces           โ”‚
                      โ”‚  Web Dashboard โ”‚ REST API โ”‚ Telegram โ”‚
                      โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                 โ”‚          โ”‚
                          โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”
                          โ”‚    Django Application    โ”‚
                          โ”‚  (DRF + JWT Auth + Rate  โ”‚
                          โ”‚       Limiting)          โ”‚
                          โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                       โ”‚
                    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                    โ”‚          Celery Task Queue           โ”‚
                    โ”‚        (Redis message broker)        โ”‚
                    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                       โ”‚
                    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                    โ”‚       StockPredictor Service         โ”‚
                    โ”‚  yfinance โ†’ preprocess โ†’ LSTM infer  โ”‚
                    โ”‚  โ†’ metrics โ†’ Matplotlib plots โ†’ DB   โ”‚
                    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

โœจ Features

Feature Description
๐Ÿง  LSTM Inference Engine Stacked LSTM (128โ†’64 units) trained on 2,454 sequences per stock
๐Ÿ“Š Rยฒ > 0.995 Model explains >99.5% of closing price variance across AAPL, MSFT, TSLA
๐Ÿ“ˆ Auto Visualization Matplotlib charts: price history + actual vs. predicted overlay
โšก Async Queue Celery + Redis for non-blocking, scalable prediction jobs
๐ŸŒ REST API JWT-authenticated endpoints for programmatic access
๐Ÿค– Telegram Bot /predict AAPL โ€” get ML predictions directly in chat
๐Ÿ”’ Rate Limiting Built-in abuse prevention via django-ratelimit
๐Ÿณ Docker Full containerized deployment with Docker Compose
โœ… CI Automated test suite runs on every push via GitHub Actions

๐Ÿ› ๏ธ Tech Stack

ML / Data

Library Version Role
TensorFlow / Keras 2.19.0 Stacked LSTM training & inference
Scikit-learn 1.7.0 MinMaxScaler, MSE, Rยฒ metrics
NumPy โ€” Sliding window sequences, array ops
yfinance 0.2.64 Real-time & historical OHLCV data
Matplotlib 3.10.3 Prediction chart generation

Backend / Infrastructure

Library Version Role
Django 5.0.6 Web framework
Django REST Framework 3.16.0 REST API
SimpleJWT 5.5.0 JWT authentication
Celery 5.4.0 Async task queue
Redis 5.0.1 Message broker & cache
python-telegram-bot 22.1 Telegram bot interface
Gunicorn 23.0.0 WSGI production server
Docker โ€” Containerization

๐Ÿ“ Project Structure

nextprice/
โ”œโ”€โ”€ core/
โ”‚   โ”œโ”€โ”€ services/
โ”‚   โ”‚   โ””โ”€โ”€ predictor.py        # ML pipeline: fetch โ†’ scale โ†’ window โ†’ LSTM โ†’ metrics
โ”‚   โ”œโ”€โ”€ management/commands/
โ”‚   โ”‚   โ””โ”€โ”€ predict.py          # CLI: python manage.py predict --ticker AAPL
โ”‚   โ”œโ”€โ”€ tasks.py                # Celery async tasks (web + Telegram delivery)
โ”‚   โ”œโ”€โ”€ telegram/               # Telegram bot handlers
โ”‚   โ”œโ”€โ”€ models.py               # Prediction model (ticker, metrics JSON, plot URLs)
โ”‚   โ”œโ”€โ”€ views.py                # DRF API views
โ”‚   โ””โ”€โ”€ tests.py                # Test suite
โ”œโ”€โ”€ user/                       # Auth & user management
โ”œโ”€โ”€ templates/                  # Web dashboard HTML
โ”œโ”€โ”€ static/                     # CSS / JS assets
โ”œโ”€โ”€ zproject/                   # Django settings & routing
โ”œโ”€โ”€ stock_prediction_model.keras # Trained LSTM model (1.4 MB)
โ”œโ”€โ”€ docker-compose.yml
โ””โ”€โ”€ requirements.txt

๐Ÿš€ Quick Start

Option A โ€” Docker (Recommended)

git clone https://github.com/jitendra-ky/nextprice
cd nextprice
cp .env.example .env          # edit .env with your keys
docker-compose up --build

Open http://localhost:8000/dashboard

Option B โ€” Local Dev

git clone https://github.com/jitendra-ky/nextprice
cd nextprice
python -m venv .venv
.venv\Scripts\activate          # Windows
# source .venv/bin/activate     # macOS / Linux
pip install -r requirements.txt
cp .env.example .env
python manage.py migrate
python manage.py createsuperuser

Start services in separate terminals:

# Terminal 1 โ€” Django
python manage.py runserver

# Terminal 2 โ€” Celery worker (required for Telegram bot)
python manage.py startcelery

# Terminal 3 โ€” Telegram bot
python manage.py telegrambot

Run a prediction from CLI

python manage.py predict --ticker AAPL
# โ†’ Predicted next-day price: $303.45
# โ†’ Rยฒ: 0.9984  |  RMSE: $3.08  |  MSE: 9.49

๐Ÿ” REST API

Method Endpoint Description Auth
POST /api/v1/register/ Register new user No
GET /api/v1/register/ Get user profile JWT
POST /api/v1/token/ Obtain JWT token No
POST /api/v1/token/refresh/ Refresh JWT token No
POST /api/v1/predict/ Run LSTM prediction JWT
GET /api/v1/predictions/ List user predictions JWT
GET /healthz/ Health check No

Example โ€” AAPL prediction response:

{
  "ticker": "AAPL",
  "next_day_price": 303.45,
  "mse": 9.491,
  "rmse": 3.08,
  "r2": 0.9984,
  "plot_urls": [
    "/media/plots/AAPL_history.png",
    "/media/plots/AAPL_pred_vs_actual.png"
  ]
}

๐Ÿค– Telegram Bot

Command Action
/start Register and link your account
/predict AAPL Run LSTM prediction for ticker
/latest View your most recent prediction
/help Show all commands

๐Ÿงช Testing & CI

python manage.py test          # Run full test suite
ruff check .                   # Linting

Automated tests run on every push via GitHub Actions (see .github/workflows/django.yml).


๐Ÿ“„ License

MIT License โ€” see LICENSE for details.

About

A comprehensive Django-based stock price prediction application with machine learning capabilities, REST API, web dashboard, and Telegram bot integration.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Packages

 
 
 

Contributors