Skip to main content
Alpaca is used for (1) trading (place/cancel orders, portfolio, positions, market data in the Trading Dashboard) and (2) historical OHLCV for stock prediction and backtesting when ALPACA_DATA_ENABLED=true. The same API key and secret are used for both; the base URL selects paper vs live.

1. Overview

If Alpaca keys are not set, trading uses MockTradingAPIService. If ALPACA_DATA_ENABLED is false or Alpaca returns no data, market data falls back to yahooquery (see Stock Prediction Setup).

1.1 Broker API (Multiuser Brokerage)

For multiuser brokerage (one Alpaca customer account per user), use the Alpaca Broker API: Set ALPACA_BROKER_* env vars. See Account Opening and Broker API.

2. Configuration

Broker API (multiuser): ALPACA_BROKER_API_KEY, ALPACA_BROKER_API_SECRET, ALPACA_BROKER_BASE_URL (sandbox: https://broker-api.sandbox.alpaca.markets), ALPACA_BROKER_PAPER.

3. Getting Alpaca Credentials

  1. Sign up at Alpaca.
  2. Paper: Paper Dashboard → API Keys.
  3. Live: Live Dashboard → API Keys. Use with care.
Use paper for development. Set ALPACA_BASE_URL=https://paper-api.alpaca.markets for paper and https://api.alpaca.markets for live.

4. Trading (Place Order, Portfolio, Market Data Tab)

  • Place order: POST /api/trades/orders — uses AlpacaTradingAPIService when keys are set.
  • Portfolio: GET /api/trades/portfolio — positions and account info from Alpaca.
  • Market data: GET /api/trades/market-data, GET /api/trades/market-data/{symbol} — bid/ask etc. from Alpaca.
Without Alpaca keys, MockTradingAPIService is used: orders are simulated, no real broker calls.

5. Historical Data (Stock Prediction and Backtesting)

When ALPACA_DATA_ENABLED=true and keys are set, MarketDataService.get_historical_data uses alpaca.data.StockHistoricalDataClient for 1D, 1H, 15Min. If Alpaca returns nothing, it falls back to yahooquery.
  • Data client: Separate from trading; uses the same ALPACA_API_KEY and ALPACA_API_SECRET.
  • Timeframes: 1D, 1H, 15Min.
  • Used by: StockPredictionService (daily/hourly/15min) and run_backtest_from_data_source.
See Stock Prediction Setup for backtest and prediction config.

6. Dependencies

alpaca-py is required for both trading and historical data:
The modal image used for Chronos already includes alpaca-py; the FastAPI app needs it when ALPACA_DATA_ENABLED=true or when Alpaca is used for trading.

7. Example .env (Paper + Data for Backtest)


8. Troubleshooting