Skip to content

Commit

Permalink
Add optional asof for historical marketdata endpoints (#632)
Browse files Browse the repository at this point in the history
  • Loading branch information
gnvk authored Jun 23, 2022
1 parent 536d007 commit 8077685
Showing 1 changed file with 36 additions and 5 deletions.
41 changes: 36 additions & 5 deletions alpaca_trade_api/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,7 @@ def _data_get(self,
resp_grouped_by_symbol: Optional[bool] = None,
page_limit: int = DATA_V2_MAX_LIMIT,
feed: Optional[str] = None,
asof: Optional[str] = None,
**kwargs):
page_token = None
total_items = 0
Expand All @@ -577,6 +578,8 @@ def _data_get(self,
path += f'/{symbol_or_symbols}'
else:
data['symbols'] = ','.join(symbol_or_symbols)
if asof:
data['asof'] = asof
if endpoint:
path += f'/{endpoint}'
resp = self.data_get(path, data=data, feed=feed,
Expand All @@ -603,9 +606,15 @@ def get_trades_iter(self,
end: Optional[str] = None,
limit: int = None,
feed: Optional[str] = None,
asof: Optional[str] = None,
raw=False) -> TradeIterator:
trades = self._data_get('trades', symbol,
start=start, end=end, limit=limit, feed=feed)
start=start,
end=end,
limit=limit,
feed=feed,
asof=asof,
)
for trade in trades:
if raw:
yield trade
Expand All @@ -618,9 +627,15 @@ def get_trades(self,
end: Optional[str] = None,
limit: int = None,
feed: Optional[str] = None,
asof: Optional[str] = None,
) -> TradesV2:
trades = list(self.get_trades_iter(symbol,
start, end, limit, feed, raw=True))
start=start,
end=end,
limit=limit,
feed=feed,
asof=asof,
raw=True))
return TradesV2(trades)

def get_quotes_iter(self,
Expand All @@ -629,9 +644,15 @@ def get_quotes_iter(self,
end: Optional[str] = None,
limit: int = None,
feed: Optional[str] = None,
asof: Optional[str] = None,
raw=False) -> QuoteIterator:
quotes = self._data_get('quotes', symbol,
start=start, end=end, limit=limit, feed=feed)
start=start,
end=end,
limit=limit,
feed=feed,
asof=asof,
)
for quote in quotes:
if raw:
yield quote
Expand All @@ -644,13 +665,16 @@ def get_quotes(self,
end: Optional[str] = None,
limit: int = None,
feed: Optional[str] = None,
asof: Optional[str] = None,
) -> QuotesV2:
quotes = list(self.get_quotes_iter(symbol=symbol,
start=start,
end=end,
limit=limit,
feed=feed,
raw=True))
raw=True,
asof=asof,
))
return QuotesV2(quotes)

def get_bars_iter(self,
Expand All @@ -661,11 +685,16 @@ def get_bars_iter(self,
adjustment: str = 'raw',
limit: int = None,
feed: Optional[str] = None,
asof: Optional[str] = None,
raw=False) -> BarIterator:
bars = self._data_get('bars', symbol,
timeframe=timeframe,
adjustment=adjustment,
start=start, end=end, limit=limit, feed=feed)
start=start,
end=end,
limit=limit,
feed=feed,
asof=asof)
for bar in bars:
if raw:
yield bar
Expand All @@ -680,6 +709,7 @@ def get_bars(self,
adjustment: str = 'raw',
limit: int = None,
feed: Optional[str] = None,
asof: Optional[str] = None,
) -> BarsV2:
bars = list(self.get_bars_iter(symbol,
timeframe,
Expand All @@ -688,6 +718,7 @@ def get_bars(self,
adjustment,
limit,
feed=feed,
asof=asof,
raw=True))
return BarsV2(bars)

Expand Down

0 comments on commit 8077685

Please sign in to comment.