Skip to content

duboolivier/aegis-cloud-trading-platform

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

29 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

AEGIS Cloud Trading Platform

AEGIS Logo

Enterprise-Grade Cloud Trading Platform with Institutional Risk Controls

License: MIT Python Next.js FastAPI


πŸ“– Table of Contents


🎯 About the Project

AEGIS (Advanced Electronic Global Investment System) is a modern, cloud-native trading platform designed with institutional-grade risk controls and regulatory compliance at its core. Built on event-driven microservices architecture, AEGIS provides real-time order processing, immutable audit trails, and comprehensive risk management.

πŸ›οΈ Project Purpose

The platform was developed to address critical gaps in retail and institutional trading systems:

  1. Regulatory Compliance First: Built-in compliance checks ensure all trades adhere to regulatory requirements before execution (KYC, sanctions screening, trading windows, position limits).

  2. Multi-Layer Risk Management: Sophisticated risk engine prevents catastrophic losses through real-time balance verification, exposure limits, concentration risks, and rate limiting.

  3. Immutable Audit Trail: Every transaction is recorded in an immutable hashchain ledger, providing cryptographic proof of trade integrity for audits and regulatory reporting.

  4. Real-Time Processing: Event-driven architecture powered by Kafka enables microsecond-latency trade processing and real-time risk assessments.

  5. Institutional Controls: Implements controls typically found in institutional trading systems: pre-trade risk checks, compliance reviews, order size limits, and execution price validation.

🎯 Target Use Cases

  • Proprietary Trading Firms: Real-time risk controls and P&L tracking
  • Wealth Management Platforms: Client order management with compliance oversight
  • Broker-Dealers: Multi-client trading with regulatory reporting
  • FinTech Startups: Production-ready trading infrastructure
  • Educational Institutions: Teaching platform for trading systems architecture

✨ Key Features

πŸ”’ Three-Engine Validation Architecture

Every order passes through three independent validation engines in sequence:

  1. Compliance Engine πŸ›‘οΈ

    • Symbol whitelist enforcement (10 approved instruments)
    • User sanctions screening (OFAC, EU sanctions lists)
    • Trading window restrictions (maintenance windows, market hours)
    • Crypto position limits (BTC: 10 max, ETH: 100 max)
    • Large order flagging for manual review
  2. Risk Engine βš–οΈ

    • Real-time balance verification
    • Position size limits (BTC: 5 max per order)
    • Notional value limits ($250k per order)
    • Daily trading limits ($1M per day)
    • Exposure limits ($500k total open positions)
    • Rate limiting (10 orders per minute)
    • Slippage tolerance checks (0.5% max)
  3. Execution Engine 🎯

    • Market impact modeling (spread + slippage)
    • Realistic price execution simulation
    • Trade confirmation and settlement
    • Balance debit/credit accounting
    • Immutable ledger recording

πŸ“Š Trading Features

  • Multi-Asset Support: Equities (AAPL, MSFT, GOOGL, etc.) and Cryptocurrencies (BTC, ETH)
  • Real-Time Market Data: Live price feeds for all instruments
  • Order Management: Full order lifecycle (PENDING β†’ ACCEPTED β†’ EXECUTED/REJECTED)
  • Balance Management: Real-time balance tracking with available/reserved funds
  • Transaction History: Complete audit trail with CSV/PDF export
  • Immutable Ledger: Cryptographic hashchain for trade verification

πŸ” Security & Compliance

  • JWT Authentication: Secure token-based authentication
  • Role-Based Access Control: User permissions and access levels
  • Audit Logging: Structured logging for all system events
  • Data Encryption: Secure communication between services
  • Immutable Records: Hashchain-based ledger prevents tampering

πŸ—οΈ Architecture

System Overview

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                         Frontend (Next.js)                       β”‚
β”‚                    http://localhost:3000                         β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                             β”‚ REST API
                             ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                      API Gateway (FastAPI)                       β”‚
β”‚                    http://localhost:8000                         β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                             β”‚ Kafka Events
                             ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                     Order Processor (Worker)                     β”‚
