This tutorial is a step-by-step demonstration of how to fetch historical data with the Alpaca Market Data API v2. This guide is part of the Alpaca 101 Tutorial Series. We will move forward assuming that you’ve registered on the Alpaca website. If you haven’t please visit Alpaca and sign up. Feel free to check out the other tutorials in this series at Alpaca Resources.
About the Alpaca Market Data API v2.
Data is the backbone of API access to the market, and we have established our very own data product that can evolve alongside the Alpaca platform. We are starting minimally with real-time and historical price data and will be adding various types of data suited for the Alpaca users. Users can find the documentation for the Alpaca Market Data API v2 below:
This tutorial will be over fetching historical data in Alpaca Market Data API v2. It is worth noting that the Alpaca Data API v2 provides historical data through multiple endpoints. These endpoints have the same URL prefix: https://data.alpaca.markets/v2
While the URL is the same for both subscription plans, users with a Free Plan will receive an error when trying to access data that is too recent.
Setting up Postman
For this tutorial, we will be implementing Postman to test any API calls which we send to the Alpaca Market Data API v2. As of right now, Alpaca Data API v2 provides three types of historical data which are Trades, Quotes, and Bars. That being said, either generate new API keys or fetch your previously generated paper API keys. Now that you have your keys ready, open up the Postman application.
- To initiate your first API request, click the + tab in the Postman UI.
- Add the following request URL:
https://paper-api.alpaca.markets/v2/account - Add your API keys
Click the Headers section and add two keys called APCA-API-KEY-ID and APCA-API-SECRET-KEY. Enter your key values in the VALUE column. - Hit the Send button
You should be able to see various account information from this API call. Now you’re ready to trade!
Fetching Historical Trade data
Let’s create a raw request to fetch historical trade data for the ETF SPY. As we are interested in fetching historical data we append the following URL
GET/v2/stocks/{symbol}/trades
to the URL prefix: https://data.alpaca.markets/v2
Note SPY
is the symbol for the SPY ETF we wish to fetch historical trade data for. We then specify the symbol, which in our case is the path parameter of SPY
. Moreover, we set the required query parameters of start
, end
, and limit
as the values of 2021-02-06
, 2021-02-07
, and 10
respectively. Testing our API call we see we receive the expected response of a trades response object containing the timestamp (t), exchange (x), trade price (p), trade size (s), trade conditions (c), trade ID (i), and tape (z) for 10 trades (as this was our limit).
Fetching Historical Quote data
Let’s create a raw request to fetch historical quote data for the ETF SPY. As we are interested in fetching historical data we append the following URL
GET/v2/stocks/{symbol}/quotes
to the URL prefix: https://data.alpaca.markets/v2
Note SPY
is the symbol for the SPY ETF we wish to fetch historical quote data for. We then specify the symbol, which in our case is the path parameter of SPY
. Moreover, we set the required query parameters of start
, end
, and limit
has the values of 2021-02-06
, 2021-02-07
, and 10
respectively. Testing our API call we see we receive the expected response of a quotes response object containing the timestamp (t), ask exchange (ax), ask price (ap), ask size (as), bid exchange (bx) , bid price (bp), bid size(bs), and quote conditions (c) for 10 quotes(as this was our limit).
Fetching Historical Bars
Let’s create a raw request to fetch historical bars for the ETF SPY. As we are interested in fetching historical data we append the following URL
GET/v2/stocks/{symbol}/bars
to the URL prefix: https://data.alpaca.markets/v2
Note SPY
is the symbol for the SPY ETF we wish to fetch historical bars for. We then specify the symbol, which in our case is the path parameter of SPY
. Moreover, we set the required query parameters of start
, end
, limit
, and timeframe
as the values of 2021-02-06
, 2021-02-07
, 10
, 1Min
respectively. Here we note the parameter `timeframe` is also required. Testing our API call we see we receive the expected response of a bars response object containing the timestamp (t), open (o), high(h), low(l), close (c), and volume (v) for 10 trades (as this was our limit) with one-minute aggregation.
Great! Now, moving forward here are some important points to remember:
- Alpaca Data API v2 provides historical data through multiple endpoints. These endpoints have the same URL prefix:
https://data.alpaca.markets/v2
- This URL is the same for both subscription plans but users with a Free Plan will receive an error when trying to access data that is too recent.
- There is a 15-minute delay for data to be classified as historical data.
- The authentication is done the same way as with the Trading API
- The query parameters
start
andend
are required for historical trades, quotes, and bars however the additional parametertimeframe
is also required for bars. - For
start
andend
, the entered data must be equal to or after this time in RFC-3339 (ISO) format. Fractions of a second are not accepted.
Thank you for using Alpaca. Here are a couple more links that you might be interested in.
You can also follow Alpaca and our weekly updates on our LinkedIn, Alpaca Community Slack, and @AlpacaHQ on Twitter!
Commission-Free trading means that there are no commission charges for Alpaca self-directed individual cash brokerage accounts that trade U.S. listed securities through an API. Relevant SEC and FINRA fees may apply.
Brokerage services are provided by Alpaca Securities LLC ("Alpaca"), member FINRA/SIPC, a wholly-owned subsidiary of AlpacaDB, Inc. Technology and services are offered by AlpacaDB, Inc.