Alpaca
US equities, options, and crypto via Alpaca. Connect with one line.
Alpaca is a commission-free US broker with a clean REST + WebSocket API, a free paper sandbox, and equity / option / crypto coverage. Usable from the core library via hz.connect("alpaca", ...).
Connect
python
import horizon as hz
ex = hz.connect("alpaca",
paper=True, # False for live
api_key="APCA-...",
api_secret="...",
)
ex.buy("AAPL", qty=10, limit=180.0)
print(ex.positions())
ex.close()
Kwargs
| Kwarg | Type | Default | Notes |
|---|---|---|---|
paper | bool | True | Paper sandbox vs. live. Fail-safe default. |
api_key | str \| None | None | Falls back to secrets / env (ALPACA_API_KEY). |
api_secret | str \| None | None | Falls back to secrets / env (ALPACA_SECRET_KEY). |
secrets | Secrets \| None | None | Credential resolver (see Secrets). |
http_client | HttpClient \| None | Rate-limited default | Inject for tests or custom retry. |
budget_usd | float | 0.0 | Optional hard cap on notional the venue will accept. |
Credentials are resolved in order: explicit kwarg → secrets.get("alpaca.api_key") → environment variable. Use whichever fits your deployment.
Install
bash
pip install -e ".[equity]" # alpaca-py + ib_async + pandas-ta
# add validate for stats inference (scipy + sklearn)
pip install -e ".[equity,validate]"
# add options for the options chain / IV path
pip install -e ".[equity,options,validate]"
Bracket / OCO / OTO order
python
from horizon.types import OrderAction
action = OrderAction.place(
market_id="AAPL", side="buy", quantity=10, price=180.0,
order_class="bracket",
take_profit_price=190.0,
stop_loss_price=175.0,
)
ex.venue.submit(action)
The venue rejects a bracket / OCO with a missing leg before anything hits Alpaca. Details: Alpaca pro docs.
Options chain
python
chain = ex.venue.options_chain(
"AAPL", expiry="2026-06-19", option_type="call",
strike_gte=175.0, strike_lte=200.0,
)
for row in chain:
print(row["symbol"], row.get("impliedVolatility"))
Pair with horizon.stats.iv_rank / iv_hv_spread for an end-to-end
research workflow.
Status
- REST submit / cancel / amend: shipped.
- Bracket / OCO / OTO as one ticket: shipped.
- Options chain snapshot: shipped (
Alpaca.options_chain). - Polling
drain_fills(): shipped. - WebSocket order stream + quote feed: see
horizon.data.live.alpaca_ws. - Rate-limited HTTP, exponential backoff, audit chain integration: shipped.
- Multi-leg options combos / spreads: follow-up.
Examples
examples/alpaca_research.py— historical bars →horizon.statsinference → backtest.examples/alpaca_paper_trading.py— full live loop against the paper sandbox.
Full integration details, wire formats, error mapping: Alpaca (professionals).