From 3151b2ee8963e50690dff9902f453a1c957c69fc Mon Sep 17 00:00:00 2001 From: Jon Michael Aanes Date: Thu, 14 Nov 2024 22:31:49 +0100 Subject: [PATCH] from_polygon_id now takes optional exchange, which defaults to NYSE. --- fin_defs/__init__.py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/fin_defs/__init__.py b/fin_defs/__init__.py index 1217ef1..98afaba 100644 --- a/fin_defs/__init__.py +++ b/fin_defs/__init__.py @@ -83,6 +83,14 @@ class StockExchange: eod_id: str | None = None +NYSE = StockExchange( + name='New York Stock Exchange', + mic='XNYS', + bloomberg_id='UN', + crunchbase_id='NYSE', +) + + @enforce_typing.enforce_types @dataclasses.dataclass(frozen=True) class Asset: @@ -117,7 +125,7 @@ class Asset: raise TypeError(msg) @staticmethod - def from_polygon_id(polygon_id: str) -> 'Asset': + def from_polygon_id(polygon_id: str, exchange: StockExchange = NYSE) -> 'Asset': """Parses an asset from the [`polygon.io`](https://polygon.io) identifier format. @@ -132,7 +140,7 @@ class Asset: ) if polygon_id.startswith('C:'): return FiatCurrency(polygon_id.removeprefix('C:').removesuffix('USD')) - return Stock.from_polygon_id(polygon_id) + return Stock.from_polygon_id(polygon_id, exchange) @abc.abstractmethod def raw_short_name(self) -> str: @@ -282,8 +290,8 @@ class Stock(Asset): raise ValueError(msg) @staticmethod - def from_polygon_id(polygon_id: str) -> 'Stock': - return Stock(polygon_id, NYSE) # TODO: Add support for non-NYSE exchanges. + def from_polygon_id(polygon_id: str, exchange: StockExchange = NYSE) -> 'Stock': + return Stock(polygon_id, exchange) def raw_short_name(self) -> str: return self.ticker @@ -404,13 +412,6 @@ CURRENCY_SYMBOLS: dict[Currency, str] = { ASSET_PREFIX: dict[Asset, str] = CURRENCY_SYMBOLS # TODO: Remove at some point. -NYSE = StockExchange( - name='New York Stock Exchange', - mic='XNYS', - bloomberg_id='UN', - crunchbase_id='NYSE', -) - EXCHANGES = [ NYSE, StockExchange(name='Nasdaq', mic='XNAS', bloomberg_id='US', crunchbase_id='NASDAQ'),