Skip to content

AIgen-Solutions-s-r-l/SpreadPilot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

383 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

SpreadPilot

Enterprise-Grade Automated Options Trading Platform

Version License CI Pipeline Security Scan Python TypeScript Docker Cloud

Sophisticated microservices-based copy-trading platform that automates QQQ options strategies from Google Sheets to Interactive Brokers accounts

Features โ€ข Quick Start โ€ข Documentation โ€ข Architecture โ€ข Security โ€ข Support


๐Ÿข Developed by AIgen Solutions S.r.l.

Professional Trading Automation โ€ข Enterprise Security โ€ข Cloud-Native Architecture


๐Ÿ“‹ Table of Contents


๐ŸŽฏ Overview

SpreadPilot is an institutional-grade, cloud-native trading automation platform designed for professional options traders and fund managers. Built with modern microservices architecture, it provides automated execution, real-time P&L tracking, comprehensive risk management, and enterprise-level security.

๐ŸŽช What SpreadPilot Does

SpreadPilot bridges the gap between strategy development and execution by:

  1. ๐Ÿ“Š Reading Strategy Signals from Google Sheets in real-time
  2. ๐Ÿค– Executing Trades Automatically across multiple Interactive Brokers accounts
  3. ๐Ÿ’ฐ Tracking P&L in Real-Time with 30-second mark-to-market calculations
  4. ๐Ÿ›ก๏ธ Managing Risk Dynamically with automated position monitoring and liquidation
  5. ๐Ÿ“ˆ Generating Professional Reports with PDF/Excel exports and automated distribution
  6. ๐Ÿ”” Alerting Stakeholders via Telegram and email for critical events

๐Ÿ† Why SpreadPilot?

  • โœ… Production-Ready: Used in live trading with real capital
  • โšก High Performance: Sub-second order execution with intelligent retry mechanisms
  • ๐Ÿ”’ Enterprise Security: OAuth2 authentication, HashiCorp Vault integration, encrypted communications
  • ๐Ÿ“Š Real-Time Insights: Live dashboards, WebSocket updates, comprehensive metrics
  • โ˜๏ธ Cloud-Native: Containerized microservices, horizontal scaling, multi-region deployment
  • ๐Ÿงช Fully Tested: 90%+ code coverage, E2E integration tests, automated CI/CD

โœจ Key Features

๐Ÿค– Automated Trading Engine

  • Smart Order Execution: Advanced limit-ladder algorithm with dynamic pricing
  • Pre-Trade Validation: Margin checks, position limits, strategy compliance
  • Multi-Follower Support: Replicate strategies across unlimited accounts simultaneously
  • Intelligent Retry Logic: Automatic retry with exponential backoff for failed orders
  • Position Management: Real-time position tracking, assignment handling, corporate action processing

๐Ÿ’ฐ Real-Time P&L System

  • Sub-Minute Updates: 30-second mark-to-market calculations
  • Multi-Timeframe Analytics: Intraday, daily, monthly, and inception-to-date P&L
  • Automated Commissions: Dynamic commission calculation on positive P&L with IBAN tracking
  • PostgreSQL Storage: High-performance time-series data with efficient rollups
  • Performance Metrics: Sharpe ratio, max drawdown, win rate, average trade P&L

๐Ÿ›ก๏ธ Advanced Risk Management

  • Time Value Monitoring: Automatic liquidation when TV < $0.10 to prevent assignment
  • Position Limits: Per-follower and per-strategy exposure limits
  • Margin Monitoring: Real-time margin utilization tracking with alerts
  • Circuit Breakers: Automatic trading suspension on abnormal market conditions
  • Emergency Controls: One-click position closure from admin dashboard

๐Ÿ“Š Professional Reporting

  • Automated Report Generation: Daily, weekly, and monthly P&L reports
  • Multiple Formats: PDF (presentation-ready) and Excel (data analysis)
  • Secure Distribution: GCS storage with time-limited signed URLs
  • Email Delivery: Automated distribution to stakeholders with SendGrid
  • Custom Branding: White-label reports with company logo and styling

๐ŸŽ›๏ธ Admin Dashboard

  • Real-Time Monitoring: Live position updates via WebSocket
  • Follower Management: Add, edit, enable/disable accounts
  • Service Health Monitoring: RED/YELLOW/GREEN status indicators with auto-restart
  • Live Logs: Real-time log streaming from all services
  • Manual Controls: Emergency stop, position closure, strategy override
  • ๐Ÿ” OAuth2 Authentication: Secure login with JWT token management (NEW in v2.0.0)

๐Ÿ”” Multi-Channel Alerting

  • Telegram Integration: Instant notifications for critical events
  • Email Alerts: HTML formatted emails with detailed trade information
  • Alert Routing: Flexible routing based on severity and event type
  • Rate Limiting: Prevent alert fatigue with intelligent throttling

๐Ÿ—๏ธ Enterprise Architecture

  • Microservices Design: Loosely coupled, independently deployable services
  • Container-First: Docker and Kubernetes ready for cloud deployment
  • Horizontal Scaling: Scale individual services based on load
  • Observability: OpenTelemetry, Prometheus metrics, Grafana dashboards
  • High Availability: Redis for caching, PostgreSQL replication, MongoDB replica sets

