From e5c9edce5e874a1b36e11ae7bc6ab299c0fac831 Mon Sep 17 00:00:00 2001 From: Jon Michael Aanes Date: Sun, 25 Aug 2024 19:14:34 +0200 Subject: [PATCH] Introduce AssetPair --- fin_defs/__init__.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/fin_defs/__init__.py b/fin_defs/__init__.py index a387a41..8321282 100644 --- a/fin_defs/__init__.py +++ b/fin_defs/__init__.py @@ -124,7 +124,8 @@ class Asset: if isinstance(self, Commodity): return f'commodity:{self.alpha_vantage_id}' - raise NotImplementedError('Unsupported asset type: ' + str(self)) + msg = f'Unsupported asset type: {repr(self)}' + raise NotImplementedError(msg) @staticmethod def from_string_id(asset_id: str) -> 'Asset': @@ -481,12 +482,15 @@ class AssetAmount: return self.amount < other.amount +AssetPair = tuple[Asset, Asset] + + @dataclasses.dataclass(frozen=True, eq=True) -class ExchangeRateSample(Asset): +class ExchangeRateSample: """Single exchange rate sample with a timestamp and various stats.""" timestamp: datetime.date | datetime.datetime - base_asset: Asset + asset_pair: AssetPair _average: Decimal | None = None last: Decimal | None = None open: Decimal | None = None @@ -509,6 +513,7 @@ class ExchangeRateSample(Asset): one = Decimal('1') return dataclasses.replace( self, + asset_pair=(self.asset_pair[1], self.asset_pair[0]), _average=one / self._average if self._average else None, last=one / self.last if self.last else None, open=one / self.open if self.open else None,