My goal is simply to get end of day OHLC and volume data from the pricing API to update on daily basis a larger historic database of all the securities I have already pulled from the Alpaca API. I am able to get the data in what appears a nested dictionary using this code:
def multiquotes():
from alpaca.data.historical import StockHistoricalDataClient
from alpaca.data.requests import StockSnapshotRequest
# keys required for stock historical data client
client = StockHistoricalDataClient(ALPACA_API_KEY, ALPACA_SECRET_KEY)
# multi symbol request - single symbol is similar
multisymbol_request_params = StockSnapshotRequest(symbol_or_symbols=["SPY", "TSLA"])
latest_multisymbol_quotes = client.get_stock_snapshot(multisymbol_request_params)
print(latest_multisymbol_quotes)
The issue is it seems to return a large nested dictionary like so
{‘SPY’: { ‘daily_bar’: { ‘close’: 446.46,
‘high’: 447.68,
‘low’: 445.08,
‘open’: 446.1,
‘symbol’: ‘SPY’,
‘timestamp’: datetime.datetime(2023, 9, 13, 4, 0, tzinfo=datetime.timezone.utc),
‘trade_count’: 9198.0,
‘volume’: 1021666.0,
‘vwap’: 446.589737},
‘latest_quote’: { ‘ask_exchange’: ‘V’,
‘ask_price’: 449.21,
‘ask_size’: 10.0,
‘bid_exchange’: ‘V’,
‘bid_price’: 448.03,
‘bid_size’: 10.0,
‘conditions’: [‘R’],…
But normal code to work with dictionaries fails:
open_prices = {}
# Iterate through each symbol's data
for symbol, symbol_data in latest_multisymbol_quotes.items():
# Get the 'open' price from the 'daily_bar' section
open_price = symbol_data['daily_bar']['open']
# Store the open price in the dictionary with the symbol as the key
open_prices[symbol] = open_price
because I am getting this error
open_price = symbol_data[‘daily_bar’][‘open’]
TypeError: ‘Snapshot’ object is not subscriptable
What is the easiest way to extract this info or convert the snapshot object to Pandas and then deal with it there?
Thanks