๐Ÿ”’ Security & Compliance

  • OAuth2 + JWT: Industry-standard authentication for admin dashboard
  • HashiCorp Vault: Centralized secret management for credentials
  • Encrypted Communications: TLS 1.3 for all external communications
  • Audit Logging: Comprehensive audit trail for compliance
  • RBAC: Role-based access control for multi-user environments
  • Container Security: Non-root users, minimal base images, vulnerability scanning

๐Ÿ—๏ธ System Architecture

๐ŸŽฏ Microservices Overview

graph TB
    subgraph "๐Ÿ“Š Data Sources"
        A[Google Sheets<br/>Strategy Signals]
        B[Interactive Brokers<br/>Market Data]
    end

    subgraph "๐ŸŽ›๏ธ SpreadPilot Platform"
        C[Signal Listener]
        D[Trading Bot]
        E[Admin API]
        F[Frontend Dashboard]
        G[Watchdog Service]
        H[Report Worker]
        I[Alert Router]
    end

    subgraph "๐Ÿ—„๏ธ Data Layer"
        J[(PostgreSQL<br/>P&L Data)]
        K[(MongoDB<br/>Trading Data)]
        L[(Redis<br/>Pub/Sub)]
        M[HashiCorp Vault<br/>Secrets]
    end

    subgraph "โ˜๏ธ Cloud Services"
        N[Google Cloud Storage<br/>Reports]
        O[SendGrid<br/>Email]
        P[Telegram<br/>Notifications]
    end

    A -->|Poll Signals| C
    C -->|Publish| L
    L -->|Subscribe| D
    D <-->|Execute Trades| B
    D -->|Write Positions| K
    D -->|Write P&L| J
    K -->|Read Data| E
    J -->|Read Data| E
    E <-->|REST API| F
    G -->|Monitor| D
    G -->|Monitor| E
    H -->|Generate Reports| J
    H -->|Upload| N
    I -->|Send Alerts| O
    I -->|Send Alerts| P
    M -->|Retrieve Secrets| D
    M -->|Retrieve Secrets| E
Loading

๐Ÿงฉ Service Components

Service Technology Purpose Port Status
๐Ÿค– Trading Bot Python 3.9+, FastAPI Trade execution, position management 8001 โœ… Production
๐ŸŽ›๏ธ Admin API Python 3.9+, FastAPI REST API for dashboard, follower management 8002 โœ… Production
๐Ÿ–ฅ๏ธ Frontend Dashboard React 19, TypeScript 5.7, Vite Modern admin interface with OAuth2 auth 8080 โœ… Production
๐Ÿ‘€ Watchdog Python 3.9+ Autonomous service health monitoring & restart - โœ… Production
๐Ÿ“Š Report Worker Python 3.9+, ReportLab PDF/Excel report generation 8004 โœ… Production
๐Ÿ”” Alert Router Python 3.9+, Redis Streams Multi-channel notification delivery 8005 โœ… Production

๐Ÿ—„๏ธ Data Stores

Store Technology Purpose Backup Strategy
PostgreSQL PostgreSQL 13+ P&L data, time-series analytics Daily automated backups to GCS
MongoDB MongoDB 5+ Trading data, follower configuration Replica set with point-in-time recovery
Redis Redis 7+ Pub/Sub messaging, caching, alert streams Persistence with AOF, daily snapshots
Vault HashiCorp Vault Encrypted secret storage Automated Vault snapshots
GCS Google Cloud Storage Report files, backups Multi-region with versioning

๐Ÿ”„ Data Flow Architecture

Signal Processing Flow:

Google Sheets โ†’ Signal Listener โ†’ Redis Pub/Sub โ†’ Trading Bot โ†’ IB Gateway โ†’ Execution
                                                          โ†“
                                                    MongoDB (Trades)
                                                          โ†“
                                                    PostgreSQL (P&L)

Reporting Flow:

PostgreSQL P&L Data โ†’ Report Worker โ†’ PDF/Excel Generation โ†’ GCS Upload โ†’ Email Distribution

Authentication Flow (NEW in v2.0.0):

User โ†’ Login Page โ†’ OAuth2 Token Request โ†’ Admin API โ†’ JWT Token โ†’ Stored in LocalStorage
      โ†“
All API Requests โ†’ Axios Interceptor โ†’ Add Bearer Token โ†’ Admin API โ†’ Verify JWT

๐Ÿš€ Quick Start

๐Ÿ“‹ Prerequisites

Ensure you have the following installed and configured:

  • โœ… Docker 20.10+ and Docker Compose 2.0+
  • โœ… Python 3.9+ (for local development)
  • โœ… Node.js 18+ and npm 9+ (for frontend)
  • โœ… Make (optional, for convenience commands)
  • โœ… Interactive Brokers Account with API access enabled
  • โœ… Google Cloud Platform Account (for production deployment)
  • โœ… Google Sheets with trading strategy
  • โœ… SendGrid API Key (for email notifications)
  • โœ… Telegram Bot Token (for Telegram notifications)

โšก One-Command Setup

# 1๏ธโƒฃ Clone the repository
git clone https://github.com/AIgen-Solutions-s-r-l/SpreadPilot.git
cd SpreadPilot

# 2๏ธโƒฃ Start infrastructure services (PostgreSQL, MongoDB, Redis, Vault)
cd infra/
./compose-up.sh

