Streaming trade data has gaps

When I subscribe to a ticker’s real-time trades, I get second gaps in the data. I’m using the alpaca python v2 streaming api (alpaca-trade-api 1.2.1) and python3.9.5 on macos 10.15. I was expecting to get all trades but it seems it just does a sampling that’s somewhat random. I tried using the historical data API to fetch all trades from the last minute, but even with a look-back time of 3 minutes, I get a 422 Unprocessable Entity error on the request. Two questions:

  1. Is there a streaming API that gives you real-time trades and guarantees delivery of all trades?
  2. What is the minimum look-back period for getting historical trade data?

Example program with trade times:

INFO:alpaca_trade_api.stream:started data stream
INFO:alpaca_trade_api.stream:connected to: wss://stream.data.alpaca.markets/v2/iex
INFO:alpaca_trade_api.stream:subscribed to trades: ['AAPL'], quotes: [] and bars: []
trade_time:2021-06-24 18:37:40.069432+00:00, current_time:2021-06-24 18:37:40.702806+00:00
trade_time:2021-06-24 18:37:44.091103+00:00, current_time:2021-06-24 18:37:44.918916+00:00
trade_time:2021-06-24 18:37:44.093297+00:00, current_time:2021-06-24 18:37:44.941262+00:00
trade_time:2021-06-24 18:37:45.000567+00:00, current_time:2021-06-24 18:37:45.013751+00:00
trade_time:2021-06-24 18:37:45.002291+00:00, current_time:2021-06-24 18:37:45.031236+00:00
trade_time:2021-06-24 18:37:45.012025+00:00, current_time:2021-06-24 18:37:45.127809+00:00
trade_time:2021-06-24 18:37:45.016740+00:00, current_time:2021-06-24 18:37:45.175280+00:00
trade_time:2021-06-24 18:37:45.022448+00:00, current_time:2021-06-24 18:37:45.231731+00:00
trade_time:2021-06-24 18:38:05.040133+00:00, current_time:2021-06-24 18:38:05.408625+00:00
trade_time:2021-06-24 18:38:05.094710+00:00, current_time:2021-06-24 18:38:06.068356+00:00
trade_time:2021-06-24 18:38:09.009501+00:00, current_time:2021-06-24 18:38:09.103607+00:00
trade_time:2021-06-24 18:38:12.002677+00:00, current_time:2021-06-24 18:38:12.037967+00:00

@natan The wss://stream.data.alpaca.markets/v2/sip streaming endpoint delivers real time trades consolidated across all markets. There is roughly a 20ms latency plus whatever network latency between Alpaca and the client, but all trades are included.

The reason you are seeing ‘gaps’ is because the endpoint is wss://stream.data.alpaca.markets/v2/iex. That only streams trades which occurred on the IEX exchange which typically only accounts for about 2% of the total market. You will need a subscription to the Alpaca ‘Unlimited’ data plan, but change the streaming connection to wss://stream.data.alpaca.markets/v2/sip and you will see all trades without the ‘gaps’.

There’s more information about the difference in the documentation.

1 Like

Thanks for the quick response Dan. I’m a neophyte when it comes to the different exchanges and what data they provide. Makes sense!