This is a currency converter that uses historical rates against a reference currency (Euro). It is compatible with Python3.6+.
The default source is the European Central Bank. This is the ECB historical rates for 42 currencies against the Euro since 1999. It can be downloaded here: eurofxref-hist.zip. The converter can use different sources as long as the format is the same.
Note that the currency converter does not query the API in real time, to avoid the overhead of the HTTP request. It uses embedded data in the library, which might not be up to date. If you need the latest data, please refer to the data section.
You can install directly after cloning:
Or use the Python package:
After installation, you should have currency_converter
in your $PATH
:
Create once the currency converter object:
Convert from EUR
to USD
using the last available rate:
Default target currency is EUR
:
You can change the date of the rate:
You can use your own currency file, as long as it has the same format (ECB):
from currency_converter import ECB_URL, SINGLE_DAY_ECB_URL
# Load the packaged data (might not be up to date)
c = CurrencyConverter()
# Download the full history, this will be up to date. Current value is:
# https://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist.zip
c = CurrencyConverter(ECB_URL)
# Dowload only the latest available day. Current value is:
# https://www.ecb.europa.eu/stats/eurofxref/eurofxref.zip
c = CurrencyConverter(SINGLE_DAY_ECB_URL)
# Load your custom file
c = CurrencyConverter('./path/to/currency/file.csv')
Since the raw data is updated only once a day, it might be better to only download it once a day:
Some rates are missing:
But we have a fallback mode for those, using a linear interpolation of the closest known rates, as long as you ask for a date within the currency date bounds:
The fallback method can be configured with the fallback_on_missing_rate_method
parameter, which currently supports "linear_interpolation"
and "last_known"
values.
We also have a fallback mode for dates outside the currency bounds:
>>> c = CurrencyConverter()
>>> c.convert(100, 'EUR', 'USD', date=date(1986, 2, 2))
Traceback (most recent call last):
RateNotFoundError: 1986-02-02 not in USD bounds 1999-01-04/2016-04-29
>>>
>>> c = CurrencyConverter(fallback_on_wrong_date=True)
>>> c.convert(100, 'EUR', 'USD', date=date(1986, 2, 2)) # fallback to 1999-01-04
117.89...
If you need exact conversions, you can use the decimal
option to use decimal.Decimal
internally when parsing rates. This will slow down the load time by a factor 10 though.
-
bounds
lets you know the first and last available date for each currency
-
currencies
is a set containing all available currencies