1
0

from_polygon_id now takes optional exchange, which defaults to NYSE.
All checks were successful
Python Ruff Code Quality / ruff (push) Successful in 23s
Run Python tests (through Pytest) / Test (push) Successful in 26s
Verify Python project can be installed, loaded and have version checked / Test (push) Successful in 22s

This commit is contained in:
Jon Michael Aanes 2024-11-14 22:31:49 +01:00
parent f5875c389c
commit 3151b2ee89
Signed by: Jmaa
SSH Key Fingerprint: SHA256:Ab0GfHGCblESJx7JRE4fj4bFy/KRpeLhi41y4pF3sNA

View File

@ -83,6 +83,14 @@ class StockExchange:
eod_id: str | None = None eod_id: str | None = None
NYSE = StockExchange(
name='New York Stock Exchange',
mic='XNYS',
bloomberg_id='UN',
crunchbase_id='NYSE',
)
@enforce_typing.enforce_types @enforce_typing.enforce_types
@dataclasses.dataclass(frozen=True) @dataclasses.dataclass(frozen=True)
class Asset: class Asset:
@ -117,7 +125,7 @@ class Asset:
raise TypeError(msg) raise TypeError(msg)
@staticmethod @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) """Parses an asset from the [`polygon.io`](https://polygon.io)
identifier format. identifier format.
@ -132,7 +140,7 @@ class Asset:
) )
if polygon_id.startswith('C:'): if polygon_id.startswith('C:'):
return FiatCurrency(polygon_id.removeprefix('C:').removesuffix('USD')) 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 @abc.abstractmethod
def raw_short_name(self) -> str: def raw_short_name(self) -> str:
@ -282,8 +290,8 @@ class Stock(Asset):
raise ValueError(msg) raise ValueError(msg)
@staticmethod @staticmethod
def from_polygon_id(polygon_id: str) -> 'Stock': def from_polygon_id(polygon_id: str, exchange: StockExchange = NYSE) -> 'Stock':
return Stock(polygon_id, NYSE) # TODO: Add support for non-NYSE exchanges. return Stock(polygon_id, exchange)
def raw_short_name(self) -> str: def raw_short_name(self) -> str:
return self.ticker return self.ticker
@ -404,13 +412,6 @@ CURRENCY_SYMBOLS: dict[Currency, str] = {
ASSET_PREFIX: dict[Asset, str] = CURRENCY_SYMBOLS # TODO: Remove at some point. 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 = [ EXCHANGES = [
NYSE, NYSE,
StockExchange(name='Nasdaq', mic='XNAS', bloomberg_id='US', crunchbase_id='NASDAQ'), StockExchange(name='Nasdaq', mic='XNAS', bloomberg_id='US', crunchbase_id='NASDAQ'),