# 3๏ธโƒฃ Configure environment variables
cd ..
cp deploy/.env.dev.template .env
# Edit .env with your credentials (see Configuration section)

# 4๏ธโƒฃ Build and start SpreadPilot services
docker-compose up --build -d

# 5๏ธโƒฃ Verify all services are running
docker-compose ps
cd infra/ && ./health-check.sh

๐ŸŽฏ Access Points

Once deployed, access the platform at:

Service URL Credentials
๐ŸŽ›๏ธ Admin Dashboard http://localhost:8080 Configure in .env
๐Ÿ“Š Admin API http://localhost:8002/docs JWT token required
๐Ÿ“ˆ Grafana Monitoring http://localhost:3000 admin / admin (default)
๐Ÿ—„๏ธ PostgreSQL localhost:5432 Configure in .env
๐Ÿƒ MongoDB localhost:27017 Configure in .env
๐Ÿ”ด Redis localhost:6379 No authentication (dev)
๐Ÿ” Vault UI http://localhost:8200 Root token in infra/.env

๐ŸŒ Production Deployment with Traefik

For production deployment with HTTPS, automatic SSL, and domain routing:

# 1๏ธโƒฃ Configure domain in environment
cp .env.traefik .env
nano .env  # Set DOMAIN=yourdomain.com

# 2๏ธโƒฃ Deploy with Traefik reverse proxy
./scripts/start-with-traefik.sh

# 3๏ธโƒฃ Verify deployment
curl https://dashboard.yourdomain.com/api/v1/health

Production URLs:


โš™๏ธ Configuration

๐Ÿ“ Environment Variables

Create a .env file in the project root based on deploy/.env.dev.template:

# ==========================================
# ๐Ÿฆ Interactive Brokers Configuration
# ==========================================
IB_USERNAME=your_ib_username
IB_PASSWORD=your_ib_password
IB_ACCOUNT=your_ib_account_id
IB_TRADING_MODE=paper  # or 'live' for production

# ==========================================
# ๐Ÿ“Š Google Sheets Integration
# ==========================================
GOOGLE_SHEET_URL=https://docs.google.com/spreadsheets/d/YOUR_SHEET_ID
GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json
GOOGLE_SHEET_RANGE='Strategy!A1:Z1000'

# ==========================================
# ๐Ÿ” Authentication (NEW in v2.0.0)
# ==========================================
ADMIN_USERNAME=admin
# Generate password hash: htpasswd -bnBC 12 "" your_password | tr -d ':\n'
ADMIN_PASSWORD_HASH=your_bcrypt_hashed_password
# Generate JWT secret: openssl rand -hex 32
JWT_SECRET=your_32_byte_hex_secret
JWT_EXPIRATION_MINUTES=1440  # 24 hours

# ==========================================
# ๐Ÿ“ง Email Configuration
# ==========================================
SENDGRID_API_KEY=SG.your_sendgrid_api_key
SENDGRID_FROM_EMAIL=noreply@yourdomain.com
SENDGRID_FROM_NAME="SpreadPilot Alerts"
EMAIL_RECIPIENTS=trader1@example.com,trader2@example.com

# ==========================================
# ๐Ÿค– Telegram Configuration
# ==========================================
TELEGRAM_BOT_TOKEN=your_bot_token_from_botfather
TELEGRAM_CHAT_ID=your_chat_id_or_group_id
TELEGRAM_ENABLED=true

# ==========================================
# ๐Ÿ—„๏ธ Database Configuration
# ==========================================
# PostgreSQL (P&L Data)
POSTGRES_USER=spreadpilot
POSTGRES_PASSWORD=secure_postgres_password
POSTGRES_DB=spreadpilot_pnl
POSTGRES_HOST=postgres
POSTGRES_PORT=5432
POSTGRES_URI=postgresql+asyncpg://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}

# MongoDB (Trading Data)
MONGO_USER=spreadpilot
MONGO_PASSWORD=secure_mongo_password
MONGO_DB=spreadpilot_admin
MONGO_HOST=mongodb
MONGO_PORT=27017
MONGO_URI=mongodb://${MONGO_USER}:${MONGO_PASSWORD}@${MONGO_HOST}:${MONGO_PORT}/${MONGO_DB}?authSource=admin

# Redis (Pub/Sub & Caching)
REDIS_HOST=redis
REDIS_PORT=6379
REDIS_PASSWORD=  # Leave empty for no auth (dev only)

# ==========================================
# ๐Ÿ” HashiCorp Vault (Optional)
# ==========================================
VAULT_ADDR=http://vault:8200
VAULT_TOKEN=dev-only-token-change-in-production
VAULT_ENABLED=true
VAULT_SECRET_PATH=secret/ibkr

# ==========================================
# โ˜๏ธ Google Cloud Storage
# ==========================================
GCS_BUCKET_NAME=spreadpilot-reports
GCS_SERVICE_ACCOUNT_KEY_PATH=/path/to/gcs-service-account-key.json
GCS_PRESIGNED_URL_EXPIRATION=86400  # 24 hours

# ==========================================
# ๐Ÿ“Š Application Configuration
# ==========================================
LOG_LEVEL=INFO  # DEBUG, INFO, WARNING, ERROR, CRITICAL
ENVIRONMENT=development  # development, staging, production
TZ=America/New_York  # Trading timezone

