1
0

Introduce AssetPair

This commit is contained in:
Jon Michael Aanes 2024-08-25 19:14:34 +02:00
parent aad7c9939e
commit e5c9edce5e
Signed by: Jmaa
SSH Key Fingerprint: SHA256:Ab0GfHGCblESJx7JRE4fj4bFy/KRpeLhi41y4pF3sNA

View File

@ -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,