How to Fetch the Latest Trade/Quote in Alpaca Market Data API v2
In this tutorial, we will go over How to Fetch the Latest Trade/Quote with Raw Requests via the Alpaca Market Data API v2.
This tutorial is a step-by-step demonstration of how to fetch the latest trade/quote in 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 cover fetching the latest trade/quote in Alpaca Market Data API v2. While the latest trade/quote endpoints are most frequently used in real-time, these endpoints are prefixed with the same URL one would use to access historical data with the Alpaca Market Data API v2:
https://data.alpaca.markets/v2
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 Latest Trade
Let’s create a raw request to fetch the latest trade for the ETF SPY. Therefore we will append the following URL
/v2/stocks/{symbol}/trades/latest
to the URL prefix: https://data.alpaca.markets/v2
Note SPY is the symbol for the SPY ETF we wish to fetch the latest trade for. We then specify the symbol, which in our case is the path parameter of SPY. Our API Call is then the following
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 the latest trade.
Fetching Latest Quote
Let’s create a raw request to fetch the latest quote for the ETF SPY. Therefore we will append the following URL
/v2/stocks/{symbol}/quotes/latest
to the URL prefix: https://data.alpaca.markets/v2
Note SPY is the symbol for the SPY ETF we wish to fetch the latest quote for. We then specify the symbol, which in our case is the path parameter of SPY. Our API Call is then the following
https://data.alpaca.markets/v2/stocks/SPY/quotes/latest
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).
{
"symbol": "SPY",
"quote": {
"t": "2021-05-13T14:27:51.742904322Z",
"ax": "V",
"ap": 411.02,
"as": 2,
"bx": "V",
"bp": 411,
"bs": 1,
"c": [
"R"
]
}
}
Great! Now, moving forward here are some important points to remember:
- While the latest trade/quote endpoints are most frequently used in real-time, these endpoints are prefixed with the same URL one would use to access historical data with the Alpaca Market Data API v2:
https://data.alpaca.markets/v2
- The authentication is done the same way as with the Trading API
- The URL is the same for all users however the data source will differ based on the user’s subscription plan.
- Users with the Free Plan will only be able to access the latest trade/quote from IEX
- Users with the Unlimited Plan will be able to access the latest trade/quote from the direct feeds from the CTA (administered by NYSE) and UTP (administered by Nasdaq) SIPs
Thank you for using Alpaca. Here are a couple more links that you might be interested in.
Sign up for the weekly newsletter to keep up with the API updates and upcoming competitions, job opportunities by clicking here.
You can also follow Alpaca and our weekly updates on our LinkedIn, Alpaca Community Slack, and @AlpacaHQ on Twitter!
Technology and services are offered by AlpacaDB, Inc. Brokerage services are provided by Alpaca Securities LLC, member FINRA/SIPC. Alpaca Securities LLC is a wholly-owned subsidiary of AlpacaDB, Inc.
Please do note that this is for illustration only and is not a recommendation or suggestion of this trade or this stock.
`