Market Data Streaming

Alpaca Data API provides websocket streaming for trades, quotes and minute bars. This helps receive the most up to date market information that could help your trading strategy to act upon certain market movement.

Specifications

  • Each account can have up to one concurrent websocket connection.
  • Trades, quotes and minute bars are supported.
  • Subscription is limited to 30 channels at a time for trades and quotes (T. and Q.). This limit is temporary and we may support more channels in the future.
  • There is no limit for the number of channels with minute bars (AM.).
  • Trades and quotes are from the 5 exchanges.
  • Minute bars are also based on the trades in the 5 exchanges.

How to connect the websocket streaming

The message protocol is almost the same as trade API stream, but please note the endpoint is wss://data.alpaca.markets/stream (papertrading API key works with this URL, too). At the beginning of connection, send API key as part of the “authenticate” action.


{
    "action": "authenticate",
    "data": {
        "key_id": "<YOUR_KEY_ID>",
        "secret_key": "<YOUR_SECRET_KEY>"
    }
}

Then the server returns the result of authentication.


{
    "stream": "authorization",
    "data": {
        "action": "authenticate",
        "status": "authorized"
    }
}

If the authentication is already done for the connection, it returns error.


{
  "stream": "listening",
  "data": {
    "error": "your connection is already authenticated"
  }
}

If the authentication fails, it also returns error.


{
  "stream": "authorization",
  "data": {
    "action": "authenticate",
    "status": "unauthorized"
  }
}

When another connection is already open, you will receive the error below.


{
  "stream": "listening",
  "data": {
    "error": "your connection is rejected while another connection is open under the same account"
  }
}

Once the authentication is done, send a listen messsge to start receiving the messages for the channels you want to receive.


{
    "action": "listen",
    "data": {
        "streams": ["T.SPY", "Q.SPY", "AM.SPY"]
    }
}

The stream names can optionally be prefixed by alpacadatav1/


{
    "action": "listen",
    "data": {
        "streams": ["alpacadatav1/T.SPY", "alpacadatav1/Q.SPY", "alpacadatav1/AM.SPY"]
    }
}

The server responds with acknowledgement (the prefix is trimmed).


{
    "stream": "listening",
    "data": {
        "streams": ["T.SPY", "Q.SPY", "AM.SPY"]
    }
}

When you want to stop certain channels to stream, send a unlisten message.


{
    "action": "unlisten",
    "data": {
        "streams": ["T.SPY", "Q.SPY"]
    }
}

It will again responds with acknowledgement.


{
    "stream": "listening",
    "data": {
        "streams": ["AM.SPY"]
    }
}

The channel names follow the rules below.

  • T.{symbol}: trades for the symbol
  • Q.{symbol}: quotes for the symbol
  • AM.{symbol}: minute bars for the symbol. (AM.* will provide all symbols)

T = Trade schema:

ev
string
event name, always “T”
T
string
symbol
i
int
trade ID
x
int
exchange code where the trade occurred
p
number
trade price
s
int
trade size (shares)
t
int
epoch timestamp in nanoseconds
c
array of int
condition flags
z
int
tape ID

Example:

{
  "ev": "T",
  "T": "SPY",
  "i": 117537207,
  "x": 2,
  "p": 283.63,
  "s": 2,
  "t": 1587407015152775000,
  "c": [
    14,
    37,
    41
  ],
  "z": 2
}

Q = Quote schema:

ev
string
event name, always “Q”
T
string
symbol
x
int
exchange code for bid quote
p
number
bid price
s
int
bid size
X
int
exchange code for ask quote
P
number
ask price
S
int
ask size
c
array of int
condition flags
t
int
epoch timestamp in nanoseconds

Example:

{
  "ev": "Q",
  "T": "SPY",
  "x": 17,
  "p": 283.35,
  "s": 1,
  "X": 17,
  "P": 283.4,
  "S": 1,
  "c": [
    1
  ],
  "t": 1587407015152775000
}

AM = Bar schema:

ev
string
event name, always “AM”
T
string
symbol
v
int
volume (shares)
av
int
accumulated volume (shares)
op
number
official open price of the bar
vw
number
VWAP
o
number
open price of the bar
h
number
high price of the bar
l
number
low price of the bar
c
number
close price of the bar
a
number
average price of the bar
s
int
epoch time at the beginning of the window in milliseconds
e
int
epoch time at the ending of the window in milliseconds

Example:

{
  "ev": "AM",
  "T": "SPY",
  "v": 48526,
  "av": 9663586,
  "op": 282.6,
  "vw": 282.0362,
  "o": 282.13,
  "c": 281.94,
  "h": 282.14,
  "l": 281.86,
  "a": 284.4963,
  "s": 1587409020000,
  "e": 1587409080000
}

Below is the example of full message exchanges.


$ wscat -c wss://data.alpaca.markets/stream
connected (press CTRL+C to quit)
>  {"action": "authenticate","data": {"key_id": "<YOUR_KEY_ID>", "secret_key": "<YOUR_SECRET_KEY>"}}
< {"stream":"authorization","data":{"action":"authenticate","status":"authorized"}}
>  {"action": "listen", "data": {"streams": ["T.SPY"]}}
< {"stream":"listening","data":{"streams":["T.SPY"]}}