β”‚                                                                   β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”      β”‚
β”‚  β”‚  Compliance  β”‚ -> β”‚     Risk     β”‚ -> β”‚  Execution   β”‚      β”‚
β”‚  β”‚    Engine    β”‚    β”‚    Engine    β”‚    β”‚    Engine    β”‚      β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜      β”‚
β”‚                             β”‚                     β”‚              β”‚
β”‚                             ↓                     ↓              β”‚
β”‚                      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”       β”‚
β”‚                      β”‚  Balance    β”‚      β”‚   Ledger    β”‚       β”‚
β”‚                      β”‚   Update    β”‚      β”‚  (Hashchain)β”‚       β”‚
β”‚                      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜       β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                             β”‚
                             ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  PostgreSQL  β”‚    Redis     β”‚   Redpanda   β”‚   Adminer    β”‚
β”‚   (Data)     β”‚   (Cache)    β”‚   (Kafka)    β”‚    (UI)      β”‚
β”‚   :5432      β”‚   :6379      β”‚   :9092      β”‚   :8081      β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Event-Driven Flow

1. User places order β†’ 2. API validates & publishes to Kafka
                        ↓
3. Worker consumes event β†’ 4. Compliance Engine validates
                        ↓
5. Risk Engine checks limits β†’ 6. Execution Engine processes
                        ↓
7. Update balances β†’ 8. Write to ledger β†’ 9. Notify user

πŸ› οΈ Technology Stack

Frontend

  • Next.js 14 - React framework with server-side rendering
  • TypeScript - Type-safe development
  • Tailwind CSS - Modern, responsive design
  • jsPDF - PDF export functionality

Backend

  • FastAPI - High-performance Python web framework
  • Python 3.12 - Latest Python features and performance
  • Pydantic - Data validation using Python type annotations
  • psycopg3 - PostgreSQL database adapter

Infrastructure

  • PostgreSQL 17 - Primary database for orders, users, balances
  • Redis 7 - Caching and rate limiting
  • Redpanda - Kafka-compatible event streaming (simpler than Kafka)
  • Docker Compose - Container orchestration

Development Tools

  • Adminer - Database management UI
  • Structured Logging - JSON-based logging for observability
  • Hot Reload - Fast development iteration

πŸš€ Getting Started

Prerequisites

  • Docker Desktop (or Docker Engine + Docker Compose)
  • Git
  • Ports Available: 3000, 8000, 8081, 5432, 6379, 9092

Quick Start

  1. Clone the repository

    git clone https://github.com/yourusername/aegis-cloud-trading-platform.git
    cd aegis-cloud-trading-platform
  2. Start all services

    docker-compose up -d
  3. Wait for services to be healthy (~30 seconds)

    docker-compose ps
  4. Access the platform

  5. Create an account

Useful Commands

# View logs from all services
docker-compose logs -f

# View logs from specific service
docker-compose logs -f back-worker

# Rebuild after code changes
docker-compose build
docker-compose up -d

# Stop all services
docker-compose down

# Stop and remove all data
docker-compose down -v

# Execute commands in containers
docker exec -it aegis-cloud-trading-platform-postgres-1 psql -U app -d trading

πŸ“ Project Structure

