Alpaca trading bot: commission-free US stocks API and code
Alpaca is the API-first US brokerage that made stock trading bots accessible to retail developers. It offers commission-free trading of US equities and crypto, a clean REST and websocket API, a Python SDK, and — crucially — a free paper-trading endpoint that mirrors the live API exactly. That last feature is why so many first stock bots are built on Alpaca: you can forward-test against real market data with fake money and identical code. This guide covers the Alpaca API model, the Python SDK, the paper-to-live switch, and the US-market realities (hours, PDT, data) that shape an equities bot.
Why Alpaca suits a stock bot
Unlike most US brokers, Alpaca was built API-first: no clunky web terminal to automate around, just clean endpoints for orders, positions and market data. Combined with commission-free trades and a true paper endpoint, it removes most of the friction that makes automating a traditional brokerage painful. It is the equities counterpart to a crypto ccxt bot.
Connecting with the Python SDK
python · alpaca_bot.pyfrom alpaca.trading.client import TradingClient
from alpaca.trading.requests import MarketOrderRequest
from alpaca.trading.enums import OrderSide, TimeInForce
client = TradingClient(KEY, SECRET, paper=True) # paper endpoint
order = MarketOrderRequest(
symbol='AAPL', qty=1,
side=OrderSide.BUY, time_in_force=TimeInForce.DAY)
client.submit_order(order)
Flip paper=True to paper=False and swap the keys to go live — the rest of the code is identical. Use a key scoped to trading only, mirroring API key security practice.
Paper to live — the safe path
Alpaca’s paper endpoint is the cleanest forward-testing setup in retail: same API, same data, fake money. Run the bot on paper for weeks, confirm the fills and P&L track your backtest, then flip to live with tiny size.
Market hours and the PDT rule
US equities trade roughly 9:30–16:00 ET, with limited pre/post-market sessions — unlike 24/7 crypto, your bot must respect the calendar. And the Pattern Day Trader rule restricts accounts under $25,000 to three day-trades per five days. Both constraints are central to the crypto vs stock bots comparison.
Equities-specific risks
Stocks gap overnight on earnings and news, so a stop set at the close can fill far worse at the next open — a risk a 24/7 crypto bot rarely faces. Stocks can also halt, freezing your bot mid-position. And real-time consolidated data may carry exchange fees. Budget for gaps, halts and data when designing an Alpaca bot.
Getting started
Sign up, generate paper keys, build the bot against the paper endpoint, and run it through several full sessions including an earnings event. Backtest first on the backtester, size from a stop with the calculator, then go live small.
Frequently asked questions
Why is Alpaca popular for trading bots?
Alpaca was built API-first with commission-free US equities and crypto, a clean Python SDK, and a free paper-trading endpoint that mirrors the live API exactly. That combination removes most of the friction of automating a traditional brokerage and lets you forward-test with identical code and real market data.
How do I switch an Alpaca bot from paper to live?
You change the paper flag from True to False and swap in your live API keys — the rest of the code is identical. Because the paper endpoint mirrors the live one, what you test on paper is what you trade live, which makes the transition unusually safe.
Does the PDT rule affect an Alpaca bot?
Yes. The Pattern Day Trader rule restricts margin accounts under $25,000 to three day-trades per rolling five business days. A high-frequency intraday bot can hit that limit quickly, so accounts below the threshold must either trade less often or hold positions overnight.
What risks are unique to an Alpaca stock bot?
US stocks trade only during market hours and gap overnight on earnings and news, so a stop set at the close can fill far worse at the next open. Stocks can also halt mid-session, freezing a position, and real-time consolidated data may carry exchange fees — all risks a 24/7 crypto bot rarely faces.