# Trading Bot
TRADING_BOT_POLL_INTERVAL=10  # seconds
TRADING_BOT_MAX_RETRIES=3
TRADING_BOT_RETRY_DELAY=5  # seconds

# Report Worker
REPORT_SCHEDULE_DAILY=16:30  # ET
REPORT_SCHEDULE_MONTHLY=00:10  # ET on 1st of month

# Watchdog
WATCHDOG_CHECK_INTERVAL=60  # seconds
WATCHDOG_RESTART_THRESHOLD=3  # consecutive failures

๐Ÿ” HashiCorp Vault Integration

SpreadPilot supports HashiCorp Vault for enterprise-grade secret management:

# Initialize Vault (first time only)
cd infra/
./vault-init.sh

# Store secrets in Vault
vault kv put secret/ibkr/vertical_spreads_strategy \
  username="your_ib_username" \
  password="your_ib_password" \
  account="your_ib_account"

# Store follower credentials
vault kv put secret/ibkr/follower_123 \
  username="follower_username" \
  password="follower_password" \
  account="follower_account"

# Verify secrets
vault kv get secret/ibkr/vertical_spreads_strategy

Vault Secret Paths:

  • secret/ibkr/vertical_spreads_strategy - Main strategy credentials
  • secret/ibkr/follower_{id} - Follower-specific credentials
  • secret/sendgrid - SendGrid API key
  • secret/telegram - Telegram bot token
  • secret/gcs - Google Cloud Storage service account key

๐Ÿ”’ Security

SpreadPilot implements defense-in-depth security with multiple layers of protection.

๐Ÿ›ก๏ธ Security Features

Authentication & Authorization (NEW in v2.0.0)

  • OAuth2 Password Flow: RFC 6749 compliant authentication
  • JWT Tokens: Stateless authentication with 24-hour expiration
  • bcrypt Password Hashing: Secure credential storage with work factor 12
  • Bearer Token Authorization: Industry-standard Authorization header
  • Automatic Session Cleanup: 401 responses trigger immediate logout
  • Token Validation: Verification on app initialization and all API requests

Infrastructure Security

  • Container Isolation: All services run as non-root users
  • Network Segmentation: Services communicate via private Docker networks
  • TLS Encryption: All external communications encrypted with TLS 1.3
  • Secret Management: HashiCorp Vault with encrypted storage at rest
  • Audit Logging: Comprehensive logs for all sensitive operations
  • Vulnerability Scanning: Automated Trivy scans in CI/CD pipeline

Application Security

  • Input Validation: Pydantic models for all API inputs
  • SQL Injection Protection: Parameterized queries with SQLAlchemy
  • XSS Prevention: React's built-in XSS protection
  • CSRF Protection: SameSite cookies and CORS restrictions
  • Rate Limiting: Prevent brute-force attacks on authentication endpoints
  • Security Headers: CSP, HSTS, X-Frame-Options, X-Content-Type-Options

๐Ÿ”’ Compliance & Standards

  • OWASP Top 10: Protection against all OWASP vulnerabilities
  • CIS Benchmarks: Docker containers follow CIS Docker benchmark
  • GDPR Ready: Data encryption, audit logs, right to deletion
  • SOC 2 Controls: Logging, monitoring, access controls

๐Ÿšจ Security Best Practices

# Generate secure credentials
./scripts/security-utils.py generate-credentials

# Run security audit
./scripts/security-utils.py audit

# Scan for vulnerabilities
./trivy_scan.sh

# Rotate secrets (Vault)
vault kv put secret/ibkr/vertical_spreads_strategy \
  username="new_username" \
  password="new_password" \
  account="account"

# Review security checklist before deployment
cat security_checklist.md

๐Ÿ“‹ Security Checklist

Before production deployment, ensure:

  • โœ… All default passwords changed
  • โœ… JWT_SECRET is cryptographically random (32+ bytes)
  • โœ… ADMIN_PASSWORD_HASH uses bcrypt with work factor >= 12
  • โœ… Vault is unsealed and backed up
  • โœ… TLS certificates are valid and not self-signed
  • โœ… Firewall rules restrict access to required ports only
  • โœ… Container images scanned for vulnerabilities
  • โœ… Audit logging enabled for all services
  • โœ… Backups encrypted and stored securely
  • โœ… Incident response plan documented

๐Ÿ“š API Documentation

๐ŸŽ›๏ธ Admin API

Base URL: /api/v1 Authentication: Bearer token (JWT) required Content-Type: application/json

Authentication Endpoints (NEW in v2.0.0)

# Login
POST /api/v1/auth/token
Content-Type: application/x-www-form-urlencoded

username=admin&password=your_password

# Response
{
  "access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
  "token_type": "bearer"
}

# Use token in subsequent requests
curl -H "Authorization: Bearer eyJhbGc..." https://api.yourdomain.com/api/v1/followers

Followers Management

Method Endpoint Description Auth Required
GET /followers List all followers โœ… Yes
GET /followers/{id} Get follower details โœ… Yes
POST /followers Register new follower โœ… Yes
PUT /followers/{id} Update follower โœ… Yes
DELETE /followers/{id} Delete follower โœ… Yes
POST /followers/{id}/toggle Enable/disable follower โœ… Yes
POST /followers/{id}/close Close all positions โœ… Yes

