# Get\_barset ignore start and end dates

**URL:** https://forum.alpaca.markets/t/get-barset-ignore-start-and-end-dates/2961
**Category:** Alpaca Integration Applications
**Created:** [October 11, 2020, 1:28am UTC](https://forum.alpaca.markets/t/get-barset-ignore-start-and-end-dates/2961 "2020-10-11T01:28:58Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![youdar](https://avatars.discourse-cdn.com/v4/letter/y/41988e/32.png) [@youdar](https://forum.alpaca.markets/u/youdar)
#### Post date: [October 11, 2020, 1:28am UTC](https://forum.alpaca.markets/t/get-barset-ignore-start-and-end-dates/2961/1 "2020-10-11T01:28:58Z")

</div>

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  
> df.head()  
> Out[5]:  
> open high low close volume  
> time  
> 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

---

<div class="post-metadata">

### Author: ![dan\_whitnable](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/dan_whitnable/32/1323_2.png) [@dan\_whitnable](https://forum.alpaca.markets/u/dan_whitnable)
#### Post date: [October 11, 2020, 3:33pm UTC](https://forum.alpaca.markets/t/get-barset-ignore-start-and-end-dates/2961/2 "2020-10-11T15:33:04Z")

</div>

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', 
                  '15Min', 
                  start='2020-10-09T16:45:00-04:00', 
                  end='2020-09-24T14:45:00-04:00',
                 )

```

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', 
                  '15Min', 
                  start=start, 
                  end=end,
                 )

```

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)

---

<div class="post-metadata">

### Author: ![youdar](https://avatars.discourse-cdn.com/v4/letter/y/41988e/32.png) [@youdar](https://forum.alpaca.markets/u/youdar)
#### Post date: [October 12, 2020, 5:39am UTC](https://forum.alpaca.markets/t/get-barset-ignore-start-and-end-dates/2961/3 "2020-10-12T05:39:45Z")

</div>

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’,

---

<div class="post-metadata">

### Author: ![youdar](https://avatars.discourse-cdn.com/v4/letter/y/41988e/32.png) [@youdar](https://forum.alpaca.markets/u/youdar)
#### Post date: [October 12, 2020, 5:40am UTC](https://forum.alpaca.markets/t/get-barset-ignore-start-and-end-dates/2961/4 "2020-10-12T05:40:23Z")

</div>

I will try your suggestion as well…

Yes, it does work
