Get_barset ignore start and end dates

The API “get_barset” appears to ignore the start and end dates…
Dose anyone know how to fix this?

data = api.get_barset([‘AAPL’], ‘15Min’, start=‘2020-09-24 11:45-04:00’, end=‘2020-09-24 14:45-04:00’)
df = data[‘AAPL’].df
open high low close volume
2020-10-07 11:30:00-04:00 115.080 115.150 114.730 114.81 105990
2020-10-07 11:45:00-04:00 114.810 114.900 114.670 114.68 87752
2020-10-07 12:00:00-04:00 114.700 114.935 114.630 114.91 65361
2020-10-07 12:15:00-04:00 114.900 114.960 114.140 114.47 117596

The short answer is the ‘start’ and ‘end’ date formats are particular about being in ISO format - and I mean VERY particular. In the example above, the separator ‘T’ was omitted along with the seconds specification. Instead of the start date being start=‘2020-09-24 11:45-04:00’ it should be formatted start=‘2020-09-24T11:45:00-04:00’ (notice the ‘T’ and the ‘:00’ for seconds). So, the following will give the expected result

    data = api.get_barset('AAPL', 

Now a little longer explanation. The get_barset method unfortunately fails silently. If it encounters an error with either the ‘start’ or ‘end’ parameter (or ‘after’ and ‘until’), it simply uses the default datetimes, doesn’t tell anyone, and goes on its merry way. The default ‘start’ is None and the default ‘end’ is the current datetime. One other peculiarity with the get_barset method is that it begins with the ‘end’ datetime and works backwards. It counts the number of non-empty bars (note this may not be the same as elapsed bars if a stock didn’t trade during a bar). The method then truncates the results when either a) the number of bars specified by the ‘limit’ parameter is reached, or b) the datetime specified by the ‘start’ parameter is reached.

Now, to complicate things, the timezone offset from UTC is specified in the ISO format. The market time offset varies depending on whether it’s daylight saving time or not. It’s sometimes -04 and sometimes -05. For example, in January the market opens at 2020-01-02T09:30:00-05:00 but in October it opens at 2020-10-02T09:30:00-04:00.

Here’s what I do to ensure the ISO format and the timezone offset are correct. There are probably other (better) ways but this works.

import pytz
import datetime

market_timezone = pytz.timezone('America/New_York')

start_dt = datetime.datetime(2020, 1, 24, 11, 45)
start = market_timezone.localize(start_dt).isoformat()

end_dt = datetime.datetime(2020, 1, 24, 14, 45)
end = market_timezone.localize(end_dt).isoformat()

Don’t be tempted to do this

start_dt = datetime.datetime(2020, 1, 24, 11, 45, tzinfo=market_timezone)

For some rather esoteric reasons (in my mind a bug) adding the timezone info will look like it works but won’t always return the correct datatime.

Using the above values for ‘start’ and ‘end’ get_barset will return the expected data

data = api.get_barset('AAPL', 

And of course, it’s not entirely that simple. The ‘end’ datetime will always be the last bar returned. However, since the results are limited by the ‘limit’ parameter, the first bar will be either the ‘start’ datetime or potentially sometime after that.

Hope that helps. (edited)

thanks Dan.

I tried different variation of dates, they all failed in the same way.
The way I solved the issue (for my application) is to use the “get_barset” only for the lates few days
and use “polygon.historic_agg_v2” with

‘multiplier’: 15,
‘timespan’: ‘minute’,

I will try your suggestion as well…

Yes, it does work