Get_bars for opening hours only and following market timing

Hi

Is it possible to use ‘get_bars’ from the Python API to:

  • Only get e.g. hour data bars from the open market (normally 9.30 to 16.00) over several days, and skip the pre/post market data bars?
  • Get hour data bars following the market hour timing, i.e. {9.30-10.30, 10.30-11.30, …} instead of {9.00-10.00, 10.00-11.00, …}, since the latter would include some pre-market data?

Thanks.

/Steven

Hi, I would also like to know the answer to this - same problem!

seems totally dead in here

Agree that there is not much response.

I solved the problem myself by getting ½ hour bars, remove those outside opening hours and resample to 1 hour bars in alignment with the opening time.

But it would be nicer if the API could handle this.

/Steven

Hi,

yes I was trying to avoid doing this but looks like it’s going to have to be done :smiley:

thanks for replying (I also queried this on github, but no response from there either…)

Hi @saskaaning and @gm_scan - sorry for joining the party late. The API does not support this currently, but I will take this back to the team and review the possibilities.

An alternate solution in the meantime could be to query minute bars for the periods you want to use.

@saskaaning As @Abel_Alpaca noted, the way I would do this is to fetch minute bars and then use the magic of the pandas resample method to create hour bars. Something like this

# Get all the bars between a start and end time
start_time = pd.to_datetime('2021-12-20', utc=True)
end_time = pd.to_datetime('2021-12-23', utc=True)
symbols = ['IBM']

# Fetch the bars and format as a dataframe
minute_bars = api_data.get_bars(symbols, '1Minute', 
                             start=start_time.isoformat(), 
                             end=end_time.isoformat(), adjustment='split').df

# Convert to market time for easier reading
minute_bars = minute_bars.tz_convert('America/New_York')

# Resample the bars to get hourly bars for only market hours
agg_functions = {'open': 'first', 'high': 'max', 'low': 'min', 'close': 'last', 'volume': 'sum', 'trade_count': 'sum', }
hour_bars = minute_bars.resample('1H', offset='30T').agg(agg_functions).between_time('9:30', '16:00')

That creates the following hourly bars as desired.

2 Likes