Candles do not fully respect start and end parameters in historical bars endpoint?

Hey, I have a question.

I would like to query candlestick data of some NYSE stocks where trading hours are between 9:30 and 16:00 EST.

For example I use the following query:
https://data.alpaca.markets/v2/stocks/HLF/bars?timeframe=4H&start=2024-04-15T09:30:00-04:00&end=2024-07-14T16:00:00-04:00&limit=5000&adjustment=raw&feed=sip&sort=asc

Please observe the parameters I set:

  • Timeframe: 4H - I would assume that since trading hours at NYSE are 6h 30m long, I would get two candles, but I receive 3
  • Start date is with ISO 8601 / RFC-3339 format which is required by the Historical bars (single symbol) endpoint
  • Same with end date

Which query will return in most cases 3 bars for a single day, none of which directly maps to 9:30-13:30 or 13:30 to 16:00 (or 16:30).

Can you please explain what I am doing wrong? I do not believe this to be a simple timezone question, but I think I have a fundamental misunderstanding in using the API.

Thanks

Hi :wave:

The issue here is that 4H bars are constructed differently from what you expect. You’d like two bars: 9:30 - 13:30 and 13:30 - 16:00, however, 4H bars are simply 4 hour bars grouped together, always starting at 4 * n hour (00:00, 04:00, 08:00, etc.). You can see this in the actual response.

To get what you want you need to query for 30Min bars between 9:30 and 16:00, and aggregate them yourself.

The other thing is that the response timezone is always UTC. So the first bar’s 08:00 UTC is actually 04:00 ET (04:00-04:00). When you’ll query for 9:30-04:00, the first bar’s timestamp will be 13:30Z (Z = UTC).

1 Like