Execution & Broker Integration
Ports & Adapters pattern — ExecutionService operates against abstract broker interface ports. Concrete adapters implement each broker's specific protocol. No order is ever silently dropped: it routes to a live broker or to the paper adapter.
Execution Architecture
Order Routing Rules
| Asset Class | Broker | Adapter |
|---|---|---|
| EQUITY | Interactive Brokers (IBKR) | IBKRAdapter via ib-insync |
| OPTION | Interactive Brokers (IBKR) | IBKRAdapter via ib-insync |
| FUTURE | Interactive Brokers (IBKR) | IBKRAdapter via ib-insync |
| FX (Forex) | IC Markets via MT4 | MT4Adapter REST bridge |
| CRYPTO | Binance | BinanceAdapter via CCXT |
| FALLBACK (any) | Paper Trading | PaperAdapter (mock, no real fills) |
Execution Routing Diagram
Signal → BrokerRouter → adapter → exchange
8-Step Order Lifecycle
create_order()Order object created — state: PENDING
persist(order)Written to TimescaleDB orders table
connect_gateway()Broker adapter connection verified / reconnected. Failure → rerouted to PaperAdapter.
place_order(order)Sent to broker API. Failure → logged as REJECTED, failure counter incremented (circuit breaker).
await_fill()Poll or callback for fill confirmation. IBKR: event-driven. Binance/MT4: polling-based.
persist(FILLED)Order updated with fill price and timestamp
update_positions()Portfolio positions updated in real-time
publish_fill_event()Redis pub/sub: fill event for downstream consumers
Interactive Brokers (IBKR)
Connection
Fill Handling
Event-driven via ib-insync asyncio events — no polling required:
Supported Order Types
Asset Classes (IBKR)
IC Markets MT4 (Forex)
REST bridge running as a Docker container (superintel-mt4-bridge) at 127.0.0.1:8005:8000. Translates REST API calls into MT4 Expert Advisor commands connected to IC Markets MT4 terminal. Fill confirmation via polling GET /order/{id} with retry-backoff.
| Method | Endpoint | Purpose |
|---|---|---|
POST | /order/place | Place a Forex market or limit order |
POST | /order/modify | Modify stop-loss or take-profit on open order |
POST | /order/close | Close an open position |
GET | /order/{id} | Poll order status for fill confirmation |
GET | /account | Account balance and margin info |
Binance (Crypto)
Singleton CCXT ccxt.async_support.binance with enableRateLimit=True. API key + secret from environment variables. Fill confirmation via fetch_order() polling every 3 seconds.
Execution Operations
Data Operations (dual-use)
Paper Trading Adapter (Fallback)
Null-implementation of the broker interface. Simulates fills with configurable slippage model (±0.1% of mid-price). Tracks paper positions in TimescaleDB paper_positions table. Activates automatically when:
- No live broker adapter available for the asset class
- Environment variable
FORCE_PAPER_TRADING=true - Live adapter fails
health_check()
NAV Arbitrage — Dual-Leg Execution
NavArbitrageDetector targets trust/spot premium arbitrage (GBTC/BTC style). Entry on Z-score extremes, exit on mean reversion. Two-leg orders submitted via asyncio.gather.
| Signal | Condition | Trade |
|---|---|---|
LONG_TRUST_SHORT_SPOT | Z-score < −2.0 AND Spread < −2% | Trust trades at >2% discount to NAV — buy trust, short spot |
SHORT_TRUST_LONG_SPOT | Z-score > +2.0 AND Spread > +2% | Trust trades at >2% premium to NAV — short trust, long spot |
Exchange Mapper — Ticker Routing
| Ticker | Asset Class | Routes To |
|---|---|---|
BTC-USDT | CRYPTO | Binance |
GBTC | EQUITY | IBKR (ETF, not crypto route) |
SPY | EQUITY | IBKR |
EURUSD | FX | IC Markets MT4 |