Skip to content

SahilSatyam/Anomaly-Detection

Repository files navigation

πŸ“ˆ Stock Anomaly Detection System

A full-stack application for real-time stock market monitoring, anomaly detection, and alerting. Uses advanced ML algorithms including LSTM neural networks, Isolation Forest, and time-series forecasting to detect unusual patterns in stock price movements.

Python React FastAPI PostgreSQL Docker License


✨ Features

πŸ” Anomaly Detection

  • Statistical Methods: Bollinger Bands, Z-Score, Volume Analysis
  • Machine Learning: Isolation Forest, LSTM Neural Networks, AutoEncoder
  • Advanced Deep Learning: Transformer AutoEncoder, Variational AutoEncoder (VAE), Temporal Convolutional Network (TCN)
  • Time-Series Forecasting: ARIMA (auto-tuned), Facebook Prophet
  • Hybrid Detection: Consensus-based multi-method analysis

πŸ“Š Visualization

  • Interactive candlestick charts with volume overlay
  • Anomaly markers directly on price charts
  • Severity-based color coding (high/medium/low)
  • Real-time data updates with auto-refresh

🚨 Alert System

  • Email notifications (SMTP)
  • Slack/Discord webhooks
  • Custom webhook support
  • Configurable severity thresholds

⚑ Performance & MLOps

  • Model caching with automatic expiration
  • Automated model retraining pipeline
  • Database query optimization with indexes
  • Pagination for large datasets
  • Prometheus metrics for model monitoring

πŸš€ Quick Start

Option 1: Docker (Recommended)

# Clone the repository
git clone https://github.com/SahilSatyam/Anomaly-Detection.git
cd Anomaly-Detection

# Configure environment
cp .env.example .env
# Edit .env with your settings

# Start all services
docker-compose up -d

# View logs
docker-compose logs -f

Access:

Option 2: Manual Setup

Backend

cd backend

# Create virtual environment
python -m venv venv
source venv/bin/activate  # Windows: venv\Scripts\activate

# Install dependencies
pip install -r requirements.txt

# Configure environment
cp .env.example .env
# Edit .env with your database credentials

# Run migrations
alembic upgrade head

# Start server
uvicorn main:app --reload --port 8000

Frontend

cd frontend

# Install dependencies
npm install

# Configure API URL (optional)
echo "REACT_APP_API_URL=http://localhost:8000" > .env.local

# Start development server
npm start

πŸ“ Project Structure

Anomaly-Detection/
β”œβ”€β”€ backend/                    # FastAPI Backend
β”‚   β”œβ”€β”€ main.py                # API endpoints
β”‚   β”œβ”€β”€ data_collection/       # Stock data fetching
β”‚   β”œβ”€β”€ data_storage/          # Database models & operations
β”‚   β”œβ”€β”€ anomaly_detection/     # Detection algorithms
β”‚   β”‚   β”œβ”€β”€ statistical_methods.py
β”‚   β”‚   β”œβ”€β”€ ml_models.py       # LSTM, Isolation Forest, AutoEncoder
β”‚   β”‚   β”œβ”€β”€ advanced_models.py # Transformer, VAE, TCN
β”‚   β”‚   β”œβ”€β”€ forecasting.py     # ARIMA, Prophet
β”‚   β”‚   β”œβ”€β”€ hybrid_detection.py
β”‚   β”‚   └── model_persistence.py
β”‚   β”œβ”€β”€ alert_system/          # Email & webhook alerts
β”‚   β”œβ”€β”€ alembic/               # Database migrations
β”‚   β”œβ”€β”€ tests/                 # Unit & integration tests
β”‚   β”‚   β”œβ”€β”€ conftest.py        # Pytest fixtures
β”‚   β”‚   β”œβ”€β”€ test_statistical_methods.py
β”‚   β”‚   β”œβ”€β”€ test_ml_models.py
β”‚   β”‚   β”œβ”€β”€ test_model_persistence.py
β”‚   β”‚   └── test_api_endpoints.py
β”‚   β”œβ”€β”€ scripts/               # Automation scripts
β”‚   β”‚   └── scheduled_retrain.py
β”‚   └── Dockerfile
β”‚
β”œβ”€β”€ frontend/                   # React Frontend
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ components/        # UI components
β”‚   β”‚   β”œβ”€β”€ pages/             # Dashboard, Settings
β”‚   β”‚   β”œβ”€β”€ hooks/             # Custom React hooks
β”‚   β”‚   β”œβ”€β”€ utils/             # Utilities (export, validation)
β”‚   β”‚   └── config/            # API configuration
β”‚   β”œβ”€β”€ nginx.conf             # Production config
β”‚   └── Dockerfile
β”‚
β”œβ”€β”€ notebooks/                  # Jupyter notebooks
β”‚   β”œβ”€β”€ GPU_Training_Colab.ipynb
β”‚   └── Model_Evaluation_Benchmarks.ipynb
β”‚
β”œβ”€β”€ docs/                       # Documentation
β”‚   └── MODEL_RETRAINING_PIPELINE.md
β”‚
β”œβ”€β”€ monitoring/                 # Prometheus configs
β”œβ”€β”€ database/                   # Init scripts
β”œβ”€β”€ .github/workflows/          # CI/CD pipelines
β”œβ”€β”€ docker-compose.yml          # Container orchestration
β”œβ”€β”€ Makefile                    # Development commands
└── DEVOPS.md                   # Deployment guide

