From e01b0df9b288b586e8554acd9f37d197b62af469 Mon Sep 17 00:00:00 2001 From: Peter Dekkers Date: Sat, 24 Aug 2024 08:39:44 +0200 Subject: [PATCH] added samples --- roboquant/monetary.py | 19 +++++++++---------- tests/samples/monetary.py | 13 ++++++++++--- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/roboquant/monetary.py b/roboquant/monetary.py index d52b178..5dc88fb 100644 --- a/roboquant/monetary.py +++ b/roboquant/monetary.py @@ -1,7 +1,7 @@ import io import logging import os -from time import time as systemtime +from time import time as stime import zipfile from abc import ABC, abstractmethod from bisect import bisect_left @@ -66,31 +66,29 @@ def convert(self, amount: "Amount", to_currency: Currency, time: datetime) -> fl class ECBConversion(CurrencyConverter): - """Currency that gets it exchange rates from the European Central Bank""" + """Currency that gets it exchange rates from the ECB (European Central Bank).""" __file_name = Path.home() / ".roboquant" / "eurofxref-hist.csv" def __init__(self): - self._rates: Dict[Currency, List[Any]] = {EUR: [(datetime.fromisoformat("2000-01-01T15:00:00+00:00"), 1.0)]} + self._rates: Dict[Currency, List[Any]] = {} if not self.exists(): self._download() self._parse() def _download(self): - # logging.info("downloading new ECB exchange rates") - print("downloading the latest ECB exchange rates") - r = requests.get("https://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist.zip", timeout=5) + logging.info("downloading the latest ECB exchange rates") + r = requests.get("https://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist.zip", timeout=10) with zipfile.ZipFile(io.BytesIO(r.content)) as z: p = Path.home() / ".roboquant" z.extractall(p) def exists(self): - """True if there is already a recently downloaded file""" + """True if there is already a recently (< 12 hours) downloaded file""" if not self.__file_name.exists(): return False - file_time = os.path.getctime(self.__file_name) - local_time = systemtime() - return file_time > local_time - 24.0*3600.0 + diff = stime() - os.path.getctime(self.__file_name) + return diff < 12.0 * 3600.0 @property def currencies(self) -> set[Currency]: @@ -98,6 +96,7 @@ def currencies(self) -> set[Currency]: return set(self._rates.keys()) def _parse(self): + self._rates = {EUR: [(datetime.fromisoformat("2000-01-01T15:00:00+00:00"), 1.0)]} with open(self.__file_name, "r", encoding="utf8") as csv_file: csv_reader = reader(csv_file) header = next(csv_reader)[1:] diff --git a/tests/samples/monetary.py b/tests/samples/monetary.py index 529198b..133e7e3 100644 --- a/tests/samples/monetary.py +++ b/tests/samples/monetary.py @@ -1,16 +1,23 @@ # %% -from roboquant.monetary import EUR, USD, JPY, ECBConversion +from roboquant.monetary import EUR, USD, JPY, ECBConversion, Amount from datetime import datetime # %% +# Different ways to create Amounts and add them to a wallet wallet = 20@EUR + 10@USD + 1000@JPY + 10@USD +wallet += EUR(10.0) +wallet += Amount(EUR, 10) print("The wallet contains", wallet) # %% +# Install a currency converter ECBConversion().register() -print("The wallet is", wallet@EUR) -print("The wallet is", wallet@USD) +# Convert a wallet to a single currency +print("The total value of the wallet today is", wallet@EUR) +print("The total value of the wallet today is", wallet@USD) + +# Convert amounts print("100@USD =", 100@USD@JPY) # %% dt1 = datetime.fromisoformat("2010-01-01-00:00:00+00:00")