Skip to content

Repository files navigation

Stock Price Prediction - Production Grade Platform

A comprehensive, production-ready machine learning platform for stock price prediction using LSTM and attention mechanisms. Built with enterprise-grade architecture, including data pipelines, model training, serving, monitoring, and deployment infrastructure.

🎯 Project Goals

Transform a tutorial LSTM model into a production-grade system with:

  • βœ… Robust Data Engineering: Multi-source data ingestion with quality validation
  • βœ… Production-grade ML: Reproducible training, evaluation, and serving
  • βœ… Scalable Architecture: Kubernetes-ready, cloud-agnostic deployment
  • βœ… Comprehensive Monitoring: Performance, data quality, and business metrics
  • βœ… Complete Testing: Unit, integration, and performance tests
  • βœ… Enterprise Security: Authentication, encryption, audit logging
  • βœ… MLOps Best Practices: Experiment tracking, model registry, versioning

πŸ“‹ Table of Contents

πŸš€ Quick Start

Prerequisites

  • Python 3.10+
  • Docker & Docker Compose
  • Git
  • PostgreSQL 15
  • Redis 7

1. Clone and Setup

# Clone the repository
git clone https://github.com/example/stock-predictor-prod.git
cd stock-predictor-prod

# Copy environment file
cp .env.example .env

# Set up development environment (installs dependencies)
make setup

# Start Docker services (PostgreSQL, Redis, MLflow, Airflow)
make docker-up

# Verify all services are running
make health-check

2. Run Training Pipeline

# Start a training pipeline
make run-training

# Or use Airflow for scheduling
make run-scheduler
make run-webui  # http://localhost:8080

3. Start Prediction API

# Development server (with auto-reload)
make run-api

# Server will be available at http://localhost:8000
# API docs at http://localhost:8000/docs

4. Monitor Performance

# Access MLflow tracking UI
open http://localhost:5000

# Monitor with Prometheus/Grafana
open http://localhost:3000  # Username: admin, Password: from .env

πŸ—οΈ Architecture

System Components

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                     Data Ingestion Layer                         β”‚
β”‚  (Alpha Vantage, IEX Cloud, FMP, Yahoo Finance)                β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                              ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                  Data Processing Pipeline                       β”‚
β”‚  (Validation, Cleaning, Feature Engineering, Feast)           β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                              ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚              Model Training & Evaluation                        β”‚
β”‚  (PyTorch LSTM, Attention, Hyperparameter Tuning)             β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                      MLflow Registry                            β”‚
β”‚  (Experiment Tracking, Model Versioning, A/B Testing)         β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                              ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚               Model Serving & Prediction API                    β”‚
β”‚  (FastAPI, Redis Caching, Load Balancing)                      β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                              ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚         Monitoring, Alerting & Continuous Improvement          β”‚
β”‚  (Prometheus, Grafana, Data Drift Detection)                   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Technology Stack

Component Technology Purpose
Orchestration Apache Airflow Workflow scheduling & DAG management
ML Framework PyTorch Lightning Model training & distributed learning
Feature Store Feast (upcoming) Feature management & serving
Model Registry MLflow Experiment tracking & model versioning
API Serving FastAPI REST API for predictions
Caching Redis Feature & prediction caching
Database PostgreSQL Data storage & metadata
Monitoring Prometheus + Grafana Metrics & visualization
Deployment Kubernetes Container orchestration
CI/CD GitHub Actions Automated testing & deployment

πŸ“ Project Structure