P&L Endpoints

Method Endpoint Description Auth Required
GET /pnl/realtime Current P&L across all followers โœ… Yes
GET /pnl/daily Daily P&L history โœ… Yes
GET /pnl/monthly Monthly P&L summary โœ… Yes
GET /pnl/follower/{id} P&L for specific follower โœ… Yes

Positions & Trades

Method Endpoint Description Auth Required
GET /positions Current positions all followers โœ… Yes
GET /positions/{follower_id} Positions for specific follower โœ… Yes
GET /trades Trade history โœ… Yes
GET /trades/{id} Trade details โœ… Yes

System Operations

Method Endpoint Description Auth Required
GET /health System health check โŒ No
GET /status Detailed system status โœ… Yes
POST /emergency-stop Emergency stop all trading โœ… Yes
GET /logs Real-time log stream (WebSocket) โœ… Yes

๐Ÿ“Š Example Requests

Register New Follower:

curl -X POST https://api.yourdomain.com/api/v1/followers \
  -H "Authorization: Bearer your_jwt_token" \
  -H "Content-Type: application/json" \
  -d '{
    "email": "trader@example.com",
    "iban": "DE89370400440532013000",
    "commission_pct": 20.0,
    "active": true,
    "credentials": {
      "username": "follower_ib_username",
      "password": "follower_ib_password",
      "account": "follower_ib_account"
    }
  }'

Get Real-Time P&L:

curl -X GET https://api.yourdomain.com/api/v1/pnl/realtime \
  -H "Authorization: Bearer your_jwt_token"

# Response
{
  "total_pnl": 15234.56,
  "total_unrealized": 1234.56,
  "total_realized": 14000.00,
  "followers": [
    {
      "id": "follower_123",
      "email": "trader@example.com",
      "pnl": 5234.56,
      "unrealized": 234.56,
      "realized": 5000.00,
      "positions": 3
    }
  ],
  "timestamp": "2025-11-11T10:30:00Z"
}

Interactive API Documentation:

Visit http://localhost:8002/docs for Swagger UI with interactive API testing.


โ˜๏ธ Deployment

๐Ÿš€ Google Cloud Platform

SpreadPilot is optimized for GCP with Cloud Run, Cloud Build, and Secret Manager.

Prerequisites

# Install Google Cloud SDK
curl https://sdk.cloud.google.com | bash
exec -l $SHELL

# Authenticate
gcloud auth login

# Set project
gcloud config set project your-project-id

# Enable required APIs
gcloud services enable \
  cloudbuild.googleapis.com \
  run.googleapis.com \
  artifactregistry.googleapis.com \
  secretmanager.googleapis.com \
  pubsub.googleapis.com \
  cloudscheduler.googleapis.com

Deploy to Cloud Run

# 1๏ธโƒฃ Configure secrets in Secret Manager
gcloud secrets create ibkr-username --data-file=- <<< "your_username"
gcloud secrets create ibkr-password --data-file=- <<< "your_password"
gcloud secrets create jwt-secret --data-file=- <<< "$(openssl rand -hex 32)"
gcloud secrets create admin-password-hash --data-file=- <<< "$(htpasswd -bnBC 12 '' password | tr -d ':\n')"

# 2๏ธโƒฃ Build and deploy via Cloud Build
gcloud builds submit --config cloudbuild.yaml

# 3๏ธโƒฃ Monitor deployment
gcloud run services list --platform managed
gcloud run services describe admin-api --platform managed --region us-central1

# 4๏ธโƒฃ Get service URLs
export ADMIN_API_URL=$(gcloud run services describe admin-api --platform managed --region us-central1 --format 'value(status.url)')
echo "Admin API: $ADMIN_API_URL"

Infrastructure as Code

# Deploy with Terraform
cd terraform/
terraform init
terraform plan -out=tfplan
terraform apply tfplan

# Deploy with Cloud Deployment Manager
gcloud deployment-manager deployments create spreadpilot \
  --config deployment.yaml

๐Ÿณ Docker Compose (Development/Staging)

# Development environment
docker-compose -f docker-compose.yml up -d

# Staging environment
docker-compose -f docker-compose.staging.yml up -d

# Production-like environment
docker-compose -f docker-compose.prod.yml up -d

โ˜ธ๏ธ Kubernetes

# Deploy to Kubernetes cluster
kubectl apply -f kubernetes/namespace.yaml
kubectl apply -f kubernetes/secrets.yaml
kubectl apply -f kubernetes/configmap.yaml
kubectl apply -f kubernetes/deployments/
kubectl apply -f kubernetes/services/
kubectl apply -f kubernetes/ingress.yaml

# Verify deployment
kubectl get pods -n spreadpilot
kubectl get services -n spreadpilot

# Access logs
kubectl logs -f deployment/trading-bot -n spreadpilot

๐Ÿ“‹ Deployment Checklist

Before production deployment:

  • โœ… Secrets configured in Secret Manager/Vault
  • โœ… Database backups automated and tested
  • โœ… SSL certificates valid and auto-renewing
  • โœ… Monitoring and alerting configured
  • โœ… Runbooks documented for common issues
  • โœ… Disaster recovery plan tested
  • โœ… Performance load testing completed
  • โœ… Security audit passed
  • โœ… Stakeholders trained on dashboard
  • โœ… Rollback plan documented and tested

