Skip to content

Commit ee7e26c

Browse files
committed
Fix error when aggregate result is empty
1 parent 9e6bd15 commit ee7e26c

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

alpaca_trade_api/polygon/entity.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,9 @@ def df(self):
8181

8282
class Aggsv2(list):
8383
def __init__(self, raw):
84+
results = raw['results'] if raw.get('results') else []
8485
super().__init__(
85-
Agg(tick) for tick in raw['results']
86+
[Agg(tick) for tick in results]
8687
)
8788
self._raw = raw
8889

tests/test_polygon/test_rest.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ def reqmock():
99
yield m
1010

1111

12-
def endpoint(path, params=''):
13-
return 'https://api.polygon.io/v1{}?apiKey=key-id&{}'.format(path, params)
12+
def endpoint(path, params='', api_version='v1'):
13+
return 'https://api.polygon.io/{}{}?{}&apiKey=key-id'.format(api_version, path, params)
1414

1515

1616
def test_polygon(reqmock):
@@ -216,6 +216,38 @@ def test_polygon(reqmock):
216216
assert len(aggs) == 1
217217
assert aggs.df.iloc[0].high == 173.21
218218

219+
# Historic Aggregates V2
220+
reqmock.get(
221+
endpoint('/aggs/ticker/AAPL/range/1/day/2018-2-2/2018-2-5',
222+
params='unadjusted=False', api_version='v2'),
223+
text='''
224+
{
225+
"ticker": "AAPL",
226+
"status": "OK",
227+
"adjusted": true,
228+
"queryCount": 55,
229+
"resultsCount": 2,
230+
"results": [
231+
{
232+
"o": 173.15,
233+
"c": 173.2,
234+
"l": 173.15,
235+
"h": 173.21,
236+
"v": 1800,
237+
"t": 1517529605000
238+
}
239+
]
240+
}''')
241+
242+
aggs = cli.historic_agg_v2('AAPL', 1, 'day',
243+
_from='2018-2-2',
244+
to='2018-2-5')
245+
assert aggs[0].o == 173.15
246+
assert len(aggs) == 1
247+
assert aggs.df.iloc[0].h == 173.21
248+
with pytest.raises(AttributeError):
249+
aggs[0].foo
250+
219251
# Last Trade
220252
reqmock.get(
221253
endpoint('/last/stocks/AAPL'),

0 commit comments

Comments
 (0)