Skip to content

Commit

Permalink
added samples
Browse files Browse the repository at this point in the history
  • Loading branch information
jbaron committed Aug 24, 2024
1 parent 177da4d commit e01b0df
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
19 changes: 9 additions & 10 deletions roboquant/monetary.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -66,38 +66,37 @@ 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]:
"""return the set of supported currencies"""
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:]
Expand Down
13 changes: 10 additions & 3 deletions tests/samples/monetary.py
Original file line number Diff line number Diff line change
@@ -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")
Expand Down

0 comments on commit e01b0df

Please sign in to comment.