Skip to content

Commit

Permalink
Add asof parameter to historical stock data requests (#477)
Browse files Browse the repository at this point in the history
  • Loading branch information
gnvk authored Jun 27, 2024
1 parent f2362cc commit 296ac38
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
12 changes: 12 additions & 0 deletions alpaca/data/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,13 @@ class StockBarsRequest(BaseBarsRequest):
adjustment (Optional[Adjustment]): The type of corporate action data normalization.
feed (Optional[DataFeed]): The stock data feed to retrieve from.
sort (Optional[Sort]): The chronological order of response based on the timestamp. Defaults to ASC.
asof (Optional[str]): The asof date of the queried stock symbol(s) in YYYY-MM-DD format.
currency (Optional[SupportedCurrencies]): The currency of all prices in ISO 4217 format. Default is USD.
"""

adjustment: Optional[Adjustment] = None
feed: Optional[DataFeed] = None
asof: Optional[str] = None


class CryptoBarsRequest(BaseBarsRequest):
Expand Down Expand Up @@ -155,9 +158,12 @@ class StockQuotesRequest(BaseTimeseriesDataRequest):
limit (Optional[int]): Upper limit of number of data points to return. Defaults to None.
feed (Optional[DataFeed]): The stock data feed to retrieve from.
sort (Optional[Sort]): The chronological order of response based on the timestamp. Defaults to ASC.
asof (Optional[str]): The asof date of the queried stock symbol(s) in YYYY-MM-DD format.
currency (Optional[SupportedCurrencies]): The currency of all prices in ISO 4217 format. Default is USD.
"""

feed: Optional[DataFeed] = None
asof: Optional[str] = None


# ############################## Trades ################################# #
Expand All @@ -176,9 +182,12 @@ class StockTradesRequest(BaseTimeseriesDataRequest):
limit (Optional[int]): Upper limit of number of data points to return. Defaults to None.
feed (Optional[DataFeed]): The stock data feed to retrieve from.
sort (Optional[Sort]): The chronological order of response based on the timestamp. Defaults to ASC.
asof (Optional[str]): The asof date of the queried stock symbol(s) in YYYY-MM-DD format.
currency (Optional[SupportedCurrencies]): The currency of all prices in ISO 4217 format. Default is USD.
"""

feed: Optional[DataFeed] = None
asof: Optional[str] = None


class CryptoTradesRequest(BaseTimeseriesDataRequest):
Expand Down Expand Up @@ -245,6 +254,7 @@ class StockLatestTradeRequest(BaseStockLatestDataRequest):
Attributes:
symbol_or_symbols (Union[str, List[str]]): The ticker identifier or list of ticker identifiers.
feed (Optional[DataFeed]): The stock data feed to retrieve from.
currency (Optional[SupportedCurrencies]): The currency the data should be returned in. Default to USD.
"""

pass
Expand All @@ -259,6 +269,7 @@ class StockLatestQuoteRequest(BaseStockLatestDataRequest):
Attributes:
symbol_or_symbols (Union[str, List[str]]): The ticker identifier or list of ticker identifiers.
feed (Optional[DataFeed]): The stock data feed to retrieve from.
currency (Optional[SupportedCurrencies]): The currency the data should be returned in. Default to USD.
"""

pass
Expand All @@ -273,6 +284,7 @@ class StockLatestBarRequest(BaseStockLatestDataRequest):
Attributes:
symbol_or_symbols (Union[str, List[str]]): The ticker identifier or list of ticker identifiers.
feed (Optional[DataFeed]): The stock data feed to retrieve from.
currency (Optional[SupportedCurrencies]): The currency the data should be returned in. Default to USD.
"""

pass
Expand Down
10 changes: 6 additions & 4 deletions tests/data/test_historical_stock_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ def test_multisymbol_quotes(reqmock, stock_client: StockHistoricalDataClient):
)

reqmock.get(
f"https://data.alpaca.markets/v2/stocks/quotes?start={_start_in_url}&symbols={_symbols_in_url}",
f"https://data.alpaca.markets/v2/stocks/quotes?start={_start_in_url}&symbols={_symbols_in_url}&asof=-",
text="""
{
"quotes": {
Expand Down Expand Up @@ -393,7 +393,7 @@ def test_multisymbol_quotes(reqmock, stock_client: StockHistoricalDataClient):
""",
)

request = StockQuotesRequest(symbol_or_symbols=symbols, start=start)
request = StockQuotesRequest(symbol_or_symbols=symbols, start=start, asof="-")

quoteset = stock_client.get_stock_quotes(request_params=request)

Expand Down Expand Up @@ -487,7 +487,7 @@ def test_get_trades(reqmock, stock_client: StockHistoricalDataClient):
)

reqmock.get(
f"https://data.alpaca.markets/v2/stocks/{symbol}/trades?start={_start_in_url}&limit={limit}",
f"https://data.alpaca.markets/v2/stocks/{symbol}/trades?start={_start_in_url}&limit={limit}&asof=2022-03-09",
text="""
{
"trades": [
Expand Down Expand Up @@ -524,7 +524,9 @@ def test_get_trades(reqmock, stock_client: StockHistoricalDataClient):
""",
)

request = StockTradesRequest(symbol_or_symbols=symbol, start=start, limit=limit)
request = StockTradesRequest(
symbol_or_symbols=symbol, start=start, limit=limit, asof="2022-03-09"
)

tradeset = stock_client.get_stock_trades(request_params=request)

Expand Down

0 comments on commit 296ac38

Please sign in to comment.