1
0

Compare commits

..

No commits in common. "6a13f5129fb7411ad72ca202d1f7527b76af43e1" and "84ab1e4218afeeabef647cf203b35696c8634545" have entirely different histories.

View File

@ -143,8 +143,6 @@ class Asset:
return f'index:{self.ticker}'
if isinstance(self, Commodity):
return f'commodity:{self.alpha_vantage_id}'
if isinstance(self, UnknownAsset):
return 'unknown:XXX'
msg = f'Unsupported asset type: {repr(self)}'
raise ValueError(msg)
@ -181,8 +179,6 @@ class Asset:
return CryptoCurrency(ticker, **attrs)
if prefix == 'commodity':
return Commodity(ticker)
if prefix == 'unknown':
return UnknownAsset()
msg = f'Unsupported asset format: {asset_id}'
raise ValueError(msg)
@ -191,12 +187,6 @@ class Asset:
return self.to_string_id()
@enforce_typing.enforce_types
@dataclasses.dataclass(frozen=True)
class UnknownAsset(Asset):
pass
@enforce_typing.enforce_types
@dataclasses.dataclass(frozen=True)
class Currency(Asset):
@ -208,7 +198,7 @@ class Currency(Asset):
class FiatCurrency(Currency):
iso_code: str
def __post_init__(self) -> None:
def __post_init__(self):
if not re.match(RE_TICKER_FORMAT, self.iso_code):
msg = f'iso_code was not in correct format: {self.iso_code}'
raise ValueError(msg)
@ -216,22 +206,6 @@ class FiatCurrency(Currency):
def raw_short_name(self) -> str:
return self.iso_code
@staticmethod
def from_currency_symbol(symbol: str) -> 'Currency | None':
for (currency, currency_symbol) in CURRENCY_SYMBOLS.items():
if currency_symbol == symbol:
return currency
return None
def to_currency_symbol(self) -> str:
return CURRENCY_SYMBOLS[self]
FiatCurrency.DKK = FiatCurrency('DKK')
FiatCurrency.NOK = FiatCurrency('NOK')
FiatCurrency.USD = FiatCurrency('USD')
FiatCurrency.EUR = FiatCurrency('EUR')
FiatCurrency.GBP = FiatCurrency('GBP')
FiatCurrency.JPY = FiatCurrency('JPY')
@enforce_typing.enforce_types
@dataclasses.dataclass(frozen=True, eq=True)
@ -239,7 +213,7 @@ class CryptoCurrency(Currency):
ccxt_symbol: str
coingecko_id: str | None = None
def __post_init__(self) -> None:
def __post_init__(self):
"""TODO
if not re.match(RE_CRYPTO_TICKER_FORMAT, self.ccxt_symbol):
msg = f'ccxt_symbol was not in correct format: {self.ccxt_symbol}'
@ -257,7 +231,7 @@ class Stock(Asset):
exchange: StockExchange
nordnet_id: int | None = None
def __post_init__(self) -> None:
def __post_init__(self):
if not re.match(RE_TICKER_FORMAT_FLEXIBLE, self.ticker):
msg = f'ticker was not in correct format: {self.ticker}'
raise ValueError(msg)
@ -275,7 +249,7 @@ class Stock(Asset):
class Index(Asset):
ticker: str
def __post_init__(self) -> None:
def __post_init__(self):
if not re.match(RE_TICKER_FORMAT_FLEXIBLE, self.ticker):
msg = f'ticker was not in correct format: {self.ticker}'
raise ValueError(msg)
@ -289,7 +263,7 @@ class Index(Asset):
class Commodity(Asset):
alpha_vantage_id: str
def __post_init__(self) -> None:
def __post_init__(self):
if not re.match(RE_TICKER_FORMAT, self.alpha_vantage_id):
msg = f'alpha_vantage_id was not in correct format: {self.alpha_vantage_id}'
raise ValueError(msg)
@ -310,10 +284,11 @@ Commodity.COFFEE = Commodity('COFFEE')
# Well known
DKK = FiatCurrency.DKK
USD = FiatCurrency.USD
EUR = FiatCurrency.EUR
DKK = FiatCurrency('DKK')
NOK = FiatCurrency('NOK')
USD = FiatCurrency('USD')
EUR = FiatCurrency('EUR')
GBP = FiatCurrency('GBP')
BTC = CryptoCurrency('BTC', coingecko_id='bitcoin')
MPC = CryptoCurrency('MPC', coingecko_id='partisia-blockchain')
SPX = Index('SPX')
@ -349,7 +324,8 @@ COMMON_NAMES: dict[Asset, list[str]] = {
USDT: ['Tether USDT'],
}
CURRENCY_CODES = {
WELL_KNOWN_SYMBOLS = (
{
sym: FiatCurrency(sym)
for sym in [
'USD',
@ -366,25 +342,18 @@ CURRENCY_CODES = {
'CZK',
]
}
WELL_KNOWN_SYMBOLS = (
CURRENCY_CODES
| {'XXBT': BTC}
| {k.raw_short_name(): k for k in COMMON_NAMES}
| {'SPX500': SPX, 'SP500': SPX, 'Nasdaq 100': NDX}
)
CURRENCY_SYMBOLS: dict[Currency, str] = {
ASSET_PREFIX: dict[Asset, str] = {
USD: '$',
EUR: '',
FiatCurrency.GBP: '£',
GBP: '£',
BTC: '',
FiatCurrency.JPY: '¥',
}
ASSET_PREFIX: dict[Asset, str] = CURRENCY_SYMBOLS # TODO: Remove at some point.
NYSE = StockExchange(
name='New York Stock Exchange',
mic='XNYS',
@ -498,30 +467,26 @@ class AssetAmount:
asset: Asset
amount: Decimal
def __str__(self) -> str:
def __str__(self):
return self.human_readable_str()
def human_readable_str(self) -> str:
def human_readable_str(self):
specificity = '2' if self.amount >= 0.10 else '3'
prefix = ASSET_PREFIX.get(self.asset, '')
return ('{}{:.' + specificity + 'f} {}').format(
prefix, self.amount, self.asset.raw_short_name(),
)
def __mul__(self, other: Decimal) -> 'AssetAmount':
def __mul__(self, other: Decimal):
if not isinstance(other, Decimal):
msg = f'other must be decimal, but was {type(other)}'
raise TypeError(msg)
return AssetAmount(self.asset, self.amount * other)
def __add__(self, other: 'AssetAmount') -> 'AssetAmount':
def __add__(self, other):
if not isinstance(other, AssetAmount):
msg = f'other must be AssetAmount, but was {type(other)}'
raise TypeError(msg)
if self.is_zero():
return other
if other.is_zero():
return self
if self.asset != other.asset:
msg = (
f'AssetAmount must have same asset, but: {self.asset} != {other.asset}'
@ -530,14 +495,10 @@ class AssetAmount:
return AssetAmount(self.asset, self.amount + other.amount)
def __sub__(self, other: 'AssetAmount') -> 'AssetAmount':
def __sub__(self, other):
if not isinstance(other, AssetAmount):
msg = f'other must be AssetAmount, but was {type(other)}'
raise TypeError(msg)
if self.is_zero():
return -other
if other.is_zero():
return self
if self.asset != other.asset:
msg = (
f'AssetAmount must have same asset, but: {self.asset} != {other.asset}'
@ -545,7 +506,7 @@ class AssetAmount:
raise ValueError(msg)
return AssetAmount(self.asset, self.amount - other.amount)
def __truediv__(self, other: 'Decimal | AssetAmount') -> 'Decimal | AssetAmount':
def __truediv__(self, other):
if isinstance(other, AssetAmount):
if self.asset != other.asset:
msg = f'AssetAmount must have same asset, but: {self.asset} != {other.asset}'
@ -553,38 +514,16 @@ class AssetAmount:
return self.amount / other.amount
return AssetAmount(self.asset, self.amount / other)
def __neg__(self) -> 'AssetAmount':
return AssetAmount(self.asset, -self.amount)
def cmp(self, other) -> Decimal:
def __lt__(self, other):
if not isinstance(other, AssetAmount):
msg = f'other must be AssetAmount, but was {type(other)}'
raise TypeError(msg)
if self.is_zero() or other.is_zero():
return self.amount - other.amount
if self.asset != other.asset:
msg = (
f'AssetAmount must have same asset, but: {self.asset} != {other.asset}'
)
raise ValueError(msg)
return self.amount - other.amount
def is_zero(self) -> bool:
return self.amount == 0
def __lt__(self, other: 'AssetAmount') -> bool:
return self.cmp(other) < 0
def __le__(self, other: 'AssetAmount') -> bool:
return self.cmp(other) <= 0
def __gt__(self, other: 'AssetAmount') -> bool:
return self.cmp(other) > 0
def __ge__(self, other: 'AssetAmount') -> bool:
return self.cmp(other) >= 0
AssetAmount.ZERO = AssetAmount(UnknownAsset(), Decimal(0))
return self.amount < other.amount
AssetPair = tuple[Asset, Asset]
@ -611,10 +550,10 @@ class ExchangeRateSample:
return self._average
return (self.high + self.low) / 2
def replace_timestamp(self, timestamp: datetime.date | datetime.datetime) -> 'ExchangeRateSample':
def replace_timestamp(self, timestamp):
return dataclasses.replace(self, timestamp=timestamp)
def invert_exchange_rate(self) -> 'ExchangeRateSample':
def invert_exchange_rate(self):
one = Decimal('1')
return dataclasses.replace(
self,