Help with Barset

I am using Barset

Get daily price data for AAPL over the last 1 trading days.

barset = api.get_barset(‘AAPL’, ‘day’)
print (barset[“AAPL”][0])

Output is
Bar({ ‘c’: 364.01,
‘h’: 367.36,
‘l’: 363.91,
‘o’: 365.12,
‘t’: 1593576000,
‘v’: 25429807})

How can the price of AAPL be 364. What am I doing wrong?

The price of AAPL at the time of posting is $118.64

The get_barset function returns a specified number of bars of data for a stock. The number of bars it returns is determined by the limit parameter. If nothing is specified the default is 100 bars (ie limit=100). The start, end, after, and until parameters can be set to fetch data over specific time frames. If no timeframe is specified the default is to return the most recent bars. The bars are returned in ascending order. The first is the earliest date and the last is the most recent date. The data is not split or dividend adjusted. It is the actual trade data on that date.

So, the following will fetch a list of the 100 most recent trading days of data (the default) for AAPL. The first bar in the list (ie datadata[‘AAPL’][0]) will be the earliest data from 100 trading days ago. The last bar (ie datadata[‘AAPL’][-1]) will be the most recent data. The data is not split or dividend adjusted.

data = api.get_barset('AAPL', 'day',)

It’s often easier to turn the result into a dataframe. This can be done easily with the df method like this.

data_df = data.df

The result is the following dataframe

A couple things to note. The last row is the most recent data and it does indeed match the current close price of 118.64 in the chart (apparently from Yahoo). The first row (100 trading days ago) is 2020-7-7 data. AAPL indeed had a close price of 364.01 on that date. The chart data appears different because it reflects split adjusted prices. AAPL had a 4:1 stock split on 2020-8-28. This makes the chart data appear to have prices which are all 1/4 the actual prices before that split.

So, to get the most recent data look at the last bar, and remember that previous bars are not split or dividend adjusted (which may make them appear different from other sources if they adjust their data).

Thanks so much for the response. Can you point me to good documentation with different basic examples, other than the Alpaca API?

I used your method, but I see a significant difference in the volume

The volume is 74.11 M as per Yahoo(and TradingView), while as per Alpaca API, the volume is just 67.7M
AAPL
open high low close volume
2020-11-16 00:00:00-05:00 118.92 120.9900 118.146 120.35 82097057
2020-11-17 00:00:00-05:00 119.55 120.6741 118.960 119.36 66758426
2020-11-18 00:00:00-05:00 118.61 119.8200 118.000 118.00 66333033
2020-11-19 00:00:00-05:00 117.59 119.0600 116.810 118.64 67716820
2020-11-20 00:00:00-05:00 118.64 118.7700 117.290 117.32 66641286

Using the get_barset method will fetch data from IEX (Investors Exchange LLC). Basically, IEX data is available to all Alpaca users but doesn’t include trades from all exchanges (so volume, for example, may be less than the total traded across all exchanges/venues). That is why the volumes indicated above do not match the chart. There is a better explanation in this post.

To get volumes closer to what is reported on sites like Yahoo and Google, use Polygon data. (However, it’s only available if you subscribe to the data or have a live account). Something like this

polygon_data = api.polygon.historic_agg_v2(symbol='AAPL',
                            multiplier=1,
                            timespan='day',
                            _from='2020-11-1',
                            to='2020-11-18')

This shows a volume on 11-19 which is a bit higher than what’s on the chart. Actually, Polygon data is more inclusive than most sites because not only does it record volumes from all the major exchanges but also ‘dark pools’ (which act as private exchanges). Most sites (even including NASDAQ) do not include this ‘dark pool’ volume which can be quite a bit at times. Here is a quote from the Polygon documentation…

We include all 16 public exchanges + dark pools. Our feed contains 100% market volume for US trading.

Hope that helps?

this is very good… thanks. how would one calculate the ev/ebitda, p/e or price/book ratios using alpaca API? Which other platform would you suggest instead?

(Late reply) I think barset is deprecated. I had trouble finding the documentation for get_bars, the replacement for barset. I finally found something here:

1 Like