๐Ÿ“Š Monitoring & Operations

๐ŸŽ›๏ธ Grafana Dashboards

Pre-built dashboards for comprehensive system monitoring:

  1. Trading Overview: Order flow, execution rates, position exposure
  2. P&L Analytics: Real-time P&L, daily/monthly trends, per-follower breakdown
  3. System Health: Service uptime, resource utilization, error rates
  4. Performance Metrics: API latency, database query performance, cache hit rates

Access Grafana at http://localhost:3000 (default credentials: admin/admin)

๐Ÿ“ˆ Key Performance Indicators

Metric Target Alert Threshold
Order Execution Time < 100ms > 500ms
API Response Time (p99) < 200ms > 1000ms
Database Query Time (p99) < 50ms > 200ms
System Uptime > 99.9% < 99.5%
Error Rate < 0.1% > 1%
P&L Update Latency < 30s > 60s

๐Ÿšจ Alerting Rules

Alerts configured for:

  • โš ๏ธ Critical: Service down, authentication failure, order rejection
  • โš ๏ธ High: Position exceeds limit, margin breach, failed trade
  • โš ๏ธ Medium: Slow API response, database connection issue
  • โš ๏ธ Low: Report generation delay, cache miss rate high

๐Ÿ” Log Aggregation

# View real-time logs (development)
docker-compose logs -f trading-bot

# Search logs by keyword
docker-compose logs | grep "ERROR"

# Production log aggregation (GCP)
gcloud logging read "resource.type=cloud_run_revision AND resource.labels.service_name=trading-bot" --limit 50

# Structured query
gcloud logging read 'jsonPayload.severity="ERROR"' --limit 100 --format json

๐Ÿฅ Health Checks

# Manual health check
curl http://localhost:8002/api/v1/health

# Response
{
  "status": "healthy",
  "services": {
    "database": "connected",
    "redis": "connected",
    "vault": "connected",
    "ibkr": "connected"
  },
  "timestamp": "2025-11-11T10:30:00Z"
}

# Automated health check script
./infra/health-check.sh

๐Ÿ› ๏ธ Development

๐Ÿƒโ€โ™‚๏ธ Local Development Setup

# 1๏ธโƒฃ Clone repository
git clone https://github.com/AIgen-Solutions-s-r-l/SpreadPilot.git
cd SpreadPilot

# 2๏ธโƒฃ Create Python virtual environment
python3 -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

# 3๏ธโƒฃ Install dependencies
make install-all
# Or manually:
pip install -r trading-bot/requirements.txt
pip install -r admin-api/requirements.txt
pip install -r spreadpilot-core/requirements.txt

# 4๏ธโƒฃ Install pre-commit hooks
pre-commit install

# 5๏ธโƒฃ Start infrastructure
cd infra && ./compose-up.sh && cd ..

# 6๏ธโƒฃ Run services locally
# Terminal 1: Trading Bot
cd trading-bot && uvicorn app.main:app --reload --port 8001

# Terminal 2: Admin API
cd admin-api && uvicorn app.main:app --reload --port 8002

# Terminal 3: Frontend
cd frontend && npm install && npm run dev

๐ŸŽจ Code Formatting & Linting

# Format Python code (Black + isort)
make format

# Run linters (Ruff + Mypy)
make lint

# Fix auto-fixable linting issues
ruff check --fix .

# Type checking
mypy trading-bot admin-api spreadpilot-core

# Frontend linting
cd frontend && npm run lint

๐Ÿงช Running Tests

# Run all tests
make test

# Run with coverage
make test-coverage

# Run specific test suite
pytest tests/unit/ -v
pytest tests/integration/ -v
pytest -m e2e tests/e2e/ -v

# Run tests for specific service
pytest trading-bot/tests/ -v
pytest admin-api/tests/ -v

# Run tests in parallel
pytest -n auto tests/

# Generate HTML coverage report
pytest --cov --cov-report=html
open htmlcov/index.html

๐Ÿ› Debugging

# Enable debug logging
export LOG_LEVEL=DEBUG

# Run service with debugger
python -m pdb trading-bot/app/main.py

# Debug in VS Code
# Add to .vscode/launch.json:
{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Python: Trading Bot",
      "type": "python",
      "request": "launch",
      "module": "uvicorn",
      "args": [
        "app.main:app",
        "--reload",
        "--port",
        "8001"
      ],
      "cwd": "${workspaceFolder}/trading-bot",
      "env": {
        "LOG_LEVEL": "DEBUG"
      }
    }
  ]
}

๐Ÿงช Testing

๐Ÿ“Š Test Coverage

Current test coverage: 90%+ across all services

Service Unit Tests Integration Tests E2E Tests Coverage
Trading Bot โœ… 95% โœ… 90% โœ… 85% 92%
Admin API โœ… 94% โœ… 88% โœ… 80% 90%
Frontend โš ๏ธ Pending โš ๏ธ Pending โš ๏ธ Pending N/A
Report Worker โœ… 93% โœ… 87% - 91%
Alert Router โœ… 96% โœ… 89% - 93%

