Skip to content

timothynn/trading-pipeline

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Trading Pipeline

A production-grade real-time trading pipeline built with Python, featuring high-frequency data processing, risk management, and analytics capabilities.

πŸš€ Features

Core Architecture

  • Real-time Data Streaming: Apache Kafka for high-throughput message processing
  • In-Memory Caching: Redis for ultra-fast state management and real-time metrics
  • Persistent Storage: PostgreSQL with time-series optimizations
  • WebSocket API: Real-time updates for trading dashboards and alerts

Trading & Risk Management

  • Order Management: Full order lifecycle with validation and execution
  • Risk Controls: VaR calculations, drawdown monitoring, position sizing
  • Circuit Breakers: Automatic trading halts under extreme conditions
  • Real-time Alerts: Configurable risk threshold monitoring

Analytics & Performance

  • Live P&L Tracking: Real-time profit/loss calculation and attribution
  • Technical Indicators: Moving averages, RSI, Bollinger Bands
  • Performance Metrics: Sharpe ratio, max drawdown, win rate analysis
  • Backtesting Framework: Historical strategy simulation and optimization

Production Features

  • Low Latency: <500ms data ingestion to alert, <100ms order processing
  • High Throughput: Handles 10k+ events per second
  • Monitoring: Prometheus metrics, structured logging, health checks
  • Scalability: Horizontal scaling with Kafka partitioning

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Market Data   │───▢│   Apache Kafka  │───▢│  Risk Manager   β”‚
β”‚     Feeds       β”‚    β”‚   (Streaming)   β”‚    β”‚   & Validators  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                β”‚                        β”‚
                                β–Ό                        β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   PostgreSQL    │◀───│     Redis       │───▢│   FastAPI       β”‚
β”‚   (Persistence) β”‚    β”‚   (Cache/State) β”‚    β”‚   (REST/WS)     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                                        β”‚
                                                        β–Ό
                                               β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                                               β”‚   Dashboard     β”‚
                                               β”‚   & Clients     β”‚
                                               β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ› οΈ Quick Start

Prerequisites

  • Nix with flakes enabled
  • devenv and direnv (alternative)
  • Docker and Docker Compose (fallback)
  • Python 3.11+

Development Setup

Choose one of the following methods. Run ./setup-dev.sh to see what's available on your system.

Option 1: Nix Flakes (Recommended)

git clone <repository>
cd trading-pipeline

# Enter development shell
nix develop

# Start services manually
start-postgres
start-redis

# Or use Docker for Kafka
docker-compose up -d kafka

Option 2: devenv (Alternative)

git clone <repository>
cd trading-pipeline
direnv allow  # Activates devenv automatically

Option 3: Docker Only

git clone <repository>
cd trading-pipeline

# Start all services
docker-compose up -d

# Install Python dependencies
python -m venv venv
source venv/bin/activate
pip install -r requirements.txt

πŸ“– For detailed setup instructions and comparisons, see docs/development-setup.md

Service Management

With Nix Flakes:

# Start individual services
start-postgres    # PostgreSQL on localhost:5432
start-redis      # Redis on localhost:6379

# Stop all services
stop-services

# Or use Docker for additional services
docker-compose up -d kafka

With devenv: Services (PostgreSQL and Redis) start automatically when entering the shell.

Running the Application

  1. Run the API:
python -m trading_pipeline.api.main
  1. Access Services:

Docker Deployment

# Start all services
docker-compose up -d

# Check service health
docker-compose ps

# View logs
docker-compose logs -f trading-api

πŸ“Š API Usage

Submit Trading Order

import httpx

response = httpx.post("http://localhost:8000/api/v1/trading/orders", json={
    "symbol": "AAPL",
    "side": "buy",
    "order_type": "limit",
    "quantity": "100",
    "price": "150.50"
})

Get Risk Metrics

response = httpx.get("http://localhost:8000/api/v1/risk/metrics/portfolio_1")
risk_data = response.json()

WebSocket Real-time Updates

const ws = new WebSocket('ws://localhost:8000/ws/live');

ws.onopen = () => {
    // Subscribe to P&L updates
    ws.send(JSON.stringify({
        type: 'subscribe',
        topic: 'pnl_updates'
    }));
};

ws.onmessage = (event) => {
    const data = JSON.parse(event.data);
    console.log('Real-time update:', data);
};

πŸ”§ Configuration

