Getting Started with Market Data API

This is a quick guide on how to start consuming market data via APIs. Starting from beginning to end, this section outlines how to install Alpaca’s software development kit (SDK), create a free alpaca account, locate your API keys, and how to request both historical and real-time data.

Installing Alpaca’s Client SDK

In this guide, we’ll be making use of the SDKs provided by Alpaca. Alpaca maintains SDKs in four languages: Python, JavaScript, C#, and Go. Follow the steps in the installation guide below to install the SDK of your choice before proceeding to the next section.

pip install alpaca-py
npm install --save @alpacahq/alpaca-trade-api

dotnet add package Alpaca.Markets

go get -u github.com/alpacahq/alpaca-trade-api-go/v2/alpaca

Generate API Keys

How to Request Market Data Through the SDK

With the SDK installed and our API keys ready, you can start requesting market data. Alpaca offers many options for both historical and real-time data, so to keep this guide succint, these examples are on obtaining historical and real-time bar data. Information on what other data is available can be found in the Market Data API reference.

To start using the SDK for historical data, import the SDK and instantiate the crypto historical data client. It’s not required for this client to pass in API keys or a paper URL.

from alpaca.data.historical import CryptoHistoricalDataClient

# No keys required for crypto data
client = CryptoHistoricalDataClient()
const Alpaca = require("@alpacahq/alpaca-trade-api");
dotnet new console -o MyApp -f net6.0

package main

import (
	"fmt"
	"time"

	"github.com/alpacahq/alpaca-trade-api-go/v2/marketdata"
)

func main() {

}

Next we’ll define the parameters for our request. Import the request class for crypto bars, CryptoBarsRequest and TimeFrame class to access time frame units more easily. This example queries for historical daily bar data of Bitcoin in the first week of September 2022.

from alpaca.data.requests import CryptoBarsRequest
from alpaca.data.timeframe import TimeFrame
from datetime import datetime

# Creating request object
request_params = CryptoBarsRequest(
                        symbol_or_symbols=["BTC/USD"],
                        timeframe=TimeFrame.Day,
                        start=datetime(2022, 9, 1),
T00:00:00Z",
                        end=datetime(2022, 9, 7)
T00:00:00Z"
                        )

Finally, send the request using the client’s built-in method, get_crypto_bars. Additionally, we’ll access the .df property which returns a pandas DataFrame of the response.

# Retrieve daily bars for Bitcoin in a DataFrame and printing it
btc_bars = client.get_crypto_bars(request_params)

# Convert to dataframe
btc_bars.df
const Alpaca = require("@alpacahq/alpaca-trade-api");
dotnet new console -o MyApp -f net6.0

package main

import (
	"fmt"
	"time"

	"github.com/alpacahq/alpaca-trade-api-go/v2/marketdata"
)

func main() {

}

Returns

		open	high	low	close	volume	trade_count	vwap
symbol	timestamp							
BTC/USD	
        2022-09-01 05:00:00+00:00	20049.0	20285.0	19555.0	20160.0	2396.3504	18060.0	19920.278135
        2022-09-02 05:00:00+00:00	20159.0	20438.0	19746.0	19924.0	1688.0641	16730.0	20045.987764
        2022-09-03 05:00:00+00:00	19924.0	19963.0	19661.0	19802.0	624.1013	9853.0	19794.111057
        2022-09-04 05:00:00+00:00	19801.0	20060.0	19599.0	19892.0	1361.6668	8489.0	19885.445568
        2022-09-05 05:00:00+00:00	19892.0	20173.0	19640.0	19762.0	2105.0539	11900.0	19814.853546
        2022-09-06 05:00:00+00:00	19763.0	20025.0	18539.0	18720.0	3291.1657	19591.0	19272.505607
        2022-09-07 05:00:00+00:00	18723.0	19459.0	18678.0	19351.0	2259.2351	16204.0	19123.487500

Request ID

All market data API endpoint provides a unique identifier of the API call in the response header with X-Request-ID key, the Request ID helps us to identify the call chain in our system.

Make sure you provide the Request ID in all support requests that you created, it could help us to solve the issue as soon as possible. Request ID can't be queried in other endpoints, that is why we suggest to persist the recent Request IDs.

$ curl -v https://data.alpaca.markets/v2/stocks/bars
...
> GET /v2/stocks/bars HTTP/1.1
> Host: data.alpaca.markets
> User-Agent: curl/7.88.1
> Accept: */*
>
< HTTP/1.1 403 Forbidden
< Date: Fri, 25 Aug 2023 09:37:03 GMT
< Content-Type: application/json
< Content-Length: 26
< Connection: keep-alive
< X-Request-ID: 0d29ba8d9a51ee0eb4e7bbaa9acff223
<
...