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.
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
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.
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 |
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/
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 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 โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
| 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 |
| 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 |
| 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 |
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
git clone https://github.com/jitendra-ky/nextprice
cd nextprice
cp .env.example .env # edit .env with your keys
docker-compose up --buildOpen http://localhost:8000/dashboard
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 createsuperuserStart 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 telegrambotpython manage.py predict --ticker AAPL
# โ Predicted next-day price: $303.45
# โ Rยฒ: 0.9984 | RMSE: $3.08 | MSE: 9.49| 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"
]
}| 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 |
python manage.py test # Run full test suite
ruff check . # LintingAutomated tests run on every push via GitHub Actions (see .github/workflows/django.yml).
MIT License โ see LICENSE for details.