This tutorial is a step-by-step demonstration of how to stream real time 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:

About Market Data API
Gain seamless access to a wealth of data with Alpaca Market Data API, offering real-time and historical equities & crypto information spanning over 7+ years.

This tutorial will be over streaming real time data in Alpaca Market Data API v2. It is worth noting that the Alpaca Data API v2 provides WebSocket streaming for trades, quotes, and minute bars.


Setting up Terminal

For this tutorial, we will be streaming real-time data through our terminal. First off, make sure you have wscat or equivalent installed so we can set up a WebSocket directly from our terminal.

Great, now to access real-time data use the following URL, where the source is either iex or sip depending on your subscription.

wss://stream.data.alpaca.markets/v2/{source}

It is necessary that you authenticate within the 10-second window to authenticate or you will be automatically disconnected. That being said to authenticate send the following action:

{"action": "auth", "key": "{APCA-API-KEY-ID}", "secret": "{APCA-API-SECRET-KEY}"}


Streaming Real Time Trades

Let’s subscribe to trade data for the stock Apple denoted by the symbol AAPL. As we are interested in streaming trade data we send the following action:

{"action":"subscribe","trades":["AAPL"]}

After we subscribe we will receive a message that describes our current list of subscriptions such as this:

[{"T":"subscription","trades":["AAPL"],"quotes":[],"bars":[]}]

Once we are subscribed to a given symbol we will begin receiving data points from the server. Trade data points have the following format: where “T” is the message type, always “t”, “S” is the symbol, “i” is the trade ID, “x” is the exchange code where the trade occurred, “p” is the trade price, “s” is the trade size”, “t” is the RFC-3339 formatted timestamp with nanosecond precision, “c” is the trade condition, and “z” is the tape.

The actions we described along with examples of the trade schema are shown below:

Subscribing to Trades

Streaming Real Time Quotes

Let’s subscribe to quote data for the stock Apple denoted by the symbol AAPL. As we are interested in streaming quote data we send the following action:

{"action":"subscribe","quotes":["AAPL"]}

After we subscribe we will receive a message that describes our current list of subscriptions such as this:

[{"T":"subscription","trades":[],"quotes":["AAPL"],"bars":[]}]

Once we are subscribed to a given symbol we will begin receiving data points from the server. Quote data points have the following format: where “T” is the message type, always “q”, “S” is the symbol, “ax” is the ask exchange code, “ap” is the ask price, “as” is the ask size,  “bx” is the bid exchange code, “bp” is the bid price,  “bs” is the bid size”, “t” is the RFC-3339 formatted timestamp with nanosecond precision, “c” is the quote condition, and “z” is the tape.

The actions we described along with examples of the quote schema are shown below:

Subscribing to Quotes

Streaming Real Time Bars

Let’s subscribe to minute bars for the stock Apple denoted by the symbol AAPL. As we are interested in streaming minute bars we send the following action:

{"action":"subscribe","bars":["AAPL"]}

After we subscribe we will receive a message that describes our current list of subscriptions such as this:

[{"T":"subscription","trades":[],"quotes":[],"bars":["AAPL"]}]

Once we are subscribed to a given symbol we will begin receiving data points from the server. Bars have the following format: where “T” is the message type, always “b”, “S” is the symbol, “o” is the open price, “h” is the high price,  “l” is low price, “c” is the close price”, “v” is volume, and “t” is the RFC-3339 formatted timestamp with nanosecond precision.

The actions we described along with examples of the bar schema are shown below:

Subscribing to Bars

Great! Now, moving forward here are some important points to remember:

  • Users with Free Plan can only connect to the IEX data source.
  • Users with the Unlimited Plan have direct feeds of trades, quotes, and minute bars from the CTA (administered by NYSE) and UTP (administered by Nasdaq) SIPs.
  • Users with Free Plan are allowed one concurrent connection and the subscription is limited to 30 channels at a time for trades and quotes. It is worth noting that there is no limit to the number of channels with minute bars.
  • Users with Unlimited Plan have no limit for the number of channels at a time for trades, quotes, and minute bars.
  • There is a full list of Server-Client messages and errors one might experience detailed full in the Alpaca Docs for the Market Data API v2 below:
WebSocket Stream
This API provides a WebSocket stream for real-time market data. This allows you to receive the most up-to-date market information, which can be used to power your trading strategies.To use the WebSocket stream, you first need to establish a connection, successfully authenticated your credential usin…

Thank you for using Alpaca. Here are a couple more links that you might be interested in.

Alpaca GitHub repository

Alpaca
Alpaca builds an API for free stock trading. Alpaca has 41 repositories available. Follow their code on GitHub.

Alpaca Blog

Alpaca Blog | API for Stock Trading
Alpaca Blog for product updates, new integration, and more. Alpaca’s commission-free trading API is built for algorithmic trading and building apps

Alpaca Forum

Alpaca Community Forum
Alpaca user forum for FAQ, feature requests, many discussions around Alpaca stock trading API

Alpaca API documentation

Welcome
This page will help you get started with Alpaca Docs. You’ll be up and running in a jiffy!

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.