Poseidon - Risk Engine Service
Overview
Poseidon is a high-performance risk engine service for the Kyan exchange that calculates comprehensive risk metrics for margin portfolios in cryptocurrency trading. It provides fast, vectorized calculations for maintenance margin requirements, portfolio greeks, and risk assessments across complex options and perpetual futures positions.
💡 Beginner Tip: Think of Poseidon as the "safety inspector" of the exchange. Before trades are executed, it quickly analyzes positions to calculate how much collateral (margin) traders need to keep their positions secure, especially during market volatility. This helps prevent liquidations and ensures the exchange remains financially stable.
Features
- Fast, vectorized calculation of risk metrics
- Support for both internal (margin accounts) and external (user portfolios) risk calculations
- Portfolio-level greeks calculation (delta, gamma, vega, theta, rho)
- Maintenance margin calculations including:
- Matrix risk (worst-case loss scenarios)
- Delta risk (systemic risk adjustments)
- Roll risk (expiration-related risk)
- Vectorized risk calculations - Fast, matrix-based computation of risk metrics
- Portfolio-level greeks - Delta, gamma, vega, theta, and rho calculations
- Advanced margin models - Matrix risk, delta risk, and roll risk components
- Dual-mode operation - Internal (margin accounts) and external (user portfolios) calculations
- Stress testing - Multiple market shock scenarios for robust risk assessment
- Real-time calculations - Optimized for low-latency processing
Margin Model Validation
Deribit Benchmark Comparison
The evaluate.py
script in the margin_comparison
directory allows for systematic comparison between Poseidon's internal risk model and Deribit's margin calculation API. This tool helps validate our risk model accuracy against industry standards.
Features
- Side-by-side comparison of margin requirements
- Systematic testing with random and structured portfolios
- Statistical analysis of margin differences
- Visual representation via scatter plots and histograms
- The exploratory data analysis notebook (eda.ipynb) can be used to further compare the two risk models, and allows cherry picking of portfolios with a large discrepancy.
Usage
# Set required environment variables
export DERIBIT_API_KEY=your_api_key
export DERIBIT_API_SECRET=your_api_secret
# Run from the margin_comparison directory
cd margin_comparison
python evaluate.py
Output
The script generates:
- Detailed comparison data saved as JSON (
margin_comparison_results_YYYYMMDD_HHMMSS.json
) - Visual representations of margin differences (
margin_comparison.pdf
) - Distribution analysis of calculation differences (
margin_comparison_histogram.pdf
)
Implementation Details
The benchmark utility uses Deribit's API to:
- Fetch real-time mark prices and implied volatilities
- Submit test portfolios for margin calculation
- Compare results with Poseidon's internal calculations
- Generate statistical summaries of differences
This validation process ensures our risk model remains properly calibrated against industry standards while maintaining the performance benefits of our vectorized implementation.
Technical Specifications
Core Technology Stack
- Runtime: Python 3.9+
- Primary Frameworks: FastAPI, NumPy, Pandas
- Build System: Poetry with NX
- Testing: Pytest
- Formatting: Ruff, Black
- Database: PostgreSQL (via SQLAlchemy)
- Version: 0.1.0
Dependencies
-
Core:
-
fastapi - API framework for endpoints
- numpy/pandas - Vectorized mathematical operations
- pydantic - Data validation and modeling
- uvicorn - ASGI server
- faststream - Stream processing
-
sqlalchemy/sqlmodel - Database ORM
-
Testing:
-
pytest - Testing framework
- pytest-asyncio - Async test support
-
pytest-cov - Test coverage
-
Internal Libraries:
- commons - Shared Python utilities
- chainpyon - Blockchain interaction utilities
Getting Started
Prerequisites
- Python 3.9-3.10
- Poetry package manager
- PostgreSQL database
- Redis instance
- Numpy and other scientific libraries
Environment Setup
Required environment variables:
DATABASE_URL=postgresql://postgres:postgres@localhost:5432/exchange # Database URL
REDIS_URL=redis://localhost:6379/0 # Redis URL
LOG_LEVEL=info # Logging level
MATRIX_RISK_ENABLED=true # Enable matrix risk calculations
Installation
Development
# Run linting
nx run poseidon:lint
# Run type checking
nx run poseidon:typecheck
# Start development server
nx run poseidon:serve
Service Architecture
Component Overview
┌───────────────┐ ┌───────────────┐ ┌───────────────┐
│ │ │ │ │ │
│ FastAPI │◄───────►│ Portfolio │◄───────►│ Risk Model │
│ Service │ │ Processor │ │ Calculator │
│ │ │ │ │ │
└───────────────┘ └───────────────┘ └───────────────┘
│ │ │
│ │ │
▼ ▼ ▼
┌───────────────┐ ┌───────────────┐ ┌───────────────┐
│ │ │ │ │ │
│ Data │ │ Greeks │ │ Market Shock │
│ Models │ │ Calculator │ │ Generator │
│ │ │ │ │ │
└───────────────┘ └───────────────┘ └───────────────┘
Core Components
-
FastAPI Service (poseidon/main.py)
-
API endpoints for risk calculations
- Request validation and handling
-
Service health monitoring
-
Portfolio Processor (poseidon/processor.py)
-
Processes input portfolios
- Computes equity and unrealized PnL
-
Vectorized position data preparation
-
Risk Model Calculator (poseidon/margin_calculator.py)
-
Maintenance margin calculations
- Matrix risk algorithms
-
Roll risk and delta risk computations
-
Greeks Calculator
-
Portfolio-level greeks computation
- Options sensitivity metrics
-
Exposure analytics
-
Data Models (poseidon/models.py)
- Position and portfolio schemas
- API request/response models
- Risk metric definitions
Project Structure
poseidon/
├── poseidon/ # Source code
│ ├── margin_calculator.py # Risk model implementation
│ ├── models.py # Data structures
│ ├── processor.py # Portfolio processing
│ ├── config.py # Configuration settings
│ └── main.py # FastAPI application
├── tests/ # Test suite
│ ├── test_margin_calculator.py # Risk model tests
│ ├── test_processor.py # Processor tests
│ └── test_main.py # API endpoint tests
├── pyproject.toml # Poetry configuration
└── pyrightconfig.json # Type checking configuration
Risk Calculation Methodology
Maintenance Margin Components
-
Matrix Risk
-
Stress-tests portfolios across price and volatility scenarios
- Identifies worst-case loss scenarios
-
Calculates required collateral to cover these losses
-
Delta Risk
-
Addresses systemic market exposure
- Applies beta adjustments for large delta exposure
-
Scales with portfolio size and market concentration
-
Roll Risk
- Adds protection for near-expiry options
- Increases as options approach expiration
- Prevents liquidity issues during settlement
Calculation Process
- Positions are converted to a standardized matrix format
- Market scenarios are generated (price shocks, volatility changes)
- Portfolio value is calculated under each scenario
- Worst-case losses are identified
- Additional risk components are added
- Final maintenance margin requirement is computed
API Reference
Endpoint | Method | Description | Request Body | Response |
---|---|---|---|---|
/private/calculate_margin_risk |
POST | Calculate risk metrics for margin accounts | Array of margin portfolios | Risk metrics with success/failure status |
/public/calculate_portfolio_risk |
POST | Calculate risk metrics for external portfolios | Array of user portfolios | Risk metrics without equity |
/private/health |
GET | Service health check | N/A | Service status |
Request Example (Margin Portfolio)
{
"pair": "BTC_USDC",
"positions": [
{
"instrument": "BTC_USDC-28JUN24-80000-C",
"size": 1.5,
"average_price": 0.015
}
],
"marginAccountId": 123,
"marginAccount": {
"total_deposits": 100.0,
"total_withdrawals": 20.0,
"accrued_funding": 0.5,
"realised_pnl": 15.0
}
}
Response Example
{
"status": "success",
"data": {
"equity": 95.5,
"unrealizedPnl": 2.0,
"maintenanceMargin": 8.5,
"initialMargin": 17.0,
"greeks": {
"delta": 0.35,
"gamma": 0.0002,
"vega": 0.15,
"theta": -0.08,
"rho": 0.03
}
}
}
Integration Points
External Dependencies
- Exchange Data Store: Market data and instrument information
- Sequencer Service: Position and account data
- SQL Database: Persistent storage of risk parameters
Internal Dependencies
- Orderbook Service: Pre-trade risk checking
- UI Components: Portfolio risk display
Performance Considerations
- Vectorized calculations using NumPy for maximum efficiency
- Optimized position matrix handling
- Caching of intermediate results
- Parallel processing for multiple portfolios
- Efficient market data processing
Monitoring and Health
- FastAPI built-in health check endpoint:
/private/health
- Performance metrics for calculation latency
- Error monitoring and alerting
Troubleshooting
Common Issues
Issue | Possible Cause | Solution |
---|---|---|
High calculation latency | Large portfolio size | 1. Check portfolio size 2. Verify NumPy optimization 3. Consider batching calculations |
Incorrect risk values | Market data issues | 1. Verify market data sources 2. Check instrument parameters 3. Validate volatility inputs |
Memory errors | Portfolio matrix size | 1. Monitor memory usage 2. Implement garbage collection 3. Consider splitting large portfolios |
Debugging Tips
- Enable debug logging by setting
LOG_LEVEL=debug
in .env - Use pandas profiling to identify bottlenecks:
- Monitor NumPy array shapes and memory usage:
Future Optimizations
- GPU acceleration for matrix operations
- Cached scenario generation
- Distributed calculation for large portfolios
- Advanced calibration for extreme market conditions
Contributing
For guidelines on contributing to this project, please see the CLAUDE.md file in the repository root.
License
Proprietary - All rights reserved
This documentation is generated from the service README. For the most up-to-date information, refer to the original README