Polygon historic_agg fails for 'day'


When I try to use:
api.polygon.historic_agg_v2('AAPL', multiplier =1, timespan="day", _from=start, to=(end))

I get the following error:
Traceback (most recent call last):
File “historicalTest.py”, line 46, in
df = api.polygon.historic_agg_v2(‘AAPL’, multiplier =1, timespan=“day”, _from=start, to=(end)).df
File “/Users/aky/Documents/Algo/venv/lib/python3.7/site-packages/alpaca_trade_api/polygon/rest.py”, line 204, in historic_agg_v2
to=fix_daily_bar_date(format_date_for_api_call(to), timespan)
File “/Users/aky/Documents/Algo/venv/lib/python3.7/site-packages/alpaca_trade_api/polygon/rest.py”, line 65, in fix_daily_bar_date
date = dateutil.parser.parse(date)
File “/Users/aky/Documents/Algo/venv/lib/python3.7/site-packages/dateutil/parser/_parser.py”, line 1374, in parse
return DEFAULTPARSER.parse(timestr, **kwargs)
File “/Users/aky/Documents/Algo/venv/lib/python3.7/site-packages/dateutil/parser/_parser.py”, line 646, in parse
res, skipped_tokens = self._parse(timestr, **kwargs)
File “/Users/aky/Documents/Algo/venv/lib/python3.7/site-packages/dateutil/parser/_parser.py”, line 725, in _parse
l = _timelex.split(timestr) # Splits the timestr into tokens
File “/Users/aky/Documents/Algo/venv/lib/python3.7/site-packages/dateutil/parser/_parser.py”, line 207, in split
return list(cls(s))
File “/Users/aky/Documents/Algo/venv/lib/python3.7/site-packages/dateutil/parser/_parser.py”, line 76, in init
TypeError: Parser must be a string or character stream, not int

The code works for any other timespan like minute, hour, month, week, etc. It just doesn’t work for day. Anyone know why?

It should work. Not exactly like yours but something like this works

# Set some arbitrary start and end dates and format as ISO strings
start_date = datetime.datetime(2020, 11, 11).isoformat()
end_date = datetime.datetime(2020, 11, 12).isoformat()

# Get minute and daily data for those dates
minute_data = api.polygon.historic_agg_v2('SPY', multiplier=15, timespan='minute',

daily_data = api.polygon.historic_agg_v2('SPY', multiplier=1, timespan='day',

# Display the results
display(minute_data, daily_data)

This displays two dataframes

What are your values for start and end? They should be strings. From the error it appears one or more may be integers?

I appreciate the quick response!

This code works for “minute” “hour”, “week”, and “month”, just not “day”

start = pytz.timezone(NY).localize(datetime(2020,1,1,9,30)).timestamp()*1000  # timestamp in micro seconds            
end = pytz.timezone(NY).localize(datetime(2020,12,30,16,0)).timestamp()*1000                    
df = api.polygon.historic_agg_v2('AAPL', multiplier =1, timespan="day", _from=start, to=(end)).df