From db294f14c8cddc87f0e351235a2b0aeea995c297 Mon Sep 17 00:00:00 2001 From: Gergely Novak Date: Wed, 1 Jun 2022 10:58:11 +0200 Subject: [PATCH] Add optional feed argument to the market data methods --- alpaca_trade_api/rest.py | 65 +++++++++++++++++++++++++++++----------- 1 file changed, 47 insertions(+), 18 deletions(-) diff --git a/alpaca_trade_api/rest.py b/alpaca_trade_api/rest.py index 0d7b6048..ba13455b 100644 --- a/alpaca_trade_api/rest.py +++ b/alpaca_trade_api/rest.py @@ -261,8 +261,12 @@ def patch(self, path, data=None): def delete(self, path, data=None): return self._request('DELETE', path, data) - def data_get(self, path, data=None, api_version='v1'): + def data_get(self, path, data=None, + feed: Optional[str] = None, api_version='v1'): base_url: URL = get_data_url() + if feed: + data = data or {} + data['feed'] = feed return self._request( 'GET', path, data, base_url=base_url, api_version=api_version, ) @@ -552,6 +556,7 @@ def _data_get(self, endpoint_base: str = 'stocks', resp_grouped_by_symbol: Optional[bool] = None, page_limit: int = DATA_V2_MAX_LIMIT, + feed: Optional[str] = None, **kwargs): page_token = None total_items = 0 @@ -574,7 +579,8 @@ def _data_get(self, data['symbols'] = ','.join(symbol_or_symbols) if endpoint: path += f'/{endpoint}' - resp = self.data_get(path, data=data, api_version=api_version) + resp = self.data_get(path, data=data, feed=feed, + api_version=api_version) if not resp_grouped_by_symbol: k = endpoint or endpoint_base for item in resp.get(k, []) or []: @@ -596,9 +602,10 @@ def get_trades_iter(self, start: Optional[str] = None, end: Optional[str] = None, limit: int = None, + feed: Optional[str] = None, raw=False) -> TradeIterator: trades = self._data_get('trades', symbol, - start=start, end=end, limit=limit) + start=start, end=end, limit=limit, feed=feed) for trade in trades: if raw: yield trade @@ -610,9 +617,10 @@ def get_trades(self, start: Optional[str] = None, end: Optional[str] = None, limit: int = None, + feed: Optional[str] = None, ) -> TradesV2: trades = list(self.get_trades_iter(symbol, - start, end, limit, raw=True)) + start, end, limit, feed, raw=True)) return TradesV2(trades) def get_quotes_iter(self, @@ -620,9 +628,10 @@ def get_quotes_iter(self, start: Optional[str] = None, end: Optional[str] = None, limit: int = None, + feed: Optional[str] = None, raw=False) -> QuoteIterator: quotes = self._data_get('quotes', symbol, - start=start, end=end, limit=limit) + start=start, end=end, limit=limit, feed=feed) for quote in quotes: if raw: yield quote @@ -634,11 +643,13 @@ def get_quotes(self, start: Optional[str] = None, end: Optional[str] = None, limit: int = None, + feed: Optional[str] = None, ) -> QuotesV2: - quotes = list(self.get_quotes_iter(symbol, - start, - end, - limit, + quotes = list(self.get_quotes_iter(symbol=symbol, + start=start, + end=end, + limit=limit, + feed=feed, raw=True)) return QuotesV2(quotes) @@ -649,11 +660,12 @@ def get_bars_iter(self, end: Optional[str] = None, adjustment: str = 'raw', limit: int = None, + feed: Optional[str] = None, raw=False) -> BarIterator: bars = self._data_get('bars', symbol, timeframe=timeframe, adjustment=adjustment, - start=start, end=end, limit=limit) + start=start, end=end, limit=limit, feed=feed) for bar in bars: if raw: yield bar @@ -667,6 +679,7 @@ def get_bars(self, end: Optional[str] = None, adjustment: str = 'raw', limit: int = None, + feed: Optional[str] = None, ) -> BarsV2: bars = list(self.get_bars_iter(symbol, timeframe, @@ -674,53 +687,69 @@ def get_bars(self, end, adjustment, limit, + feed=feed, raw=True)) return BarsV2(bars) - def get_latest_bar(self, symbol: str) -> BarV2: + def get_latest_bar(self, symbol: str, feed: Optional[str] = None) -> BarV2: resp = self.data_get( '/stocks/{}/bars/latest'.format(symbol), + feed=feed, api_version='v2') return self.response_wrapper(resp['bar'], BarV2) - def get_latest_bars(self, symbols: List[str]) -> LatestBarsV2: + def get_latest_bars(self, symbols: List[str], + feed: Optional[str] = None) -> LatestBarsV2: resp = self.data_get( f'/stocks/bars/latest?symbols={_join_with_commas(symbols)}', + feed=feed, api_version='v2') return self.response_wrapper(resp['bars'], LatestBarsV2) - def get_latest_trade(self, symbol: str) -> TradeV2: + def get_latest_trade(self, symbol: str, + feed: Optional[str] = None) -> TradeV2: resp = self.data_get( '/stocks/{}/trades/latest'.format(symbol), + feed=feed, api_version='v2') return self.response_wrapper(resp['trade'], TradeV2) - def get_latest_trades(self, symbols: List[str]) -> LatestTradesV2: + def get_latest_trades(self, symbols: List[str], + feed: Optional[str] = None) -> LatestTradesV2: resp = self.data_get( f'/stocks/trades/latest?symbols={_join_with_commas(symbols)}', + feed=feed, api_version='v2') return self.response_wrapper(resp['trades'], LatestTradesV2) - def get_latest_quote(self, symbol: str) -> QuoteV2: + def get_latest_quote(self, symbol: str, + feed: Optional[str] = None) -> QuoteV2: resp = self.data_get( '/stocks/{}/quotes/latest'.format(symbol), + feed=feed, api_version='v2') return self.response_wrapper(resp['quote'], QuoteV2) - def get_latest_quotes(self, symbols: List[str]) -> LatestQuotesV2: + def get_latest_quotes(self, symbols: List[str], + feed: Optional[str] = None) -> LatestQuotesV2: resp = self.data_get( f'/stocks/quotes/latest?symbols={_join_with_commas(symbols)}', + feed=feed, api_version='v2') return self.response_wrapper(resp['quotes'], LatestQuotesV2) - def get_snapshot(self, symbol: str) -> SnapshotV2: + def get_snapshot(self, symbol: str, + feed: Optional[str] = None) -> SnapshotV2: resp = self.data_get('/stocks/{}/snapshot'.format(symbol), + feed=feed, api_version='v2') return self.response_wrapper(resp, SnapshotV2) - def get_snapshots(self, symbols: List[str]) -> SnapshotsV2: + def get_snapshots(self, symbols: List[str], + feed: Optional[str] = None) -> SnapshotsV2: resp = self.data_get( '/stocks/snapshots?symbols={}'.format(_join_with_commas(symbols)), + feed=feed, api_version='v2') return self.response_wrapper(resp, SnapshotsV2)