SnapshotsV2 in AlpacaTradeApi

Could somebody show me a working example of retrieving and parsing snapshots in v2 with the latest Alpaca Trade Api?
Thanks,
Doug

I’m using the sandbox api url for testing
https://data.sandbox.alpaca.markets/v2/stocks/snapshots?symbols=AAPL,TSLA,GOOGL

@dougoxmich Snapshots have a rather complex structure being a list of objects with each object having multiple attributes. They are understandably a bit difficult to parse. Here is a python example of how to get snapshot data for several stocks and then place it into a nicely formatted dataframe. The five data objects returned by a snapshot can be accessed with these names (using the python SDK)

latest_trade
latest_quote
minute_bar
daily_bar
prev_daily_bar

The attributes of each object can be accessed with these names (again when using the python SDK)

trade_mapping_v2 = {
“i”: “id”,
“S”: “symbol”,
“c”: “conditions”,
“x”: “exchange”,
“p”: “price”,
“s”: “size”,
“t”: “timestamp”,
“z”: “tape”, # stocks only
“tks”: “takerside” # crypto only
}

quote_mapping_v2 = {
“S”: “symbol”,
“x”: “exchange”, # crypto only
“ax”: “ask_exchange”,
“ap”: “ask_price”,
“as”: “ask_size”,
“bx”: “bid_exchange”,
“bp”: “bid_price”,
“bs”: “bid_size”,
“c”: “conditions”, # stocks only
“t”: “timestamp”,
“z”: “tape” # stocks only
}

bar_mapping_v2 = {
“S”: “symbol”,
“x”: “exchange”, # crypto only
“o”: “open”,
“h”: “high”,
“l”: “low”,
“c”: “close”,
“v”: “volume”,
“t”: “timestamp”,
“n”: “trade_count”,
“vw”: “vwap”
}

So, something like this to get various data. First make a dict including the data you want. Create the labels for the columns. Then turn the dict into a dataframe.

# Make a dict which includes whatever data from the snapshot one wishes
# These will become the columns in the final dataframe
snapshot_dict = {stock: [snapshot.prev_daily_bar.close, 
                         snapshot.daily_bar.open,
                         snapshot.latest_trade.price,
                         snapshot.latest_quote.bid_price,
                         snapshot.minute_bar.volume]
                 for stock, snapshot in snapshots.items()
                 }

# Define the column names (must be in the same order as defined above)
snapshot_columns=['prev_close', 'todays_open', 'latest_price', 'latest_bid', 'last_minute_volume']

# Turn the dict into a dataframe
snapshot_df = pd.DataFrame(snapshot_dict.values(), snapshot_dict.keys(), columns=snapshot_columns)

The result will look like this

You could of course access the data directly from the dict but I find dataframes immensely easier to use. Hope that helps.

@Dan_Whitnable_Alpaca

Hi, I’m getting the following error when using this code; will you please help explain. Thanks!


NameError Traceback (most recent call last)
in ()
6 snapshot.latest_quote.bid_price,
7 snapshot.minute_bar.volume]
----> 8 for stock, snapshot in snapshots.items()
9 }
10

NameError: name ‘snapshots’ is not defined

@phellstrand Did you fetch all the snapshots using the API? Something like this

# Define whatever symbols you want snapshots for then fetch all at once
symbols = ['IBM', 'AAPL', 'F']
snapshots = api_data.get_snapshots(symbols)

That may have been what you missed?