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.
- 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
- 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
- Email notifications (SMTP)
- Slack/Discord webhooks
- Custom webhook support
- Configurable severity thresholds
- Model caching with automatic expiration
- Automated model retraining pipeline
- Database query optimization with indexes
- Pagination for large datasets
- Prometheus metrics for model monitoring
# 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 -fAccess:
- Frontend: http://localhost:3000
- Backend API: http://localhost:8000
- API Docs: http://localhost:8000/docs
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 8000cd frontend
# Install dependencies
npm install
# Configure API URL (optional)
echo "REACT_APP_API_URL=http://localhost:8000" > .env.local
# Start development server
npm startAnomaly-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
| Method | Endpoint | Description |
|---|---|---|
| GET | /api/stocks |
List all tracked stocks |
| GET | /api/stock-data |
Get historical prices |
| Method | Endpoint | Description |
|---|---|---|
| GET | /api/anomalies |
Get detected anomalies |
| POST | /api/detect-anomalies |
Trigger detection |
| GET | /api/detection/status |
Detection system status |
| 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 |
| 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
| 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.
| 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 |
# 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# 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# Start with monitoring
docker-compose --profile monitoring up -d
# Scale backend
docker-compose up -d --scale backend=3GitHub Actions pipeline includes:
- Automated testing on push/PR
- Docker image building
- Security scanning (Trivy)
- Container registry push
See .github/workflows/ci-cd.yml
# Apply migrations
cd backend && alembic upgrade head
# Create new migration
alembic revision --autogenerate -m "Description"Enable with: docker-compose --profile monitoring up -d
- Grafana: http://localhost:3001 (admin/admin)
- Prometheus: http://localhost:9090
Available metrics:
http_requests_total- Request countshttp_request_duration_seconds- Latencyanomaly_detection_runs_total- Detection runsmodel_cache_hits_total- Cache efficiency
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- Create detector class in
backend/anomaly_detection/ - Implement
detect()method returningList[AnomalyResult] - Register in
__init__.py - Add to
/api/detect-anomaliesendpoint
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
- Railway/Render: One-click deploy with
docker-compose.yml - AWS ECS: Enterprise scale with
cloudbuild.yamlpatterns - DigitalOcean: App Platform with managed PostgreSQL
This project is licensed under the MIT License - see the LICENSE file for details.
- Fork the repository
- Create feature branch:
git checkout -b feature/amazing-feature - Commit changes:
git commit -m 'Add amazing feature' - Push to branch:
git push origin feature/amazing-feature - Open a Pull Request
For issues and feature requests, please use the GitHub Issues page.