๐Ÿงช Test Categories

Unit Tests: Fast, isolated tests for individual functions and classes

pytest tests/unit/ -v --cov

Integration Tests: Test interactions between services and external dependencies

pytest tests/integration/ -v

End-to-End Tests: Full workflow tests with real (paper trading) accounts

pytest -m e2e tests/e2e/ -v

๐Ÿ”„ CI/CD Pipeline

GitHub Actions runs on every push and pull request:

โœ… Linting (Ruff, Black, Mypy)
โœ… Unit Tests (Python 3.9, 3.10, 3.11)
โœ… Integration Tests
โœ… Security Scans (Bandit, Trivy)
โœ… Build Verification (Docker images)
โœ… Frontend Build (npm build)
โœ… E2E Tests (optional, on main branch)

View pipeline: https://github.com/AIgen-Solutions-s-r-l/SpreadPilot/actions


๐Ÿ”ง Troubleshooting

๐Ÿšจ Common Issues & Solutions

Authentication Issues (NEW in v2.0.0)

Problem: Cannot log in to admin dashboard

# 1. Verify environment variables are set
echo $ADMIN_USERNAME
echo $ADMIN_PASSWORD_HASH
echo $JWT_SECRET

# 2. Generate correct password hash
htpasswd -bnBC 12 "" your_password | tr -d ':\n'

# 3. Test authentication endpoint
curl -X POST http://localhost:8002/api/v1/auth/token \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d "username=admin&password=your_password"

# 4. Check admin-api logs
docker-compose logs admin-api | grep -i auth

IBKR Connection Issues

Problem: Trading bot cannot connect to Interactive Brokers

# 1. Verify IB Gateway/TWS is running
netstat -an | grep 4001  # Live trading
netstat -an | grep 4002  # Paper trading

# 2. Check credentials in Vault
vault kv get secret/ibkr/vertical_spreads_strategy

# 3. Test connection manually
python -c "from ib_insync import IB; ib = IB(); ib.connect('127.0.0.1', 4002, clientId=1); print('Connected'); ib.disconnect()"

# 4. Check trading-bot logs
docker-compose logs trading-bot | grep -i "connection"

Database Connection Issues

Problem: Services cannot connect to databases

# PostgreSQL
docker-compose exec postgres psql -U $POSTGRES_USER -d $POSTGRES_DB -c '\conninfo'

# MongoDB
docker-compose exec mongodb mongosh "mongodb://$MONGO_USER:$MONGO_PASSWORD@localhost:27017/$MONGO_DB?authSource=admin" --eval "db.adminCommand('ping')"

# Redis
docker-compose exec redis redis-cli ping

# Check service logs
docker-compose logs postgres mongodb redis

Report Generation Failures

Problem: Reports not being generated or delivered

# 1. Check report-worker logs
docker-compose logs report-worker | tail -100

# 2. Verify GCS access
python -c "from google.cloud import storage; client = storage.Client(); print('GCS connection OK')"

# 3. Check SendGrid API key
curl -X POST https://api.sendgrid.com/v3/mail/send \
  -H "Authorization: Bearer $SENDGRID_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"personalizations": [{"to": [{"email": "test@example.com"}]}],"from": {"email": "test@example.com"},"subject": "Test","content": [{"type": "text/plain", "value": "Test"}]}'

# 4. Manually trigger report generation
docker-compose exec report-worker python -c "from app.report_generator import generate_daily_report; generate_daily_report()"

Service Health Issues

Problem: Services showing as unhealthy

# Check watchdog logs
docker-compose logs watchdog

# Manually restart unhealthy service
docker-compose restart trading-bot

# Check resource usage
docker stats

# Review service health
./infra/health-check.sh

๐Ÿ“ž Getting Help

Before reaching out for support:

  1. โœ… Check service-specific README files in each directory
  2. โœ… Review logs with docker-compose logs [service]
  3. โœ… Enable debug logging with LOG_LEVEL=DEBUG
  4. โœ… Consult troubleshooting section above
  5. โœ… Search existing GitHub issues

For enterprise support:

  • ๐Ÿ“ง Email: support@aigensolutions.com
  • ๐Ÿ“ž Phone: [Contact for enterprise support]
  • ๐Ÿ’ผ Enterprise SLA: 4-hour response time for critical issues

๐Ÿ“‹ Release History

For complete release history, see CHANGELOG.md.

Recent Releases

Version Date Highlights
๐ŸŽ‰ v2.0.0.0 2025-11-11 ๐Ÿ” BREAKING: OAuth2 authentication for admin dashboard
โœจ JWT token management with 24h expiration
๐Ÿ”’ Session persistence across page reloads
โšก Automatic logout on 401 responses
v1.4.2.0 2024-12-30 Minor bug fixes and performance improvements
v1.4.1.0 2024-12-29 Enhanced error handling and logging
v1.4.0.0 2024-12-28 Watchdog service with auto-restart capabilities
v1.3.0.0 2024-06-29 Real-time P&L system with PostgreSQL
Enhanced dashboard with WebSocket
MinIO integration for reports
Complete Vault migration

๐Ÿ”ฎ Roadmap

