From 4d2c694486047b49adee4cc82d459b39639b7e0e Mon Sep 17 00:00:00 2001 From: Jon Michael Aanes Date: Thu, 8 Aug 2024 18:07:34 +0200 Subject: [PATCH] Added raw short name --- fin_defs/__init__.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/fin_defs/__init__.py b/fin_defs/__init__.py index 3b0cfca..f5223cc 100644 --- a/fin_defs/__init__.py +++ b/fin_defs/__init__.py @@ -20,6 +20,7 @@ import dataclasses import datetime import re from decimal import Decimal +import abc import enforce_typing @@ -105,6 +106,11 @@ class Asset: return FiatCurrency(polygon_id.removeprefix('C:').removesuffix('USD')) return Stock.from_polygon_id(polygon_id) + @abc.abstractmethod + def raw_short_name(self) -> str: + """A short name or ticker that does not nessisarily uniquely identify + the asset, but may be commonly used within the industry.""" + def to_string_id(self) -> str: """Formats the asset id using the fin_defs asset format.""" if isinstance(self, Stock): @@ -167,6 +173,8 @@ class FiatCurrency(Currency): msg = f'iso_code was not in correct format: {self.iso_code}' raise ValueError(msg) + def raw_short_name(self) -> str: + return self.iso_code @enforce_typing.enforce_types @dataclasses.dataclass(frozen=True, eq=True) @@ -181,6 +189,9 @@ class CryptoCurrency(Currency): raise ValueError(msg) """ + def raw_short_name(self) -> str: + return self.ccxt_symbol + @enforce_typing.enforce_types @dataclasses.dataclass(frozen=True, eq=True) class Stock(Asset): @@ -197,6 +208,9 @@ class Stock(Asset): def from_polygon_id(polygon_id: str, stock_exchange: StockExchange) -> 'Stock': return Stock(polygon_id, stock_exchange) + def raw_short_name(self) -> str: + return self.ticker + @enforce_typing.enforce_types @dataclasses.dataclass(frozen=True, eq=True) @@ -208,6 +222,8 @@ class Index(Asset): msg = f'ticker was not in correct format: {self.ticker}' raise ValueError(msg) + def raw_short_name(self) -> str: + return self.ticker @enforce_typing.enforce_types @dataclasses.dataclass(frozen=True, eq=True) @@ -219,6 +235,9 @@ class Commodity(Asset): msg = f'alpha_vantage_id was not in correct format: {self.alpha_vantage_id}' raise ValueError(msg) + def raw_short_name(self) -> str: + return self.alpha_vantage_id # TODO + Commodity.CRUDE_OIL_WTI = Commodity('WTI') Commodity.CRUDE_OIL_BRENT = Commodity('BRENT') @@ -289,7 +308,7 @@ WELL_KNOWN_SYMBOLS = ( ] } | {'XXBT': BTC} - | {str(k): k for k in COMMON_NAMES} + | {k.raw_short_name(): k for k in COMMON_NAMES} | {'SPX500': SPX, 'SP500': SPX, 'Nasdaq 100': NDX} )