Get_bars vs. get_barset

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

api.get_bars()

or

api.get_barset()

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?

Thanks
@Dan_Whitnable_Alpaca @mahmoud_alpaca

1 Like

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.

2 Likes

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…

3 Likes

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]
print(bars)

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]
print(bars)

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 = dt.datetime.now(pytz.timezone('US/Eastern'))
    _2DaysAgo = _timeNow - dt.timedelta(days=2) 

    _bars = api.get_bars(_ticker, TimeFrame.Day,
                         start=_2DaysAgo.isoformat(),
                         end=None,
                         limit=2
                         )
    print(_bars)

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).
image

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.

3 Likes

@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

Came on this thread via googling. Alpaca documentation is the worst for an API I’ve ever seen. A wiki would be appreciated. There are so many simple errors that could be corrected.

It makes me question whether I should trust my money to this platform.

@galactic Alpaca is transitioning to a different API documentation platform which can be found at https://docs.alpaca.markets/. Some of the confusion may be from links which point to the old docs. The old docs aren’t maintained and are in place only during the transition period.

A few things to note about the docs 1) they document the API methods and not any specific SDK and 2) the Alpaca APIs are grouped into

  • Trading APIs (for placing trades and checking ones account)
  • Market Data APIs
  • Connect APIs (for creating 3rd party apps using OAuth)
  • Broker APIs (only applicable if you are a registered broker partner)

There is an overview of these API groups in the getting started or Welcome section of the docs.

The SDKs are generally provided ‘as is’ and the Github repositories contain the base code and some documentation. The alpaca-py python SDK however, is documented separately here.

Note there are two distinct areas of the docs 1) the Documentation area and 2) the API Reference area. As those names imply, the Documentation area has general explanation and background information (eg types of supported orders). The API Reference contains the specific details and parameters for each endpoint.

If you see any errors, or have suggestions on how to make the documentation better, there is a “Suggest Edits” link on each page (Documentation area only). Everyone is encouraged to use those links and let Alpaca know how to make the Documentation better.

All that said, @galactic are there specific errors you have found or areas which could be done better?

Oh ok, it’s good to see there is newer documentation. Yes, there are specific errors like this:

It’s the 3rd result when I search for “alpaca tutorials.” Numerous obvious errors in this tutorial, like the code snippets missing line breaks and the use of get_barset(). It looks like it’s part of official docs based on URL, and “Alpaca team” stated at the top as the author, but it’s terribly out of date with code that doesn’t work. Let me reiterate that this was the 3rd result in my google query for “alpaca tutorials.” This needs to be a redirect to a current tutorial.

@galactic Yes, these “learn” articles can get out of date quickly. This specific one was from Dec 21, 2021 and posted by a community member and, as noted, has many things which aren’t current.

I’ll begin an internal dialog for a process to keep the various learn articles up to date. It’s a known issue.