Horizon Design Principles
Rust Core
All critical paths run in Rust: risk checks, order management, position tracking, exchange communication, and fill processing. Python never touches the hot path.
No IPC
Python calls Rust directly via PyO3. Zero serialization overhead. No sockets, no message queues, no JSON encoding between layers.
f64 Everywhere
All numeric values at the Python boundary use
f64 for simplicity. No Decimal conversion needed.Enum Dispatch
Exchange backends use enum dispatch (
ExchangeBackend) instead of trait objects. Avoids dynamic dispatch overhead and unsafe downcasts.Component Overview
Horizon Engine
TheEngine is the central orchestrator. It owns:
- RiskManager: 8-point risk pipeline that every order passes through
- OrderManager: tracks order lifecycle with DashMap for concurrent access
- PositionTracker: maintains positions with atomic P&L calculations
- ContingentManager: manages stop-loss, take-profit, and bracket orders with OCO linking
- Exchange Map: multiple exchange backends keyed by name (e.g.,
"paper","polymarket","kalshi","alpaca","ibkr","coinbase","robinhood") - FeedManager: tokio runtime with spawned async tasks for WebSocket/REST feeds
- Database: optional SQLite persistence layer for crash recovery
Exchange Model
Exchanges are registered by name in aHashMap<String, ExchangeBackend>. The first exchange becomes the primary (default for routing). Additional exchanges can be added via add_exchange().
All exchanges implement the Exchange trait:
submit_order(): submit an order to the exchangecancel_order(): cancel a single ordercancel_all(): cancel all orderscancel_for_market(): cancel orders for a specific marketdrain_fills(): pull pending fillsfetch_positions(): sync positions from the exchangeamend_order(): amend an order’s price and/or size (cancel+resubmit on live, in-place on paper)
Side::Yes / Side::No. Equity and crypto exchanges (Alpaca, IBKR, Coinbase, Robinhood) use Side::Long. The Side enum carries through the entire pipeline — risk checks, position tracking, and fill processing all handle both conventions.
Feed System
TheFeedManager owns a tokio runtime. Each feed runs as a spawned async task:
| Feed | Transport | Update Model |
|---|---|---|
| BinanceWS | WebSocket | Real-time trade stream |
| PolymarketBook | WebSocket | Orderbook snapshots |
| KalshiBook | HTTP polling | Orderbook polling |
| Alpaca | WebSocket | Equity/crypto quotes and trades |
| IBKR | HTTP polling | Stocks, options, ForecastEx market data |
| RESTFeed | HTTP polling | Generic REST endpoint |
DashMap<String, FeedSnapshot> and read by the Python layer each cycle via engine.feed_snapshot(name).
Contingent Orders
TheContingentManager manages synthetic stop-loss and take-profit orders. These are not sent to the exchange. Instead, the engine monitors triggers each tick cycle and converts them to real OrderRequests when conditions are met.
- Stop-loss: triggers when price crosses below (sell) or above (buy) the trigger level
- Take-profit: triggers on price target or unrealized PnL threshold
- OCO linking: two contingent orders can be linked so that when one triggers, the other is automatically canceled
- Bracket orders: entry order + stop-loss + take-profit, all submitted and linked in one call
Persistence Layer
SQLite with WAL mode provides crash recovery and an audit trail:- fills: append-only journal with
INSERT OR IGNOREdedup - order_events: order lifecycle log (one row per status change)
- position_snapshots: periodic snapshots for fast recovery
- risk_events: kill switch activations, violations
- strategy_runs: run start/end timestamps
Horizon Data Flow
Monitoring Stack
Horizon provides three monitoring layers:| Layer | Component | Description |
|---|---|---|
| Metrics | MetricsServer | Prometheus /metrics endpoint with counters, gauges, and histograms |
| Alerts | AlertManager | Multi-channel alerts (Discord, Telegram, webhooks, log) with cooldown throttling |
| Calibration | CalibrationTracker | Tracks prediction accuracy with Brier scores over time (prediction markets) |
| Dashboard | TUI (Textual) | Real-time terminal dashboard with P&L, positions, orders, risk, fills, feeds |