Help with alpaca polygon api

Environment

Language
Python 3.7

Alpaca SDK Version
Latest

Other Environment Details
Using historic_agg_v2 polygon function to get aggregate data

Problem

Summary
I am new to alpaca. My usage of historic_agg_v2 is returning a None object. Trying to get hourly, minutes, or day polygon aggregate data. I am using agg_v2 since it return historic data for minutes, but this doesn’t seem to work with my usage. I also tried passing limit =10 and instead of ‘day’ using ‘hour’, either didn’t seem to work. Would greatly appreciate help here. Thanks

Paper or Live Trading?
Live Trading account

Example Code

ENDPOINT ='https://api.alpaca.markets'
api = alpaca.REST(KEYID, SECRETKEY, ENDPOINT)
api. api.polygon.historic_agg_v2('AAPL', 1, 'day', 2019-10-28, 2019-10-29, False)```

TypeError: 'NoneType' object is not iterable
1 Like

Looks like the dates you pass are not enclosed in quotes. Best to use a date(2019, 10, 28) format. I suggest explicitly use parameter keys _from=…, to=… and then increment the date by 1 or 2 days to check.

I played with both v1 and v2 - both seem to work for your purpose with some quirks. In v1 call, you can call for only 1 minute or 1 day bars; in v2 you can specify multiples of these periods (eg 5 minutes, 60 minutes, 5 days). In v1 you can specify a datetime object in _from= and to= and the time part will be taken account in filtering (for example you can request regular market hours only); in v2 you cannot filter by time, results will sweep for before and after hours.

Hope this helps.

1 Like

Thanks Trader_Joe.
I just wanted to highlight the solution that I used to get the dataframe, (has been long due!!)
df = api.polygon.historic_agg_v2(‘AAPL’, 1, ‘day’, ‘2014-10-28’,str(datetime.date.today()), False,limit=10).df
This returns a dataframe df with last 10 transactions.
One can set any dates and time frames as Trader_Joe mentioned in his reply.