diff --git a/fin_defs/__init__.py b/fin_defs/__init__.py index cc4fbb6..97b1b3b 100644 --- a/fin_defs/__init__.py +++ b/fin_defs/__init__.py @@ -143,6 +143,8 @@ 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) @@ -179,6 +181,8 @@ 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) @@ -192,6 +196,7 @@ class Asset: class UnknownAsset(Asset): pass + @enforce_typing.enforce_types @dataclasses.dataclass(frozen=True) class Currency(Asset): @@ -513,6 +518,10 @@ class AssetAmount: 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}' @@ -525,6 +534,10 @@ class AssetAmount: 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}'