Skip to content

Commit e656368

Browse files
authored
Merge pull request #71 from alpacahq/nil-v2-agg-fix
Fix error when aggregate result is empty
2 parents 9e6bd15 + dfb869f commit e656368

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-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: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ 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(
14+
api_version, path, params
15+
)
1416

1517

1618
def test_polygon(reqmock):
@@ -216,6 +218,42 @@ def test_polygon(reqmock):
216218
assert len(aggs) == 1
217219
assert aggs.df.iloc[0].high == 173.21
218220

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

0 commit comments

Comments
 (0)