Get_bars vs. get_barset

Apologies if this is a silly question, but I’m not sure which of these methods to use:




I read somewhere that get_barset is the newer version, but it also seems that get_bars is part of the V2 API.

Is there a particular difference between the two functions? Is one better than the other?

@Dan_Whitnable_Alpaca @mahmoud_alpaca

The get_barset is marked as deprecated in the current Python API sources on GitHub - most probably because it uses the Alpaca Data API v1 endpoint for requesting data.


Thanks for clearing that up, Oleg :smile:

1 Like

It would be nice that documentation on the site was updated to reflect as such.

# Get daily price data for AAPL over the last 5 trading days.
barset = api.get_barset('AAPL', 'day', limit=5)
aapl_bars = barset['AAPL']
1 Like

Absolutely. The documentation is a mess. I’m sure it was quite accurate and organised at the beginning, but now, with so much changes and developments, the documentation is all over the place.

I think Alpaca should completely re-do the documentation from scratch. Maybe present it in a wiki format…

Hey Maxks90,

Maybe you know how to fix this based on your question? I went by the documentation (beginner here) and used the example to pull for 2 days of data (current/or close and previous day close). If I change this to get_bars, it breaks.

barset = api.get_barset(ticker, ‘day’, limit=2)
bars = barset[ticker]

Do you know how I need to change this to use for the get_bars() instead? I tried changing some parms around, but it seems that I get a different data object back from what I was using.

Thanks for any help!

— Update

I tried to change the date to RFC-3339 type format, but not getting anywhere. Doesn’t like this either. Stuck.

d2 = datetime.datetime(2022, 1, 27)
d2_with_timezone = d2.replace(tzinfo=pytz.UTC)
x = d2_with_timezone.isoformat()

d1 = datetime.datetime(2022, 1, 28)
d1_with_timezone = d1.replace(tzinfo=pytz.UTC)
y = d1_with_timezone.isoformat()

bars = api.get_bars(ticker, ‘1Day’, start=x, end=y, limit=2)
bars = bars[ticker]

So this will return just (1) bar, but not (2) bars for current and previous day. Not sure why that is the case.

1 Like

So it appears if I start 1/26/2022 and end 1/27/2022 that I get 2 bars. What I need to do is get previous day and if market is open the most recently delayed 15 min daily bar.

1 Like

I think @Dan_Whitnable_Alpaca mentioned at some point that, with get_barset(), the bars are calculated backwards (or starting with the most recent one). This is why you can simply put barset = api.get_barset(ticker, ‘day’, limit=2) and get the last 2 days.

For some reason, despite being newer (and supposedly better), get_bars() doesn’t do this. Instead, the bars are calculated forwards, which means you need to specify the start time.

This is what I would do:

def get_last2Days_bars(_ticker):
    _timeNow ='US/Eastern'))
    _2DaysAgo = _timeNow - dt.timedelta(days=2) 

    _bars = api.get_bars(_ticker, TimeFrame.Day,

This should get the last two daily bars. As the market is currently open (at time of posting), the results are a bar for today and a bar for yesterday (28th and 27th).

I imagine that, if the market were currently closed, the function would return the previous two days (26th and 27th). I obviously haven’t tested this though.

1 Like

Thank you so much Maxks90! This works wonderfully. I was able to quickly test to make sure I could run and then merged back into my main program. The rest of the code works great as access by index of bars[0] and bars[1]

Appreciate greatly the time and attention you provided to ensure that I build the right foundation with Alpaca.

1 Like

Per my post yesterday - this is where we need examples like this that you just provided to help others convert over from outdated documentation.

1 Like

@Dan_Whitnable_Alpaca, would it be possible to get this functionality added to get_bars()? I imagine most people would want to get the most recent bars by default, so this would make things a lot easier. Those who want to get bars from a specific date range can simply use the start and end parameters.

1 Like