Get_bars per minute only return 724 bars

Trying to get last 2000 bars for AAPL, but only get 724:

alpaca = api.REST(API_KEY, API_SECRET, BASE_URL)

df = alpaca.get_bars(“AAPL”, TimeFrame.Minute, limit=2000, adjustment=‘raw’).df

print(len(df[‘close’].values.tolist())) # prints 724

Is this because I am using free tier?

Hey :wave:

Unfortunately there’s no way to “get the last 2000 bars for AAPL” using our API. The only supported way of querying our market data is to “get at most 2000 bars in the given start-end interval”, and the data always is in ascending order by timestamp.

Since in your function call you’re not providing the start or the end arguments their defaults are used, which for start is the beginning of the current day and for end is the current time, or the current time - 15 minutes if you’re on the free plan. So you’re query really is:

alpaca.get_bars(
    symbol="AAPL",
    start=today_morning,
    end=now,
    timeframe=TimeFrame.Minute,
    limit=2000,
    adjustment="raw",
)

If you run this query at the weekend for example, you’ll get 0 bars. When you ran it there were 724 bars on that day.

If you really really want to get the last 2000 AAPL bars, you can work around this in the following way:

week_ago = datetime.date.today() - datetime.timedelta(days=7)
df = alpaca.get_bars(
    symbol="AAPL",
    start=week_ago,
    timeframe=TimeFrame.Minute,
    adjustment="raw",
).df.tail(2000)

This will query all bars in the last one week (which should pretty much always be more than 2000 for AAPL), and then takes the last 2000 of them.

thanks, but how do I remove extended hours bars from the list?