Skip to main content
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

exchange and exchanges are mutually exclusive. Use exchange for single-exchange mode or exchanges for multi-exchange mode.

Lifecycle

1

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.
2

Build engine

Creates the Engine with exchange backend(s) and risk config. For multi-exchange, the first exchange in the list becomes the primary.
3

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.
4

Recover state

If persistence is enabled, loads the latest position snapshot and replays fills since that snapshot. Detects orphaned orders from previous runs.
5

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.
6

Start feeds

Starts all configured feeds (WebSocket connections, REST pollers) on the engine’s feed manager.
7

Sync positions

In live mode, syncs positions from each exchange to reconcile local state.
8

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
9

Shutdown

On Ctrl+C or SIGTERM: snapshot positions, end run record, cancel all orders (with 5s timeout), stop feeds.

Persistence Defaults

The db_path parameter uses a sentinel value (...) to distinguish between “use default” and “explicitly disabled”:

Authentication

Every call to hz.run() requires a valid Horizon API key. There are three ways to provide it:

Option 1: Explicit api_key parameter (clearest)

This is the most explicit approach and works in notebooks, scripts, and CI pipelines.

Option 3: Saved credentials (programmatic setup)

If you used horizon.auth.setup() or the CLI horizon setup, your key is saved encrypted at ~/.horizon/credentials.json and loaded automatically.

Key formats

Both formats work identically. The 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

See Authentication for the full guide.

Examples

Paper trading (simplest)

Live on Polymarket

Equity trading on Alpaca

Multi-asset (prediction market + stocks)

Multi-outcome events

See Multi-Outcome Events for the full guide.

Multi-exchange with netting