πŸ”Œ API Endpoints

Stock Data

Method Endpoint Description
GET /api/stocks List all tracked stocks
GET /api/stock-data Get historical prices

Anomaly Detection

Method Endpoint Description
GET /api/anomalies Get detected anomalies
POST /api/detect-anomalies Trigger detection
GET /api/detection/status Detection system status

Models & Alerts

Method Endpoint Description
GET /api/models List cached ML models
DELETE /api/models/{type}/{symbol} Delete cached model
GET /api/alerts/status Alert system status
GET /api/alerts/history Alert history

System

Method Endpoint Description
GET /api/health Health check
GET /api/ready Readiness check
GET /metrics Prometheus metrics

Full API documentation at http://localhost:8000/docs


βš™οΈ Configuration

Environment Variables

Variable Description Default
DATABASE_URL PostgreSQL connection Required
LOG_LEVEL Logging level INFO
LOG_FORMAT json or text text
ALERT_EMAIL_ENABLED Enable email alerts false
SLACK_WEBHOOK_URL Slack notifications -
DISCORD_WEBHOOK_URL Discord notifications -
REACT_APP_API_URL Backend URL for frontend http://localhost:8000

See .env.example for all available options.


πŸ§ͺ Testing

Test Suite Overview

Test Category Description Location
Statistical Methods Bollinger, Z-Score, Volume detection tests/test_statistical_methods.py
ML Models Isolation Forest, LSTM, AutoEncoder tests/test_ml_models.py
Model Persistence Caching, versioning, cleanup tests/test_model_persistence.py
API Endpoints REST API, validation, error handling tests/test_api_endpoints.py

Running Tests

# Run all tests with coverage
make test

# Backend tests with verbose output
cd backend && pytest -v --cov=. --cov-report=html

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

# Skip slow tests (LSTM training)
pytest -m "not slow" -v

# Run only API tests
pytest tests/test_api_endpoints.py -v

# Frontend tests
cd frontend && npm test -- --coverage

Coverage Report

# Generate HTML coverage report
cd backend && pytest --cov=anomaly_detection --cov-report=html

# View report
open htmlcov/index.html  # macOS
start htmlcov/index.html  # Windows

πŸ“¦ Deployment

Docker Compose (Production)

# Start with monitoring
docker-compose --profile monitoring up -d

# Scale backend
docker-compose up -d --scale backend=3

CI/CD

GitHub Actions pipeline includes:

  • Automated testing on push/PR
  • Docker image building
  • Security scanning (Trivy)
  • Container registry push

See .github/workflows/ci-cd.yml

Database Migrations

# Apply migrations
cd backend && alembic upgrade head

# Create new migration
alembic revision --autogenerate -m "Description"

πŸ“Š Monitoring

Prometheus Metrics

Enable with: docker-compose --profile monitoring up -d

Available metrics:

  • http_requests_total - Request counts
  • http_request_duration_seconds - Latency
  • anomaly_detection_runs_total - Detection runs
  • model_cache_hits_total - Cache efficiency

πŸ› οΈ Development

Makefile Commands

make help        # Show all commands
make install     # Install dependencies
make dev         # Start development servers
make test        # Run all tests
make lint        # Run linters
make docker-up   # Start Docker containers
make migrate     # Run database migrations

Adding New Detection Methods

  1. Create detector class in backend/anomaly_detection/
  2. Implement detect() method returning List[AnomalyResult]
  3. Register in __init__.py
  4. Add to /api/detect-anomalies endpoint

☁️ Cloud Deployment

Google Cloud Platform (Recommended for low traffic)

Deploy to Cloud Run for pay-per-use pricing (scales to zero when idle):

# Quick deploy (Windows)
cd deploy/gcp
.\deploy.ps1 -ProjectId "your-project-id"

# Or with external database (free)
.\deploy.ps1 -ProjectId "your-project" -ExternalDbUrl "postgresql://..."

Estimated Cost: ~$0-9/month for low traffic

πŸ“– See full guide: deploy/gcp/README.md

Other Platforms

  • Railway/Render: One-click deploy with docker-compose.yml
  • AWS ECS: Enterprise scale with cloudbuild.yaml patterns
  • DigitalOcean: App Platform with managed PostgreSQL

πŸ“ License

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


🀝 Contributing

  1. Fork the repository
  2. Create feature branch: git checkout -b feature/amazing-feature
  3. Commit changes: git commit -m 'Add amazing feature'
  4. Push to branch: git push origin feature/amazing-feature
  5. Open a Pull Request

πŸ“§ Support

For issues and feature requests, please use the GitHub Issues page.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published