aegis-cloud-trading-platform/
β”œβ”€β”€ back/                           # Backend services
β”‚   β”œβ”€β”€ app/
β”‚   β”‚   β”œβ”€β”€ api/                    # REST API endpoints
β”‚   β”‚   β”‚   └── routes/
β”‚   β”‚   β”‚       β”œβ”€β”€ auth.py         # Authentication (register, login)
β”‚   β”‚   β”‚       β”œβ”€β”€ orders.py       # Order management
β”‚   β”‚   β”‚       β”œβ”€β”€ symbols.py      # Market data
β”‚   β”‚   β”‚       β”œβ”€β”€ balance.py      # Account balances
β”‚   β”‚   β”‚       └── ledger.py       # Transaction history
β”‚   β”‚   β”œβ”€β”€ engines/                # Trading engines
β”‚   β”‚   β”‚   β”œβ”€β”€ compliance.py       # Regulatory validation
β”‚   β”‚   β”‚   β”œβ”€β”€ risk.py             # Risk management
β”‚   β”‚   β”‚   └── execution.py        # Trade execution
β”‚   β”‚   β”œβ”€β”€ workers/                # Kafka consumers
β”‚   β”‚   β”‚   └── processor.py        # Main order processor
β”‚   β”‚   β”œβ”€β”€ core/                   # Core utilities
β”‚   β”‚   β”‚   β”œβ”€β”€ config.py           # Configuration
β”‚   β”‚   β”‚   β”œβ”€β”€ security.py         # JWT authentication
β”‚   β”‚   β”‚   └── logging.py          # Structured logging
β”‚   β”‚   β”œβ”€β”€ domain/                 # Domain models
β”‚   β”‚   β”‚   └── models.py           # Pydantic models
β”‚   β”‚   β”œβ”€β”€ infra/                  # Infrastructure
β”‚   β”‚   β”‚   β”œβ”€β”€ db/                 # Database
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ init.sql        # Schema & seed data
β”‚   β”‚   β”‚   β”‚   └── session.py      # Connection pool
β”‚   β”‚   β”‚   β”œβ”€β”€ broker/             # Kafka
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ producer.py     # Event publishing
β”‚   β”‚   β”‚   β”‚   └── consumer.py     # Event consumption
β”‚   β”‚   β”‚   └── redis/              # Cache
β”‚   β”‚   β”‚       └── client.py       # Redis operations
β”‚   β”‚   β”œβ”€β”€ utils/                  # Utilities
β”‚   β”‚   β”‚   └── hashchain.py        # Ledger hashing
β”‚   β”‚   └── main.py                 # FastAPI application
β”‚   β”œβ”€β”€ Dockerfile                  # Backend container
β”‚   └── requirements.txt            # Python dependencies
β”œβ”€β”€ front/                          # Frontend application
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ app/                    # Next.js pages
β”‚   β”‚   β”‚   β”œβ”€β”€ login/              # Login page
β”‚   β”‚   β”‚   β”œβ”€β”€ register/           # Registration page
β”‚   β”‚   β”‚   β”œβ”€β”€ dashboard/          # Portfolio overview
β”‚   β”‚   β”‚   β”œβ”€β”€ market/             # Market data & trading
β”‚   β”‚   β”‚   β”œβ”€β”€ orders/             # Order management
β”‚   β”‚   β”‚   β”œβ”€β”€ ledger/             # Transaction history
β”‚   β”‚   β”‚   β”œβ”€β”€ layout.tsx          # Root layout
β”‚   β”‚   β”‚   β”œβ”€β”€ page.tsx            # Landing page
β”‚   β”‚   β”‚   └── globals.css         # Global styles
β”‚   β”‚   └── lib/
β”‚   β”‚       └── api.ts              # API client
β”‚   β”œβ”€β”€ public/
β”‚   β”‚   └── brand/                  # Logo and branding
β”‚   β”œβ”€β”€ Dockerfile                  # Frontend container
β”‚   β”œβ”€β”€ package.json                # Node dependencies
β”‚   β”œβ”€β”€ next.config.js              # Next.js config
β”‚   └── tsconfig.json               # TypeScript config
β”œβ”€β”€ docker-compose.yml              # Service orchestration
β”œβ”€β”€ start.sh                        # Start script
β”œβ”€β”€ stop.sh                         # Stop script
β”œβ”€β”€ rebuild.sh                      # Rebuild script
└── README.md                       # This file

πŸ”„ Trading Engine Pipeline

Order Processing Flow

# 1. API receives order
POST /orders {
  "symbol": "BTC",
  "side": "BUY",
  "quantity": 2.5
}

# 2. Publish to Kafka
kafka.publish('orders', order_event)

# 3. Worker consumes event
worker.process_order(order_event)

# 4. Compliance Engine
βœ“ Symbol in whitelist (BTC βœ“)
βœ“ User not sanctioned
βœ“ Within trading window
βœ“ Quantity ≀ 10 BTC (2.5 βœ“)
β†’ STATUS: ACCEPTED

# 5. Risk Engine
βœ“ Balance sufficient ($250k available, needs $212k)
βœ“ Order size ≀ 5 BTC (2.5 βœ“)
βœ“ Notional ≀ $250k ($212k βœ“)
βœ“ Daily limit ≀ $1M ($212k βœ“)
βœ“ Exposure ≀ $500k ($212k + $0 existing βœ“)
βœ“ Rate limit ≀ 10/min (1 βœ“)
βœ“ Slippage ≀ 0.5% (0.025% βœ“)
β†’ STATUS: ACCEPTED

# 6. Execution Engine
Market Price: $85,000.00
Spread (0.02%): +$17.00
Slippage (0.0251%): +$21.34
Execution Price: $85,038.34
Total Cost: $212,595.85
β†’ STATUS: EXECUTED

# 7. Balance Update
Before: $250,000.00
After:  $37,404.15
Reserved: $0.00

# 8. Ledger Entry
{
  "trade_id": "uuid",
  "order_id": "uuid",
  "symbol": "BTC",
  "quantity": 2.5,
  "exec_price": 85038.34,
  "spread_pct": 0.02,
  "slippage_pct": 0.0251,
  "prev_hash": "abc123...",
  "current_hash": "def456..."
}

