Imperative API
hz.connect(): direct order placement without the pipeline
When you just want to place an order (a manual trade, a bootstrap script, a notebook experiment), use hz.connect(). It wraps any venue with an ergonomic client that supports buy, sell, short, flatten, and context-manager cleanup.
Quick example
python
import horizon as hz
with hz.connect("paper", initial_cash_usd=100_000) as ex:
ex.buy("AAPL", qty=10, limit=180.0)
ex.buy("MSFT", qty=5, limit=400.0)
print(ex.balance())
print(ex.open_orders())
Methods
Context manager vs explicit
python
# Context manager. auto close
with hz.connect("paper", initial_cash_usd=100_000) as ex:
ex.buy("AAPL", 10, limit=180)
# Explicit. you manage lifecycle
ex = hz.connect("paper", initial_cash_usd=100_000)
ex.buy("AAPL", 10, limit=180)
ex.close() # don't forget
Context manager is preferred.
Venues supported
python
hz.connect("paper", initial_cash_usd=100_000) # works today
hz.connect("alpaca", paper=True, api_key_env="ALPACA_KEY", ...) # planned
hz.connect("ibkr", account_env="IBKR_ACCOUNT", ...) # planned
hz.connect("polymarket", private_key_env="POLY_KEY", ...) # planned
hz.connect("kalshi", email_env="KALSHI_EMAIL", ...) # planned
hz.connect("hyperliquid", private_key_env="HL_KEY", ...) # planned
Only "paper" fully works today. The others return scaffolds: the Rust client binding is a future release.
Escape hatch from strategies
Inside a Strategy.evaluate() with ctx, you can reach the imperative API:
python
class EmergencyFlatten(Strategy):
def evaluate(self, f, universe, ctx):
if self._emergency():
ctx.exchange("paper").flatten_all()
return []
return [...]
Source
horizon/imperative.py. ~220 lines. Thin wrapper over the Venue protocol.