
This blog was last updated on January 23, 2026.
This tutorial will be a step by step demonstration of how to get market data using the Alpaca's Trading API. We will move forward assuming that you’ve registered on Alpaca's Trading API account.
To get the most out of this guide, explore the following resources first:
Alpaca provides market data from various sources. Check the Market Data API doc to learn more about your data options.
Step 1: Setup
1-1. Install / Update Alpaca-py via uv package manager
!uv pip install alpaca-py --upgrade1.2 Import Necessary SDKs and Dependencies
from datetime import date, datetime, timedelta
from zoneinfo import ZoneInfo
from alpaca.data.historical.stock import StockHistoricalDataClient
from alpaca.data.requests import StockBarsRequest, StockTradesRequest, StockQuotesRequest
from alpaca.data.timeframe import TimeFrame, TimeFrameUnit
Step 2: Setting Up Stock Historical Data Client
# Define the stock historical data client
stock_historical_data_client = StockHistoricalDataClient(ALPACA_API_KEY, ALPACA_SECRET_KEY)
Similarly let’s create a function to get market data for Apple(AAPL). Within this function we print out how much AAPL has moved in the specific timeframe.
See the API documentation for “Working with /orders” for code samples.
Getting Historical Bars Data by Symbol
# Specify the symbol for market data
SYMBOL = 'AAPL'
# Define now based on the New York time zone
now = datetime.now(ZoneInfo("America/New_York"))
# Get historical bars by symbol
# ref. https://docs.alpaca.markets/reference/stockbars-1
req = StockBarsRequest(
symbol_or_symbols = [SYMBOL],
timeframe=TimeFrame(amount = 1, unit = TimeFrameUnit.Hour), # specify timeframe
start = now - timedelta(days = 5), # specify start datetime, default=the beginning of the current day.
# end=None, # specify end datetime, default=now
limit = 2, # specify limit
)
stock_historical_data_client.get_stock_bars(req).dfThe output will look like the image below.

Getting Historical Trades Data by Symbol
# Specify the symbol for market data
SYMBOL = 'AAPL'
# Define now based on the New York time zone
now = datetime.now(ZoneInfo("America/New_York"))
# Get historical trades by symbol
# ref. https://docs.alpaca.markets/reference/stocktrades-1
req = StockTradesRequest(
symbol_or_symbols = [SYMBOL],
# specify start datetime, default=the beginning of the current day.
start = now - timedelta(days = 5),
# specify end datetime, default=now
# end=None,
# specify limit
limit = 2,
)
stock_historical_data_client.get_stock_trades(req).dfThe output will look like the image below.

Getting Historical Quotes Data by Symbol
# Specify the symbol for market data
SYMBOL = 'AAPL'
# Define now based on the New York time zone
now = datetime.now(ZoneInfo("America/New_York"))
# Get historical quotes by symbol
# ref. https://docs.alpaca.markets/reference/stockquotes-1
req = StockQuotesRequest(
symbol_or_symbols = [SYMBOL],
start = now - timedelta(days = 5), # specify start datetime, default=the beginning of the current day.
# specify end datetime, default=now
# end=None,
# specify limit # specify end datetime, default=now
limit = 2, # specify limit
)
stock_historical_data_client.get_stock_quotes(req).dfThe output will look like the image below.

Conclusion
Alpaca’s Trading API provides the market data across stocks, options, and crypto and allows you to retrieve historical insights including quotes, snapshots, Greeks, option chains, and many more.
Keep in mind that no single market data provides a complete picture. It's essential to understand the limitations of any single data and validate your findings with additional technical indicators and comprehensive analysis. However, combining theoretical insights with practical application can help strengthen your overall trading approach.
For more on market data and trading with Alpaca's Trading API, see the additional resources below:
- How to Get Alpaca Trading API Key and Start Connecting
- How to Start Paper Trading with Alpaca Trading API
- How To Trade Options with Alpaca's Dashboard and Trading API
If you’d like to learn more about Alpaca’s Trading API, explore the resources below:
Frequently Asked Questions
Which Market Data API is free?
Alpaca's Trading API provides free market data for stocks, options, and crypto. You can access historical and current data including quotes, snapshots, option chains, and Greeks. Learn more at https://alpaca.markets/data and https://docs.alpaca.markets/docs/about-market-data-api?ref=alpaca.markets
How do I get Alpaca's paper Trading API key?
Create an account or sign in at https://app.alpaca.markets/account/login. Select the Paper Trading account, then navigate to the dashboard API Keys section to generate and copy your API key and secret key.
Is Alpaca's Trading API suitable for algorithmic traders and quants?
Yes. Alpaca's Trading API is suitable for algorithmic traders and quantitative researchers. It offers free market data for stocks, options, and crypto, including historical quotes, snapshots, option chains, and Greeks. You can upgrade to Algo Trader Plus to access real time SIP stock data and OPRA options data. More details are available at https://alpaca.markets/data and https://docs.alpaca.markets/docs/about-market-data-api?ref=alpaca.markets
Past hypothetical backtest results do not guarantee future returns, and actual results may vary from the analysis.
Please note that this article is for educational and informational purposes only. All screenshots are for illustrative purposes only. Alpaca does not recommend any specific securities or investment strategies.
Commission free trading is available to Alpaca's retail customers. Commission free trading means that there are no commission charges for Alpaca self-directed individual cash brokerage accounts that trade U.S.-listed securities and options through an API. However, certain arrangements with authorized business partners or the use of the Elite Smart Router as part of the Alpaca Elite offering may preclude commission free trades by Alpaca Securities. Please refer to the Brokerage Fee Schedule for more information. Relevant regulatory fees may apply. Alpaca reserves the right to charge additional fees if it is determined that order flow is non-retail in nature.
Options trading is not suitable for all investors due to its inherent high risk, which can potentially result in significant losses. Please read Characteristics and Risks of Standardized Options before investing in options.
All investments involve risk, and the past performance of a security, or financial product does not guarantee future results or returns. There is no guarantee that any investment strategy will achieve its objectives. Please note that diversification does not ensure a profit, or protect against loss. There is always the potential of losing money when you invest in securities, or other financial products. Investors should consider their investment objectives and risks carefully before investing.
Securities brokerage services are provided by Alpaca Securities LLC ("Alpaca Securities"), member FINRA/SIPC, a wholly-owned subsidiary of AlpacaDB, Inc. Technology and services are offered by AlpacaDB, Inc.
Cryptocurrency is highly speculative in nature, involves a high degree of risks, such as volatile market price swings, market manipulation, flash crashes, and cybersecurity risks. Cryptocurrency regulations are continuously evolving, and it is your responsibility to understand and abide by them. Cryptocurrency trading can lead to large, immediate and permanent loss of financial value. You should have appropriate knowledge and experience before engaging in cryptocurrency trading. For additional information, please click here.
Cryptocurrency services are made available by Alpaca Crypto LLC ("Alpaca Crypto"), a FinCEN registered money services business (NMLS # 2160858), and a wholly-owned subsidiary of AlpacaDB, Inc. Alpaca Crypto is not a member of SIPC or FINRA. Cryptocurrencies are not stocks and your cryptocurrency investments are not protected by either FDIC or SIPC. Please see the Disclosure Library for more information.
This is not an offer, solicitation of an offer, or advice to buy or sell securities or cryptocurrencies or open a brokerage account or cryptocurrency account in any jurisdiction where Alpaca Securities or Alpaca Crypto, respectively, are not registered or licensed, as applicable