What is wrong with get_bars between start and end?

df = alpaca.get_bars(
symbol=“AAPL”,
start=datetime. datetime(2013, 1, 12) - datetime.timedelta(days=10),
end=datetime. datetime(2013, 1, 12),
timeframe=TimeFrame.Minute,
adjustment=“raw”,
).df.between_time(“14:30”, “21:00”).tail(40)

$ python3 alpaca.py
Traceback (most recent call last):
File “/home/jzyprince/.local/lib/python3.9/site-packages/alpaca_trade_api/rest.py”, line 234, in _one_request
resp.raise_for_status()
File “/home/jzyprince/.local/lib/python3.9/site-packages/requests/models.py”, line 1021, in raise_for_status
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 422 Client Error: Unprocessable Entity for url: https://data.alpaca.markets/v2/stocks/AAPL/bars?timeframe=1Min&adjustment=raw&start=2013-01-02+00%3A00%3A00&end=2013-01-12+00%3A00%3A00

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “/home/jzyprince/abc/examples/alpaca.py”, line 15, in
df = alpaca.get_bars(
File “/home/jzyprince/.local/lib/python3.9/site-packages/alpaca_trade_api/rest.py”, line 714, in get_bars
bars = list(self.get_bars_iter(symbol,
File “/home/jzyprince/.local/lib/python3.9/site-packages/alpaca_trade_api/rest.py”, line 698, in get_bars_iter
for bar in bars:
File “/home/jzyprince/.local/lib/python3.9/site-packages/alpaca_trade_api/rest.py”, line 585, in _data_get
resp = self.data_get(path, data=data, feed=feed,
File “/home/jzyprince/.local/lib/python3.9/site-packages/alpaca_trade_api/rest.py”, line 270, in data_get
return self._request(
File “/home/jzyprince/.local/lib/python3.9/site-packages/alpaca_trade_api/rest.py”, line 213, in _request
return self._one_request(method, url, opts, retry)
File “/home/jzyprince/.local/lib/python3.9/site-packages/alpaca_trade_api/rest.py”, line 242, in _one_request
raise APIError(error, http_error)
alpaca_trade_api.rest.APIError: invalid start

@sheep There are three issues with your get_bars request.

  1. the start and end parameters must be timezone aware.
  2. the start and end parameters must be strings in ISO format
  3. the earliest date which Alpaca has data for is 2016-01-01

So, one way to do what you want could be this

import pytz

df = api_data.get_bars(symbol='AAPL',
                       start=(datetime.datetime(2016, 1, 12, tzinfo=pytz.timezone('America/New_York')) - datetime.timedelta(days=10)).isoformat(),
                       end=datetime.datetime(2016, 1, 12, tzinfo=pytz.timezone('America/New_York')).isoformat(),
                       timeframe=TimeFrame.Minute,
                       adjustment='raw',
                       ).df.between_time('14:30', '21:00').tail(40)

Note however that returned data is always in UTC timezone (even if the input was in another timezone). Therefore, between_time('14:30', '21:00') will be between those UTC times. Be careful doing this because daylight savings time will offset the UTC time from market time by an hour during parts of the year. It’s best to covert to market timezone first then use between_time.