Error: Python v2 api - get_bars - end is too late for subscription

Hi there,
Am trying to get a simple get_bars call, using Python api (api_version = ‘v2’), but it fails with obscure unexplainable error below. The call is done during market open hours.

alpaca_trade_api.rest.APIError: end is too late for subscription

The call is done using code below:
data = api.get_bars(symbol, TimeFrame.Minute, today, tomorrow, adjustment=‘raw’).df

symbol is valid ticker, today equal to today’s date in yyyy-mm-dd format, tomorrow to tomorrow’s date in the same format.

Any ideas? Very few parameters, so not sure what is going wrong.

Thanks a lot.

2 Likes

Why is it so difficult to get replies to such posts on this forum - no one from Alpaca is doing community support?

1 Like

I’m having the same issue with the get_trades api and got a result by hardcoding the start/end date strings to be ‘2021-03-21T12:56:25.010246+00:00’ and ‘2021-03-22T16:56:25.010246+00:00’.

However it’s not clear what date range values are allowed here. The documentation around this is terrible.

1 Like

OK I think I’ve figured this out after some experimentation. It looks like you need to keep the end date at least about an hour before the current time. My guess is these historical data apis must have some type of import job that populates a database that supports the api, but lags behind real-time by an hour or so.

2 Likes

Thank you very much for your reply!!! Much appreciated! Agree on doc, not mentioned anywhere and official examples are provided on assumption of realtime data (near realtime). will try it but if it’s true about the lag, api is little worth for any real testing/use.

3 Likes

Is this a difference due to free market data? May be the paid market data subscription has near real time data access?

I do not know, could be related to free market data, but in this case how to test algorithms
based on alpaca API that imply near real-time market data availability?

1 Like

This has been frustrating me for weeks now. Was considering paying for the premium data but I think I’m going to find another data source if this is the level (read: lack) of community support we can expect from Alpaca. Polygon has its own problems but at least it works most of the time without issues like this.

Can you post your code, i can see if i can be of help. Make sure the the start date and end date are proper with the timeframe.

Here is some sample code to fetch the previous 1 hour of minute bars. One will get the error end is too late for subscription if not subscribed to the ‘Unlimited’ data plan (ie only using the ‘Free’ data plan). The Free data plan doesn’t provide access to the most recent 15 minutes of data. The error is saying the end datetime is too ‘late’. The fix is to set the end to earlier than 15 minutes ago. Something like this

# Import datetime and timezone packages
import datetime as dt
import pytz

# Import the TimeFrame class to use in 'get_bars'
from alpaca_trade_api.rest import TimeFrame

# Set a constant for UTC timezone
UTC = pytz.timezone('UTC')

# Get the current time, 15minutes, and 1 hour ago
time_now = dt.datetime.now(tz=UTC)
time_15_min_ago = time_now - dt.timedelta(minutes=15)
time_1_hr_ago = time_now - dt.timedelta(hours=1)

# Get data from previous hour
# If using the Free plan, the latest one can fetch is 15 minutes ago
data_df = api.get_bars('IBM', TimeFrame.Minute, 
             start=time_1_hr_ago.isoformat(), 
             end=time_15_min_ago.isoformat(), 
             adjustment='raw'
             ).df

# Convert to market time to make it easier to read
data_df.index = data_df.index.tz_convert('America/New_York')
data_df

The data_df dataframe will look like this

If one subscribes to the ‘Unlimited’ data plan then all bars can be accessed. Just replace time_15_min_ago with time_now.

Hope that helps.

2 Likes

Hi Dan,
Thank you for your reply. I will try it next week.
Perhaps error messages can be made more intuitive, as too late for subscription was not clear at all.

Thanks again, Dan.
15 minutes trick worked.

Hi Dan,

Thanks for the fantastic and detailed answer. I have a (perhaps silly) question:

# Get the current time, 15minutes, and 1 hour ago
time_now = dt.datetime.now(tz=UTC)
time_15_min_ago = time_now - dt.timedelta(minutes=15)
time_1_hr_ago = time_now - dt.timedelta(hours=1)

As a London-based trader, would I need to change the tz=UTC parameter to specify New York? Or would Alpaca automatically return the bars based on my computer’s local time?

EDIT: it seems that the datetimes are all stored/processed in UTC, so the above TZ conversions are really only for readability. Since my algo will be autonomous, I won’t need to see these times myself, so I guess it’s best to keep them in UTC.