Querying historical prices on more than 200 symbols

I see from the API documentation that the max number of symbols is limited to 200: https://docs.alpaca.markets/api-documentation/api-v2/market-data/bars/

I need to query > 200 symbols over the past 5 years. How can I do so in Python? It does not need to be done within an instance, over a few minutes would be fine.

Please help!


You can query more symbols using e.g. the following method.

import alpaca_trade_api as tradeapi
import pandas as pd

key_id = "YOUR KEY"
secret_key = "YOUR SECRET KEY"
base_url = 'https://paper-api.alpaca.markets'

# The connection to the Alpaca API.
api = tradeapi.REST(key_id, secret_key, base_url)

# Get SP500 symbols.
table = pd.read_html('https://en.wikipedia.org/wiki/List_of_S%26P_500_companies')
sp500_symbols = table[0].Symbol

bars = {}
batch_size = 100
index = 0

# We need to go through the symbols in batch sizes since there is a limit on how many symbols we can get at a time.
while index < len(sp500_symbols):

    # Get bars for batch.
    symbol_batch = sp500_symbols[index:index + batch_size]
    bars_batch = api.get_barset(symbols=symbol_batch, timeframe='minute', limit=1)

    # Update overall bar dict with retrieved bars batch.

    # Update index to retrieve next batch.
    index += batch_size

# Sort bars for niceness.
bars = {key: bars[key] for key in sorted(bars.keys())}