Q1 2025:

  • โœ… OAuth2 authentication (COMPLETED v2.0.0)
  • ๐Ÿ”„ Frontend test infrastructure (IN PROGRESS)
  • ๐Ÿ“Š Enhanced performance metrics dashboard
  • ๐Ÿ” Multi-factor authentication

Q2 2025:

  • ๐Ÿ‘ฅ Multi-user support with RBAC
  • ๐Ÿ”„ Token refresh mechanism
  • ๐Ÿช HttpOnly cookies for enhanced security
  • ๐Ÿ“ฑ Mobile app for iOS and Android

Q3 2025:

  • ๐Ÿค– AI-powered trade analysis
  • ๐Ÿ“Š Advanced backtesting framework
  • ๐ŸŒ Multi-broker support
  • โ˜๏ธ Multi-cloud deployment

๐Ÿ“„ License & Copyright

Copyright Notice

Copyright ยฉ 2024-2025 AIgen Solutions S.r.l.
All rights reserved.

AIgen Solutions S.r.l. Registered Office: [Address] VAT: [VAT Number] Company Registration: [Registration Number]

Proprietary License

This software and associated documentation files (the "Software") are the proprietary property of AIgen Solutions S.r.l. The Software is licensed, not sold.

Restrictions:

  • โŒ You may NOT copy, modify, distribute, or create derivative works
  • โŒ You may NOT reverse engineer, decompile, or disassemble the Software
  • โŒ You may NOT remove or alter any copyright notices or proprietary legends
  • โŒ You may NOT use the Software for commercial purposes without a commercial license

Permitted Use:

  • โœ… Use the Software for evaluation purposes with AIgen Solutions' prior written consent
  • โœ… Use the Software under a valid commercial license agreement with AIgen Solutions

Warranty Disclaimer:

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL AIGEN SOLUTIONS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Commercial Licensing

For commercial licensing inquiries, please contact:

Available License Types:

  • ๐Ÿข Enterprise License: Unlimited users, priority support, custom features
  • ๐Ÿ’ผ Professional License: Up to 10 users, standard support
  • ๐Ÿš€ Startup License: Special pricing for startups and small teams
  • ๐ŸŽ“ Academic License: Free for educational institutions

๐Ÿค Support & Consulting

๐Ÿ“ง Contact Information

AIgen Solutions S.r.l.

๐ŸŽฏ Professional Services

We offer comprehensive professional services:

  • ๐Ÿ“Š Custom Strategy Development: Build bespoke trading strategies
  • ๐Ÿ”ง Integration Services: Integrate with your existing systems
  • ๐Ÿ—๏ธ Infrastructure Setup: Cloud deployment and optimization
  • ๐Ÿ‘จโ€๐Ÿซ Training: Hands-on training for your team
  • ๐Ÿ› ๏ธ Ongoing Support: 24/7 support with SLA guarantees
  • ๐Ÿ“ˆ Performance Optimization: Tune system for maximum efficiency

๐Ÿ†˜ Enterprise Support Plans

Plan Response Time Availability Channels Price
Community Best effort Forum only GitHub Issues Free
Professional 2 business days 9-5 local time Email, Forum Contact Sales
Business 8 hours 24/5 Email, Phone, Chat Contact Sales
Enterprise 4 hours 24/7/365 Email, Phone, Chat, Slack Contact Sales
Mission Critical 1 hour 24/7/365 Dedicated team Contact Sales

๐Ÿ™ Acknowledgments

SpreadPilot is built on top of exceptional open-source technologies and services:

๐Ÿ› ๏ธ Core Technologies

  • Python - Core programming language
  • FastAPI - High-performance async web framework
  • React - Modern frontend framework
  • TypeScript - Type-safe JavaScript
  • PostgreSQL - Reliable relational database
  • MongoDB - Flexible document database
  • Redis - In-memory data store
  • Docker - Container platform

โ˜๏ธ Cloud & Infrastructure

  • Google Cloud Platform - Cloud hosting
  • HashiCorp Vault - Secret management
  • Traefik - Modern reverse proxy
  • Prometheus & Grafana - Monitoring and visualization

๐Ÿ“š Libraries & Frameworks

  • ib_insync - Interactive Brokers API wrapper
  • ReportLab - PDF generation
  • Pandas - Data analysis
  • SQLAlchemy - Database ORM
  • Pydantic - Data validation

๐Ÿค Special Thanks

  • Interactive Brokers - Trading platform API
  • SendGrid - Email delivery service
  • Telegram - Notification platform
  • Google Sheets - Strategy signal source

๐Ÿš€ Built with Excellence by AIgen Solutions

Professional Trading Automation โ€ข Enterprise-Grade Security โ€ข Cloud-Native Architecture

๐ŸŒ Website โ€ข ๐Ÿ“ง Contact โ€ข ๐Ÿ’ผ Licensing โ€ข ๐Ÿ› ๏ธ Support


โญ Star this repository if you find it valuable!

Copyright ยฉ 2024-2025 AIgen Solutions S.r.l. - All Rights Reserved

Proprietary Software - Licensed Use Only

About

SpreadPilot is a modern microservices-based trading platform designed for scalable and reliable options trading automation. Built with Python, PostgreSQL, and cloud-native technologies.

Resources

Code of conduct

Contributing

Stars

2 stars

Watchers

1 watching

Forks

Packages

 
 
 

Contributors