stock-predictor-prod/
β”œβ”€β”€ .github/
β”‚   └── workflows/
β”‚       └── ci.yml                 # CI/CD pipeline
β”œβ”€β”€ airflow/
β”‚   β”œβ”€β”€ dags/                     # Airflow DAGs
β”‚   β”œβ”€β”€ logs/
β”‚   └── plugins/
β”œβ”€β”€ configs/
β”‚   β”œβ”€β”€ default.yaml              # Default configuration
β”‚   β”œβ”€β”€ production.yaml            # Production settings
β”‚   β”œβ”€β”€ prometheus.yml             # Prometheus config
β”‚   └── grafana/
β”‚       └── provisioning/          # Grafana dashboards
β”œβ”€β”€ data/
β”‚   β”œβ”€β”€ raw/                       # Original data from APIs
β”‚   β”œβ”€β”€ processed/                 # Cleaned & validated data
β”‚   └── features/                  # Engineered features
β”œβ”€β”€ docs/
β”‚   β”œβ”€β”€ architecture/              # System design docs
β”‚   β”‚   └── adr/                  # Architecture Decision Records
β”‚   β”œβ”€β”€ api/                       # API documentation
β”‚   └── guides/                    # User guides
β”œβ”€β”€ docker/
β”‚   β”œβ”€β”€ Dockerfile.train           # Training image
β”‚   β”œβ”€β”€ Dockerfile.serve           # Serving image
β”‚   β”œβ”€β”€ Dockerfile.dev             # Development image
β”‚   └── Dockerfile.airflow         # Airflow image
β”œβ”€β”€ k8s/
β”‚   β”œβ”€β”€ deployment.yaml            # K8s deployment
β”‚   β”œβ”€β”€ service.yaml               # K8s service
β”‚   β”œβ”€β”€ configmap.yaml             # K8s config
β”‚   └── secrets.yaml               # K8s secrets
β”œβ”€β”€ notebooks/
β”‚   β”œβ”€β”€ exploration/               # Data exploration
β”‚   └── experiments/               # Model experiments
β”œβ”€β”€ production/
β”‚   β”œβ”€β”€ data_ingestion/            # Data collection APIs
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   β”œβ”€β”€ base.py
β”‚   β”‚   β”œβ”€β”€ alpha_vantage.py
β”‚   β”‚   β”œβ”€β”€ iex_cloud.py
β”‚   β”‚   └── validators.py
β”‚   β”œβ”€β”€ features/                  # Feature engineering
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   β”œβ”€β”€ technical_indicators.py
β”‚   β”‚   β”œβ”€β”€ statistical.py
β”‚   β”‚   └── feature_store.py
β”‚   β”œβ”€β”€ models/                    # Model definitions
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   β”œβ”€β”€ lstm.py
β”‚   β”‚   β”œβ”€β”€ attention.py
β”‚   β”‚   └── ensemble.py
β”‚   β”œβ”€β”€ training/                  # Training pipeline
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   β”œβ”€β”€ train.py
β”‚   β”‚   β”œβ”€β”€ evaluate.py
β”‚   β”‚   β”œβ”€β”€ hyperparameter_tuning.py
β”‚   β”‚   └── callbacks.py
β”‚   β”œβ”€β”€ serving/                   # Prediction serving
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   β”œβ”€β”€ api.py
β”‚   β”‚   β”œβ”€β”€ models_loader.py
β”‚   β”‚   └── cache.py
β”‚   β”œβ”€β”€ monitoring/                # Monitoring & metrics
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   β”œβ”€β”€ metrics.py
β”‚   β”‚   β”œβ”€β”€ data_drift.py
β”‚   β”‚   └── alerting.py
β”‚   β”œβ”€β”€ utils/                     # Shared utilities
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   β”œβ”€β”€ config.py
β”‚   β”‚   β”œβ”€β”€ logging.py
β”‚   β”‚   └── database.py
β”‚   └── health_check.py            # System health check
β”œβ”€β”€ scripts/
β”‚   β”œβ”€β”€ init_db.sql                # Database initialization
β”‚   β”œβ”€β”€ train_model.py             # Training script
β”‚   β”œβ”€β”€ evaluate_model.py           # Evaluation script
β”‚   └── deploy_model.py            # Deployment script
β”œβ”€β”€ tests/
β”‚   β”œβ”€β”€ unit/                      # Unit tests
β”‚   β”œβ”€β”€ integration/               # Integration tests
β”‚   └── performance/               # Performance tests
β”œβ”€β”€ .env.example                   # Environment template
β”œβ”€β”€ .gitignore
β”œβ”€β”€ .pre-commit-config.yaml        # Pre-commit hooks
β”œβ”€β”€ docker-compose.yml             # Docker services
β”œβ”€β”€ Makefile                       # Development tasks
β”œβ”€β”€ pyproject.toml                 # Project metadata & dependencies
β”œβ”€β”€ README.md                      # This file
└── requirements.txt               # Python dependencies

πŸ“¦ Installation

1. Clone Repository

git clone https://github.com/example/stock-predictor-prod.git
cd stock-predictor-prod

2. Set Up Python Environment

Using venv:

python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

Using conda:

conda create -n stock-pred python=3.10
conda activate stock-pred

3. Install Dependencies

# Install pyproject dependencies
pip install -e .

# Install dev dependencies for development
pip install -e ".[dev]"

# Run setup (installs pre-commit hooks)
make setup

4. Configure Environment

# Copy environment template
cp .env.example .env

# Edit .env with your API keys and credentials
nano .env

5. Initialize Database

# Start PostgreSQL via Docker
make docker-up

# Wait for PostgreSQL to be ready, then initialize
make db-init

πŸ’» Usage

Data Ingestion

from production.data_ingestion import AlphaVantageProvider

provider = AlphaVantageProvider(api_key="your_key")
data = provider.get_daily("AAPL", start_date="2023-01-01")

Feature Engineering

from production.features import TechnicalIndicators
from datetime import datetime, timedelta

indicators = TechnicalIndicators(lookback_period=60)
features = indicators.calculate_all(df)

Model Training

from production.training import Trainer
from production.models import LSTMWithAttention

model = LSTMWithAttention(
    input_size=15,  # Number of features
    hidden_size=64,
    num_layers=2,
    dropout=0.2
)

trainer = Trainer(model)
history = trainer.fit(train_loader, val_loader, epochs=50)

Making Predictions

from production.serving import PredictionServer

server = PredictionServer()
prediction = server.predict(
    ticker="AAPL",
    days_ahead=5
)

Via API

curl -X POST "http://localhost:8000/predict" \
  -H "Content-Type: application/json" \
  -d '{
    "ticker": "AAPL",
    "days_ahead": 5,
    "confidence_interval": 0.95
  }'

πŸ§ͺ Testing

# Run all tests
make test

# Run only unit tests
make test-unit

# Run integration tests
make test-integration

# Run with coverage report
make test-cov

# Run specific test file
pytest tests/unit/test_feature_engineering.py -v

πŸ”§ Development

Code Quality

# Format code
make format

# Lint code
make lint

# Type checking
make type-check

# Security scanning
make security

Start Development Containers

# Start all services
make docker-up

# View logs
make docker-logs

# Access services
# PostgreSQL: localhost:5432
# Redis: localhost:6379
# MLflow: http://localhost:5000
# Airflow: http://localhost:8080
# Prometheus: http://localhost:9090
# Grafana: http://localhost:3000
# Adminer: http://localhost:8081

Running Jupyter Notebook

make notebook

# Open http://localhost:8888

πŸš€ Deployment

Docker Build

# Build training image
docker build -f docker/Dockerfile.train -t stock-predictor:latest .

# Build API image
docker build -f docker/Dockerfile.serve -t stock-predictor-api:latest .

Kubernetes Deployment

# Apply configurations
kubectl apply -f k8s/

# Check deployment status
kubectl get pods
kubectl logs -f <pod-name>

Environment Variables

# Production deployment requires these environment variables
export POSTGRES_HOST=prod-db.example.com
export POSTGRES_PASSWORD=<secure-password>
export REDIS_HOST=prod-redis.example.com
export MLFLOW_TRACKING_URI=https://mlflow.example.com
export API_KEY_SECRET=<secure-api-key>

πŸ“Š Monitoring

MLflow Experiment Tracking

make run-tracker  # http://localhost:5000

Metrics & Visualization

# Prometheus: http://localhost:9090
# Grafana: http://localhost:3000 (admin/admin)

Application Logs

# View logs
docker-compose logs -f api

# Or in development
tail -f logs/app.log

πŸ“š Documentation

Build and view complete documentation:

make docs
make docs-serve

Navigate to http://localhost:8000 in your browser.

See the docs/ folder for:

🀝 Contributing

  1. Create a feature branch (git checkout -b feature/amazing-feature)
  2. Commit your changes (git commit -m 'Add amazing feature')
  3. Push to branch (git push origin feature/amazing-feature)
  4. Open a Pull Request

πŸ“ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ”— Related Resources

πŸ“§ Support

For issues, questions, or suggestions:


Built with ❀️ by the ML Engineering Team

About

A production-grade, future-proof stock price prediction system built using modern time-series modeling. It evolves an LSTM-based stock-RNN into an industry-ready platform with robust data pipelines, advanced deep learning models, backtesting, uncertainty estimation, explainability, and full MLOps for real-world deployment.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages