Documentation Index
Fetch the complete documentation index at: https://mathematicalcompany.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
hz.run() is the single entry point for running a strategy. It creates the engine, starts feeds, recovers state, syncs positions, and enters the main loop.
Full Signature
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
name | str | required | Strategy name (shown in logs and dashboard) |
exchange | Exchange or None | None | Single exchange config: Polymarket(...), Kalshi(...), Alpaca(...), IBKR(...), Coinbase(...), Robinhood(...) |
exchanges | list | None | Multi-exchange configs, e.g. [Polymarket(...), Alpaca(...)] |
markets | list[str] | [] | Market slugs or tickers to trade |
feeds | dict[str, Feed] | {} | Named feeds, e.g. {"btc": BinanceWS("btcusdt")} |
pipeline | list[Callable] | required | Pipeline functions. Final fn must return list[Quote] |
risk | Risk or RiskConfig | defaults | Risk configuration |
interval | float | 0.5 | Seconds between strategy cycles |
mode | str | "paper" | "paper" for simulation, "live" for real trading |
dashboard | bool | False | Enable TUI dashboard |
params | dict | {} | Custom params accessible via ctx.params |
db_path | str or None | auto | SQLite path. Default: HORIZON_DB env or ./{name}.db. Set None to disable. |
events | list[Event] | None | Multi-outcome events (each outcome becomes a market). See Multi-Outcome Events. |
netting_pairs | list[tuple] | None | Market pairs whose positions offset for risk |
api_key | str or None | None | Horizon API key. Falls back to HORIZON_API_KEY env var. See Authentication. |
Lifecycle
Validate API key
Validates the Horizon API key (from
api_key param or HORIZON_API_KEY env var). Uses a local cache at ~/.horizon/license.json - only makes a network call if the cache is expired or missing. See Authentication.Build engine
Creates the Engine with exchange backend(s) and risk config. For multi-exchange, the first exchange in the list becomes the primary.
Register events and netting pairs
If
events is provided, each event is registered on the engine and its outcomes are expanded into markets. If netting_pairs is provided, registers each pair on the engine for cross-exchange hedging.Recover state
If persistence is enabled, loads the latest position snapshot and replays fills since that snapshot. Detects orphaned orders from previous runs.
Resolve markets
In live mode, fetches market metadata from exchange APIs. For Polymarket, queries the Gamma API for token IDs and condition IDs. For Kalshi, sets the ticker. For Alpaca/IBKR, resolves symbols directly.
Start feeds
Starts all configured feeds (WebSocket connections, REST pollers) on the engine’s feed manager.
Main loop
Each cycle:
- Poll fills from live exchanges
- Update daily P&L for drawdown tracking
- For each market: build context → run pipeline → process result
- Cancel stale orders → submit new quotes
- Snapshot positions to DB every 50 cycles
- Evict terminal orders every 100 cycles
Persistence Defaults
Thedb_path parameter uses a sentinel value (...) to distinguish between “use default” and “explicitly disabled”:
| Value | Behavior |
|---|---|
... (default) | Use HORIZON_DB env var, or ./{name}.db |
"/path/to/db" | Use the specified path |
None | Disable persistence entirely |
Authentication
Every call tohz.run() requires a valid Horizon API key. There are three ways to provide it:
Option 1: Explicit api_key parameter (clearest)
Option 2: Environment variable (recommended for production)
Option 3: Saved credentials (programmatic setup)
If you usedhorizon.auth.setup() or the CLI horizon setup, your key is saved encrypted at ~/.horizon/credentials.json and loaded automatically.
Key formats
| Prefix | Source | Description |
|---|---|---|
hz_live_ | Website (api.mathematicalcompany.com) | Manual signup key |
hz_sdk_ | horizon.auth.setup() or CLI | Programmatically generated SDK key |
hz_sdk_ keys are generated via the SDK’s auth module and are useful for automated/agentic setups where no browser is available.
Resolution order
| Priority | Source |
|---|---|
| 1 | api_key param in hz.run() |
| 2 | HORIZON_API_KEY environment variable |
| 3 | ~/.horizon/credentials.json (saved by setup) |