Rejection Scenarios

# Insufficient Balance
Risk Engine β†’ REJECTED
Reason: "Insufficient balance: available $10,000, required $212,596"

# Quantity Limit Exceeded
Risk Engine β†’ REJECTED
Reason: "Quantity 6.0 exceeds limit 5.0 for BTC"

# Sanctioned User
Compliance Engine β†’ REJECTED
Reason: "User on sanctions list"

# Symbol Not Allowed
Compliance Engine β†’ REJECTED
Reason: "Trading symbol 'DOGE' is not allowed"

# Trading Window Closed
Compliance Engine β†’ REJECTED
Reason: "Trading is disabled during maintenance (00:00-02:00 UTC)"

# Large Order Review
Compliance Engine β†’ PENDING_REVIEW (Risk passed)
Reason: "Large BTC order (7.5 units) requires compliance review (threshold: 3.0)"

πŸ” Security & Compliance

Compliance Checks

Check Description Example Limit
Symbol Whitelist Only approved instruments can be traded 10 symbols (BTC, ETH, AAPL, etc.)
Sanctions Screening Block sanctioned entities OFAC, EU lists
Trading Windows Restrict trading during maintenance 00:00-02:00 UTC blocked
Position Limits Maximum position size per asset BTC: 10, ETH: 100
Review Thresholds Flag large orders for manual review BTC > 3, ETH > 20

Risk Controls

Control Purpose Limit
Balance Check Prevent overdrafts Available balance
Order Size Limit single order risk BTC: 5 per order
Notional Limit Cap order value $250k per order
Daily Limit Prevent excessive trading $1M per day
Exposure Limit Total open position risk $500k total
Rate Limiting Prevent order spam 10 orders/minute
Slippage Tolerance Execution price protection 0.5% max

Authentication

  • JWT Tokens: Secure, stateless authentication
  • Password Hashing: bcrypt with salt
  • Token Expiration: Configurable expiry
  • Protected Routes: All trading endpoints require authentication

πŸ“š API Documentation

Authentication

# Register
POST /auth/register
{
  "name": "John Doe",
  "username": "johndoe",
  "password": "securepassword123"
}

# Login
POST /auth/login
{
  "username": "johndoe",
  "password": "securepassword123"
}
β†’ Returns: { "access_token": "eyJ...", "token_type": "bearer" }

Orders

# Place Order
POST /orders
Authorization: Bearer <token>
{
  "symbol": "BTC",
  "side": "BUY",
  "quantity": 1.5
}

# Get Orders
GET /orders
Authorization: Bearer <token>

# Get Order Details
GET /orders/{order_id}
Authorization: Bearer <token>

Market Data

# Get All Symbols
GET /symbols

# Get Symbol Price
GET /symbols/{symbol}

Balance

# Get Balance
GET /balance
Authorization: Bearer <token>

# Get Transaction History
GET /balance/transactions
Authorization: Bearer <token>

Full API Documentation

Interactive API docs available at: http://localhost:8000/docs


πŸ§ͺ Testing

Manual Testing

# Test order placement
curl -X POST http://localhost:8000/orders \
  -H "Authorization: Bearer <token>" \
  -H "Content-Type: application/json" \
  -d '{"symbol": "BTC", "side": "BUY", "quantity": 1.0}'

# Check order status
curl http://localhost:8000/orders/<order_id> \
  -H "Authorization: Bearer <token>"

# View balance
curl http://localhost:8000/balance \
  -H "Authorization: Bearer <token>"

Testing Scenarios

  1. Normal Order: 1 BTC at market price
  2. Large Order: 8 BTC (triggers compliance review)
  3. Excessive Order: 15 BTC (rejected - exceeds max limit)
  4. Insufficient Funds: Order exceeding available balance
  5. Invalid Symbol: Try to trade "DOGE" (not whitelisted)
  6. Rate Limiting: Place 15 orders in 1 minute

🀝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

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

πŸ“„ License

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


πŸ™ Acknowledgments

  • Inspired by institutional trading systems
  • Built with modern cloud-native practices
  • Designed for educational and commercial use

πŸ“ž Contact

Project Maintainer: Olivier Dubo

For questions, suggestions, or support, please open an issue on GitHub.


AEGIS - Enterprise Trading Infrastructure

Built with ❀️ using FastAPI, Next.js, and Docker

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors