# SnapshotsV2 in AlpacaTradeApi

**URL:** <https://forum.alpaca.markets/t/snapshotsv2-in-alpacatradeapi/7109>\
**Category:** Alpaca Market Data\
**Created:** [October 14, 2021, 11:27pm UTC](https://forum.alpaca.markets/t/snapshotsv2-in-alpacatradeapi/7109 "2021-10-14T23:27:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dougoxmich](https://avatars.discourse-cdn.com/v4/letter/d/b5ac83/32.png) [@dougoxmich](https://forum.alpaca.markets/u/dougoxmich)\
**Post date:** [October 14, 2021, 11:27pm UTC](https://forum.alpaca.markets/t/snapshotsv2-in-alpacatradeapi/7109/1 "2021-10-14T23:27:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![okway\_usway](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/okway_usway/32/3163_2.png) [@okway\_usway](https://forum.alpaca.markets/u/okway_usway)\
**Post date:** [October 15, 2021, 4:49pm UTC](https://forum.alpaca.markets/t/snapshotsv2-in-alpacatradeapi/7109/2 "2021-10-15T16:49:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Dan\_Whitnable\_Alpaca](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/dan_whitnable_alpaca/32/1658_2.png) [@Dan\_Whitnable\_Alpaca](https://forum.alpaca.markets/u/Dan_Whitnable_Alpaca)\
**Post date:** [October 18, 2021, 5:20pm UTC](https://forum.alpaca.markets/t/snapshotsv2-in-alpacatradeapi/7109/3 "2021-10-18T17:20:50Z")

</div>

@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.

```auto
# 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

 ![image](https://us1.discourse-cdn.com/flex020/uploads/alp/original/2X/4/45737e08af40d2e447e4c05e85f1b9ae755596a2.png)

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

---

<div class="post-metadata">

**Author:** ![phellstrand](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/phellstrand/32/4169_2.png) [@phellstrand](https://forum.alpaca.markets/u/phellstrand)\
**Post date:** [May 2, 2022, 5:48pm UTC](https://forum.alpaca.markets/t/snapshotsv2-in-alpacatradeapi/7109/4 "2022-05-02T17:48:12Z")

</div>

@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

---

<div class="post-metadata">

**Author:** ![Dan\_Whitnable\_Alpaca](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/dan_whitnable_alpaca/32/1658_2.png) [@Dan\_Whitnable\_Alpaca](https://forum.alpaca.markets/u/Dan_Whitnable_Alpaca)\
**Post date:** [May 9, 2022, 9:33pm UTC](https://forum.alpaca.markets/t/snapshotsv2-in-alpacatradeapi/7109/5 "2022-05-09T21:33:26Z")

</div>

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

```auto
# 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?
