sheep
August 10, 2022, 11:40pm
1
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
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.
1 Like
sheep
September 10, 2022, 12:20am
3
thanks, but how do I remove extended hours bars from the list?
thanks, but how do I remove extended hours bars from the list?
df = alpaca.get_bars(
symbol="AAPL",
start=datetime.date.today() - datetime.timedelta(days=10),
timeframe=TimeFrame.Minute,
adjustment="raw",
).df.tz_convert("US/Eastern").between_time("9:30", "16:00").tail(2000)
Notice that in this particular example a week is not enough to get 2000 AAPL bars (in the last week there were only 1955 of them).
sheep
January 14, 2023, 10:54pm
6
thanks, how to get non_holiday and non_weekend bars only?
When the market is closed (on holidays and weekends), no trades happen, thus no bars are generated either.