6.5 KiB
6.5 KiB
Unified Trading System Architecture
A high-performance trading system that seamlessly handles backtesting, paper trading, and live trading using a three-tier architecture optimized for different performance requirements.
Architecture Overview
Three-Tier Design
-
Rust Core (Hot Path - Microseconds)
- Order book management
- Order matching engine
- Real-time risk checks
- Position tracking
- Live P&L calculations
-
Bun Orchestrator (Warm Path - Milliseconds)
- System coordination
- Data routing and normalization
- API gateway (REST + WebSocket)
- Exchange connectivity
- Strategy management
-
Python Analytics (Cold Path - Seconds+)
- Portfolio optimization
- Complex risk analytics
- ML model inference
- Performance attribution
- Market regime detection
Trading Modes
Backtest Mode
- Processes historical data at maximum speed
- Realistic fill simulation with market impact
- Comprehensive performance metrics
- Event-driven architecture for accuracy
Paper Trading Mode
- Uses real-time market data
- Simulates fills using actual order book
- Tracks virtual portfolio with realistic constraints
- Identical logic to live trading for validation
Live Trading Mode
- Connects to real brokers/exchanges
- Full risk management and compliance
- Real-time position and P&L tracking
- Audit trail for all activities
Key Features
Unified Strategy Interface
Strategies work identically across all modes:
class MyStrategy extends BaseStrategy {
async onMarketData(data: MarketData) {
// Same code works in backtest, paper, and live
const signal = await this.generateSignal(data);
if (signal.strength > 0.7) {
await this.submitOrder(signal.toOrder());
}
}
}
Mode Transitions
Seamlessly transition between modes:
- Backtest → Paper: Validate strategy performance
- Paper → Live: Deploy with confidence
- Live → Paper: Test modifications safely
Performance Optimizations
Backtest Mode:
- Batch data loading
- Parallel event processing
- Memory-mapped large datasets
- Columnar data storage
Paper/Live Mode:
- Lock-free data structures
- Batched market data updates
- Efficient cross-language communication
- Minimal allocations in hot path
Getting Started
Prerequisites
- Rust (latest stable)
- Bun runtime
- Python 3.10+
- Docker (for dependencies)
Installation
- Build Rust Core:
cd apps/stock/core
cargo build --release
npm run build:napi
- Install Bun Orchestrator:
cd apps/stock/orchestrator
bun install
- Setup Python Analytics:
cd apps/stock/analytics
python -m venv venv
source venv/bin/activate # or venv\Scripts\activate on Windows
pip install -r requirements.txt
Running the System
- Start Analytics Service:
cd apps/stock/analytics
python main.py
- Start Orchestrator:
cd apps/stock/orchestrator
bun run dev
- Connect to UI:
Open WebSocket connection to
ws://localhost:3002
API Examples
Submit Order (REST)
curl -X POST http://localhost:3002/api/orders \
-H "Content-Type: application/json" \
-d '{
"symbol": "AAPL",
"side": "buy",
"quantity": 100,
"orderType": "limit",
"limitPrice": 150.00
}'
Subscribe to Market Data (WebSocket)
const socket = io('ws://localhost:3002');
socket.emit('subscribe', {
symbols: ['AAPL', 'GOOGL'],
dataTypes: ['quote', 'trade']
});
socket.on('marketData', (data) => {
console.log('Market update:', data);
});
Run Backtest
curl -X POST http://localhost:3002/api/backtest/run \
-H "Content-Type: application/json" \
-d '{
"mode": "backtest",
"startDate": "2023-01-01T00:00:00Z",
"endDate": "2023-12-31T23:59:59Z",
"symbols": ["AAPL", "GOOGL", "MSFT"],
"initialCapital": 100000,
"strategies": [{
"id": "mean_reversion_1",
"name": "Mean Reversion Strategy",
"enabled": true,
"allocation": 1.0,
"symbols": ["AAPL", "GOOGL", "MSFT"],
"parameters": {
"lookback": 20,
"entryZScore": 2.0,
"exitZScore": 0.5
}
}]
}'
Configuration
Environment Variables
Orchestrator (.env):
PORT=3002
DATA_INGESTION_URL=http://localhost:3001
ANALYTICS_SERVICE_URL=http://localhost:3003
QUESTDB_HOST=localhost
QUESTDB_PORT=9000
POSTGRES_HOST=localhost
POSTGRES_PORT=5432
Analytics Service (.env):
ANALYTICS_PORT=3003
REDIS_URL=redis://localhost:6379
DATABASE_URL=postgresql://user:pass@localhost:5432/trading
Risk Limits Configuration
{
"maxPositionSize": 100000,
"maxOrderSize": 10000,
"maxDailyLoss": 5000,
"maxGrossExposure": 1000000,
"maxSymbolExposure": 50000
}
Monitoring
Metrics Exposed
- Order latency (submission to fill)
- Market data latency
- Strategy performance metrics
- System resource usage
- Risk limit utilization
Health Endpoints
- Orchestrator:
GET http://localhost:3002/health - Analytics:
GET http://localhost:3003/health
Development
Adding a New Strategy
- Extend
BaseStrategyclass - Implement required methods
- Register with
StrategyManager - Configure parameters
Adding a New Data Source
- Implement
MarketDataSourcetrait in Rust - Add connector in Bun orchestrator
- Configure data routing
Adding Analytics
- Create new endpoint in Python service
- Implement analysis logic
- Add caching if needed
- Update API documentation
Performance Benchmarks
Backtest Performance
- 1M bars/second processing rate
- 100K orders/second execution
- Sub-millisecond strategy evaluation
Live Trading Latency
- Market data to strategy: <100μs
- Order submission: <1ms
- Risk check: <50μs
Resource Usage
- Rust Core: ~200MB RAM
- Bun Orchestrator: ~500MB RAM
- Python Analytics: ~1GB RAM
Troubleshooting
Common Issues
"Trading engine not initialized"
- Ensure mode is properly initialized
- Check Rust build completed successfully
"No market data received"
- Verify data-ingestion service is running
- Check symbol subscriptions
- Confirm network connectivity
"Risk check failed"
- Review risk limits configuration
- Check current positions
- Verify daily P&L hasn't exceeded limits
License
MIT License - See LICENSE file for details