Key environment variables (see .env.example):

# Database
DATABASE_URL=postgresql://trading_user:trading_pass@localhost:5432/trading_pipeline

# Redis
REDIS_URL=redis://localhost:6379

# Kafka
KAFKA_BOOTSTRAP_SERVERS=localhost:9092

# Risk Management
MAX_POSITION_SIZE=100000.0
MAX_PORTFOLIO_RISK=0.02
CIRCUIT_BREAKER_THRESHOLD=0.05

# Performance Targets
MAX_LATENCY_MS=500
TARGET_THROUGHPUT=10000

πŸ§ͺ Testing

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

# Run tests
pytest

# Run with coverage
pytest --cov=trading_pipeline

# Integration tests
pytest tests/integration/

πŸ“ˆ Monitoring & Observability

Metrics (Prometheus)

  • Order processing latency
  • Risk calculation performance
  • Cache hit rates
  • WebSocket connection counts

Logging (Structured)

import structlog
logger = structlog.get_logger()

logger.info(
    "Order processed",
    order_id=order.id,
    symbol=order.symbol,
    latency_ms=processing_time
)

Health Checks

  • /health/ - Basic service status
  • /health/detailed - Full dependency health
  • /health/ready - Kubernetes readiness probe
  • /health/live - Kubernetes liveness probe

🏭 Production Deployment

Kubernetes

apiVersion: apps/v1
kind: Deployment
metadata:
  name: trading-pipeline
spec:
  replicas: 3
  selector:
    matchLabels:
      app: trading-pipeline
  template:
    metadata:
      labels:
        app: trading-pipeline
    spec:
      containers:
      - name: api
        image: trading-pipeline:latest
        ports:
        - containerPort: 8000
        env:
        - name: DATABASE_URL
          valueFrom:
            secretKeyRef:
              name: trading-secrets
              key: database-url

Performance Tuning

  • Database: Connection pooling, read replicas, partitioning
  • Redis: Clustering, memory optimization, eviction policies
  • Kafka: Partition tuning, batch size optimization
  • API: Worker processes, async connection limits

πŸ”’ Security

  • JWT authentication for API access
  • Input validation with Pydantic
  • SQL injection prevention with SQLAlchemy
  • Rate limiting and request size limits
  • Secrets management with environment variables

πŸ“š Project Structure

trading-pipeline/
β”œβ”€β”€ src/trading_pipeline/
β”‚   β”œβ”€β”€ api/                 # FastAPI routes and WebSocket
β”‚   β”œβ”€β”€ core/               # Configuration and models
β”‚   β”œβ”€β”€ data/               # Database and cache layers
β”‚   β”œβ”€β”€ streaming/          # Kafka integration
β”‚   β”œβ”€β”€ risk/               # Risk management engine
β”‚   β”œβ”€β”€ backtesting/        # Strategy backtesting
β”‚   └── analytics/          # Performance calculations
β”œβ”€β”€ tests/
β”‚   β”œβ”€β”€ unit/              # Unit tests
β”‚   └── integration/       # Integration tests
β”œβ”€β”€ deployment/            # Kubernetes manifests
β”œβ”€β”€ monitoring/           # Prometheus/Grafana config
β”œβ”€β”€ docs/                # Additional documentation
β”œβ”€β”€ devenv.nix           # devenv configuration
β”œβ”€β”€ flake.nix            # Nix flakes configuration
β”œβ”€β”€ .envrc               # direnv configuration
└── setup-dev.sh         # Development setup helper

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes with tests
  4. Run the test suite and linting
  5. Submit a pull request

Development Guidelines

  • Follow PEP 8 style guide
  • Add type hints to all functions
  • Write comprehensive tests
  • Update documentation for new features
  • Use structured logging

πŸ“„ License

MIT License - see LICENSE file for details.

πŸ†˜ Support

  • Issues: Create GitHub issues for bugs and feature requests
  • Documentation: Check /docs directory for detailed guides
  • Performance: See monitoring dashboards for system health

πŸš€ Roadmap

  • Machine learning-based risk models
  • Multi-exchange connectivity
  • Advanced order types (iceberg, TWAP, VWAP)
  • Mobile dashboard application
  • Enhanced backtesting with walk-forward analysis

About

High-performance data pipeline processing market data streams with Apache Kafka, Redis, and PostgreSQL. Features risk management, backtesting framework, and real-time analytics dashboard.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors