Market Data - Alpaca-py (2024)

Toggle table of contents sidebar

The market data API allows you to access both live and historical data for equities, cryptocurrencies, and options.Over 5 years of historical data is available for thousands of equity and cryptocurrency symbols.Various data types are available such as bars/candles (OHLCV), trade data (price and sales), and quote data. Forcrypto, there is also orderbook data. For more information on the data types available,please look at the API reference.

Subscription Plans#

Most market data features are free to use. However, if you are a professional or institution, you maywish to expand with the unlimited plan. Learn more about the subscriptions plans atalpaca.markets/data.

API Keys#

You can sign up for API keys here. API keys allow you to accessstock data. Keep in mind, crypto data does not require authentication to use. i.e. you can initialize CryptoHistoricalDataClient withoutproviding API keys. However, if you do provide API keys, your rate limit will be higher.

Historical Data#

There are 2 historical data clients: StockHistoricalDataClient, CryptoHistoricalDataClient, and OptionHistoricalDataClient.The crypto data client does not require API keys to use.

Clients#

Historical Data can be queried by using one of the two historical data clients: StockHistoricalDataClient,CryptoHistoricalDataClient, and OptionHistoricalDataClient. Historical data is available for Bar, Trade and Quote datatypes.For crypto, latest orderbook data is also available.

from alpaca.data import CryptoHistoricalDataClient, StockHistoricalDataClient, OptionHistoricalDataClient# no keys required.crypto_client = CryptoHistoricalDataClient()# keys requiredstock_client = StockHistoricalDataClient("api-key", "secret-key")option_client = OptionHistoricalDataClient("api-key", "secret-key")

Retrieving Latest Quote Data#

The latest quote data is available through the historical data clients.The method will return a dictionary of Trade objects that are keyed by the correspondingsymbol. We will need to use the StockLatestQuoteRequest model to prepare the request parameters.

Attention

Models that are returned by both historical data clients are agnostic of the number ofsymbols that were passed in. This means that you must use the symbol as a key to accessthe data regardless of whether a single symbol or multiple symbols were queried. Below is an exampleof this in action.

Multi Symbol

Here is an example of submitting a data request for multiple symbols. The symbol_or_symbols parametercan accommodate both a single symbol or a list of symbols. Notice how the data for a singlesymbol is accessed after the query. We use the symbol we desire as a key to access the data.

from alpaca.data.historical import StockHistoricalDataClientfrom alpaca.data.requests import StockLatestQuoteRequest# keys required for stock historical data clientclient = StockHistoricalDataClient('api-key', 'secret-key')# multi symbol request - single symbol is similarmultisymbol_request_params = StockLatestQuoteRequest(symbol_or_symbols=["SPY", "GLD", "TLT"])latest_multisymbol_quotes = client.get_stock_latest_quote(multisymbol_request_params)gld_latest_ask_price = latest_multisymbol_quotes["GLD"].ask_price

Single Symbol

This is a similar example but for a single symbol. The key thing to notice is how we stillneed to use the symbol as a key to access the data we desire. This might seem odd since we onlyqueried a single symbol. However, this must be done since the data models are agnostic to the numberof symbols.

from alpaca.data.historical import CryptoHistoricalDataClientfrom alpaca.data.requests import CryptoLatestQuoteRequest# no keys requiredclient = CryptoHistoricalDataClient()# single symbol requestrequest_params = CryptoLatestQuoteRequest(symbol_or_symbols="ETH/USD")latest_quote = client.get_crypto_latest_quote(request_params)# must use symbol to access even though it is single symbollatest_quote["ETH/USD"].ask_price

Retrieving Historical Bar Data#

You can request bar (candlestick) data via the HistoricalDataClients. In this example, we querydaily bar data for “BTC/USD” and “ETH/USD” since July 1st 2022 using CryptoHistoricalDataClient.You can convert the response to a multi-index pandas dataframe using the .df property.

from alpaca.data.historical import CryptoHistoricalDataClientfrom alpaca.data.requests import CryptoBarsRequestfrom alpaca.data.timeframe import TimeFramefrom datetime import datetime# no keys required for crypto dataclient = CryptoHistoricalDataClient()request_params = CryptoBarsRequest( symbol_or_symbols=["BTC/USD", "ETH/USD"], timeframe=TimeFrame.Day, start=datetime(2022, 7, 1), end=datetime(2022, 9, 1) )bars = client.get_crypto_bars(request_params)# convert to dataframebars.df# access bars as list - important to note that you must access by symbol key# even for a single symbol request - models are agnostic to number of symbolsbars["BTC/USD"]

Real Time Data#

Clients#

The data stream clients lets you subscribe to real-time data via WebSockets. There are clientsfor crypto data, stock data and option data. These clients are different from the historical ones. They do nothave methods which return data immediately. Instead, the methods in these clients allow you to assignmethods to receive real-time data.

from alpaca.data import CryptoDataStream, StockDataStream# keys are required for live datacrypto_stream = CryptoDataStream("api-key", "secret-key")# keys requiredstock_stream = StockDataStream("api-key", "secret-key")option_stream = OptionDataStream("api-key", "secret-key")

Subscribing to Real-Time Quote Data#

This example shows how to receive live quote data for stocks. To receive real time data, you will need to providethe client an asynchronous function to handle the data. The client will assign this provided methodto receive the real-time data as it is available.

Finally, you will need to call the run method to start receiving data.

from alpaca.data.live import StockDataStreamwss_client = StockDataStream('api-key', 'secret-key')# async handlerasync def quote_data_handler(data): # quote data will arrive here print(data)wss_client.subscribe_quotes(quote_data_handler, "SPY")wss_client.run()
Market Data - Alpaca-py (2024)
Top Articles
Latest Posts
Recommended Articles
Article information

Author: Jeremiah Abshire

Last Updated:

Views: 6528

Rating: 4.3 / 5 (74 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Jeremiah Abshire

Birthday: 1993-09-14

Address: Apt. 425 92748 Jannie Centers, Port Nikitaville, VT 82110

Phone: +8096210939894

Job: Lead Healthcare Manager

Hobby: Watching movies, Watching movies, Knapping, LARPing, Coffee roasting, Lacemaking, Gaming

Introduction: My name is Jeremiah Abshire, I am a outstanding, kind, clever, hilarious, curious, hilarious, outstanding person who loves writing and wants to share my knowledge and understanding with you.