-
Notifications
You must be signed in to change notification settings - Fork 202
feat: Add auction data handling to stock historical data client #576
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
1712bdd
0d688ad
356bc67
4b14fd2
fd114da
767823f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -10,7 +10,7 @@ | |||||||||||||||||||||
from alpaca.data.historical.utils import ( | ||||||||||||||||||||||
parse_obj_as_symbol_dict, | ||||||||||||||||||||||
) | ||||||||||||||||||||||
from alpaca.data.models import BarSet, QuoteSet, TradeSet | ||||||||||||||||||||||
from alpaca.data.models import BarSet, QuoteSet, TradeSet, AuctionSet | ||||||||||||||||||||||
from alpaca.data.requests import ( | ||||||||||||||||||||||
StockBarsRequest, | ||||||||||||||||||||||
StockLatestBarRequest, | ||||||||||||||||||||||
|
@@ -19,6 +19,7 @@ | |||||||||||||||||||||
StockQuotesRequest, | ||||||||||||||||||||||
StockSnapshotRequest, | ||||||||||||||||||||||
StockTradesRequest, | ||||||||||||||||||||||
StockAuctionsRequest, | ||||||||||||||||||||||
) | ||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
|
@@ -72,6 +73,25 @@ def __init__( | |||||||||||||||||||||
raw_data=raw_data, | ||||||||||||||||||||||
) | ||||||||||||||||||||||
|
||||||||||||||||||||||
def get_stock_auction(self, request_params: StockAuctionsRequest): | ||||||||||||||||||||||
"""Returns auction data for an equity or list of equities over a given time period. | ||||||||||||||||||||||
|
||||||||||||||||||||||
Args: | ||||||||||||||||||||||
request_params (StockAuctionsRequest): The request object for retrieving stock auction data. | ||||||||||||||||||||||
|
||||||||||||||||||||||
Returns: | ||||||||||||||||||||||
Union[AuctionSet, RawData]: The auction data either in raw or wrapped form | ||||||||||||||||||||||
""" | ||||||||||||||||||||||
raw_auctions = self._get_marketdata( | ||||||||||||||||||||||
path="/stocks/auctions", | ||||||||||||||||||||||
params=request_params.to_request_fields(), | ||||||||||||||||||||||
) | ||||||||||||||||||||||
Comment on lines
+85
to
+88
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have recently introduced *1 #583
Suggested change
|
||||||||||||||||||||||
|
||||||||||||||||||||||
if self._use_raw_data: | ||||||||||||||||||||||
return raw_auctions | ||||||||||||||||||||||
|
||||||||||||||||||||||
return AuctionSet(raw_auctions) | ||||||||||||||||||||||
|
||||||||||||||||||||||
def get_stock_bars( | ||||||||||||||||||||||
self, request_params: StockBarsRequest | ||||||||||||||||||||||
) -> Union[BarSet, RawData]: | ||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
from datetime import datetime | ||
from typing import Dict, List, Optional | ||
|
||
from alpaca.common.models import ValidateBaseModel as BaseModel | ||
from alpaca.common.types import RawData | ||
from alpaca.data.mappings import AUCTION_MAPPING | ||
from alpaca.data.models.base import BaseDataSet, TimeSeriesMixin | ||
|
||
|
||
class Auction(BaseModel): | ||
"""Represents one auction of aggregated trade data over a specified interval. | ||
|
||
Attributes: | ||
symbol (str): The ticker identifier for the security whose data forms the bar. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could you please add other attribuites to docstring? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. all done |
||
|
||
""" | ||
|
||
symbol: str | ||
timestamp: datetime | ||
condition: str | ||
price: float | ||
size: float | ||
exchange: str | ||
|
||
def __init__(self, symbol: str, raw_data: RawData) -> None: | ||
"""Instantiates an auction | ||
|
||
Args: | ||
raw_data (RawData): Raw unparsed auction data from API, contains ohlc and other fields. | ||
""" | ||
mapped_auction = {} | ||
|
||
if raw_data is not None: | ||
mapped_auction = { | ||
AUCTION_MAPPING[key]: val | ||
for key, val in raw_data.items() | ||
if key in AUCTION_MAPPING | ||
} | ||
|
||
super().__init__(symbol=symbol, **mapped_auction) | ||
|
||
|
||
class AuctionSet(BaseDataSet, TimeSeriesMixin): | ||
"""A collection of Auctions. | ||
|
||
Attributes: | ||
data (Dict[str, List[Auction]]): The collection of Auctions keyed by symbol. | ||
""" | ||
|
||
data: Dict[str, List[Auction]] = {} | ||
|
||
def __init__(self, raw_data: RawData) -> None: | ||
"""A collection of Auctions. | ||
|
||
Args: | ||
raw_data (RawData): The collection of raw auction data from API keyed by Symbol. | ||
""" | ||
|
||
parsed_auctions = {} | ||
|
||
raw_auctions = raw_data | ||
|
||
if raw_auctions is not None: | ||
for symbol, auctions in raw_auctions.items(): | ||
|
||
auction_data = [] | ||
for auction in auctions: | ||
c = auction.get("c") | ||
o = auction.get("o") | ||
|
||
if c is not None: | ||
auction_data.extend(c) | ||
if o is not None: | ||
auction_data.extend(o) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. discussion: I felt it might be good to have indicator or separation between open and close. even though it might be possible to know based on timestamp. prefer to put something easy to distinguish. |
||
|
||
parsed_auctions[symbol] = [ | ||
Auction(symbol, auction) | ||
for auction in auction_data | ||
if auction is not None | ||
] | ||
|
||
super().__init__(data=parsed_auctions) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,6 +60,14 @@ def __init__(self, **data: Any) -> None: | |
super().__init__(**data) | ||
|
||
|
||
# ############################## Auctions ################################# # | ||
|
||
|
||
class StockAuctionsRequest(BaseTimeseriesDataRequest): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you please write docstring and also add to api_reference? |
||
feed: Optional[DataFeed] = None | ||
asof: Optional[str] = None | ||
|
||
|
||
# ############################## Bars ################################# # | ||
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we use plural?