Skip to content

Commit 6b8a26e

Browse files
authored
Merge pull request #173 from shlomikushchi/historic_agg_v2
Use v2 by default for historic_agg_v2
2 parents ffec0b8 + 703e9a6 commit 6b8a26e

File tree

3 files changed

+44
-103
lines changed

3 files changed

+44
-103
lines changed

alpaca_trade_api/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
from .rest import REST # noqa
22
from .stream2 import StreamConn # noqa
33

4-
__version__ = '0.46'
4+
__version__ = '0.47'

alpaca_trade_api/polygon/rest.py

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import dateutil
12
import requests
23
from .entity import (
34
Aggs, Aggsv2, Aggsv2Set,
@@ -59,8 +60,8 @@ def historic_trades(self, symbol, date, offset=None, limit=None):
5960
return Trades(raw)
6061

6162
def historic_trades_v2(
62-
self, symbol, date, timestamp=None, timestamp_limit=None,
63-
reverse=None, limit=None
63+
self, symbol, date, timestamp=None, timestamp_limit=None,
64+
reverse=None, limit=None
6465
):
6566
path = '/ticks/stocks/trades/{}/{}'.format(symbol, date)
6667
params = {}
@@ -92,8 +93,8 @@ def historic_quotes(self, symbol, date, offset=None, limit=None):
9293
return Quotes(raw)
9394

9495
def historic_quotes_v2(
95-
self, symbol, date, timestamp=None, timestamp_limit=None,
96-
reverse=None, limit=None
96+
self, symbol, date, timestamp=None, timestamp_limit=None,
97+
reverse=None, limit=None
9798
):
9899
path = '/ticks/stocks/nbbo/{}/{}'.format(symbol, date)
99100
params = {}
@@ -109,27 +110,46 @@ def historic_quotes_v2(
109110

110111
return QuotesV2(raw)
111112

112-
def historic_agg(self, size, symbol,
113-
_from=None, to=None, limit=None):
114-
path = '/historic/agg/{}/{}'.format(size, symbol)
115-
params = {}
116-
if _from is not None:
117-
params['from'] = _from
118-
if to is not None:
119-
params['to'] = to
120-
if limit is not None:
121-
params['limit'] = limit
122-
raw = self.get(path, params)
123-
124-
return Aggs(raw)
125-
126113
def historic_agg_v2(self, symbol, multiplier, timespan, _from, to,
127114
unadjusted=False, limit=None):
128-
path = '/aggs/ticker/{}/range/{}/{}/{}/{}'.format(
129-
symbol, multiplier, timespan, _from, to
130-
)
131-
params = {}
132-
params['unadjusted'] = unadjusted
115+
"""
116+
117+
:param symbol:
118+
:param multiplier: Size of the timespan multiplier (distance between
119+
samples. e.g if it's 1 we get for daily 2015-01-05, 2015-01-06,
120+
2015-01-07, 2015-01-08.
121+
if it's 3 we get 2015-01-01, 2015-01-04,
122+
2015-01-07, 2015-01-10)
123+
:param timespan: Size of the time window: minute, hour, day, week,
124+
month, quarter, year
125+
:param _from: some use isoformat some use timestamp. for now we
126+
handle both.
127+
examples of different usages: pylivetrader,
128+
alpaca-backtrader.
129+
:param to:
130+
:param unadjusted:
131+
:param limit: max samples to retrieve (seems like we get "limit - 1" )
132+
:return:
133+
"""
134+
path_template = '/aggs/ticker/{symbol}/range/{multiplier}/' \
135+
'{timespan}/{_from}/{to}'
136+
if isinstance(_from, int):
137+
path = path_template.format(symbol=symbol,
138+
multiplier=multiplier,
139+
timespan=timespan,
140+
_from=_from,
141+
to=to
142+
)
143+
else:
144+
path = path_template.format(symbol=symbol,
145+
multiplier=multiplier,
146+
timespan=timespan,
147+
_from=dateutil.parser.parse(
148+
_from).date().isoformat(),
149+
to=dateutil.parser.parse(
150+
to).date().isoformat()
151+
)
152+
params = {'unadjusted': unadjusted}
133153
if limit:
134154
params['limit'] = limit
135155
raw = self.get(path, params, version='v2')

tests/test_polygon/test_rest.py

Lines changed: 0 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -139,85 +139,6 @@ def test_polygon(reqmock):
139139
with pytest.raises(AttributeError):
140140
quotes[0].foo
141141

142-
# Historic Aggregates
143-
reqmock.get(
144-
endpoint('/historic/agg/minute/AAPL') +
145-
'&from=2018-2-2&to=2018-2-5&limit=100',
146-
text='''
147-
{
148-
"map": {
149-
"a": "average",
150-
"c": "close",
151-
"h": "high",
152-
"l": "low",
153-
"o": "open",
154-
"t": "timestamp",
155-
"v": "volume"
156-
},
157-
"status": "success",
158-
"aggType": "min",
159-
"symbol": "AAPL",
160-
"ticks": [
161-
{
162-
"o": 173.15,
163-
"c": 173.2,
164-
"l": 173.15,
165-
"h": 173.21,
166-
"v": 1800,
167-
"t": 1517529605000
168-
}
169-
]
170-
}''')
171-
172-
aggs = cli.historic_agg('minute', 'AAPL',
173-
_from='2018-2-2',
174-
to='2018-2-5',
175-
limit=100)
176-
assert aggs[0].open == 173.15
177-
assert aggs[0].timestamp.day == 1
178-
assert len(aggs) == 1
179-
assert aggs.df.iloc[0].high == 173.21
180-
with pytest.raises(AttributeError):
181-
aggs[0].foo
182-
183-
reqmock.get(
184-
endpoint('/historic/agg/day/AAPL') +
185-
'&from=2018-2-2&to=2018-2-5&limit=100',
186-
text='''
187-
{
188-
"map": {
189-
"a": "average",
190-
"c": "close",
191-
"h": "high",
192-
"l": "low",
193-
"o": "open",
194-
"d": "day",
195-
"v": "volume"
196-
},
197-
"status": "success",
198-
"aggType": "day",
199-
"symbol": "AAPL",
200-
"ticks": [
201-
{
202-
"o": 173.15,
203-
"c": 173.2,
204-
"l": 173.15,
205-
"h": 173.21,
206-
"v": 1800,
207-
"d": "2018-02-02"
208-
}
209-
]
210-
}''')
211-
212-
aggs = cli.historic_agg('day', 'AAPL',
213-
_from='2018-2-2',
214-
to='2018-2-5',
215-
limit=100)
216-
assert aggs[0].open == 173.15
217-
assert aggs[0].day.day == 2
218-
assert len(aggs) == 1
219-
assert aggs.df.iloc[0].high == 173.21
220-
221142
# Historic Aggregates V2
222143
reqmock.get(
223144
endpoint(

0 commit comments

Comments
 (0)