- Saved searches
- Use saved searches to filter your results more quickly
- License
- alexprengere/currencyconverter
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.rst
- About
- Currency Converter with Rate
- Features
- Sources [Currency symbols, Bank sources, Language code formatter, crypto-currencies]
- Usage Example
- 1. Currency Conversion
- Available methods:
- 2. Rates
- 2.1 Get Latest Rates
- 2.2 Get Historical Rates
- 2.3 Get Fluctuation Rates
- 2.4 Get Timeseries Rates
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
A Python currency converter using the European Central Bank data.
License
alexprengere/currencyconverter
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.rst
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:
$ python setup.py install --user
Or use the Python package:
$ pip install --user currencyconverter
After installation, you should have currency_converter in your $PATH :
$ currency_converter 100 USD --to EUR 100.000 USD = 87.512 EUR on 2016-05-06
Create once the currency converter object:
>>> from currency_converter import CurrencyConverter >>> c = CurrencyConverter()
Convert from EUR to USD using the last available rate:
>>> c.convert(100, 'EUR', 'USD') # doctest: +SKIP 137.5.
Default target currency is EUR :
>>> c.convert(100, 'EUR') 100.0 >>> c.convert(100, 'USD') # doctest: +SKIP 72.67.
You can change the date of the rate:
>>> from datetime import date # datetime works too >>> c.convert(100, 'EUR', 'USD', date=date(2013, 3, 21)) 129...
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:
import os.path as op import urllib.request from datetime import date from currency_converter import ECB_URL, CurrencyConverter filename = f"ecb_date.today():%Y%m%d>.zip" if not op.isfile(filename): urllib.request.urlretrieve(ECB_URL, filename) c = CurrencyConverter(filename)
>>> c.convert(100, 'BGN', date=date(2010, 11, 21)) Traceback (most recent call last): RateNotFoundError: BGN has no rate for 2010-11-21
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:
>>> c = CurrencyConverter(fallback_on_missing_rate=True) >>> c.convert(100, 'BGN', date=date(2010, 11, 21)) 51.12.
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.
>>> c = CurrencyConverter(decimal=True) >>> c.convert(100, 'EUR', 'USD', date=date(2013, 3, 21)) Decimal('129.100')
>>> first_date, last_date = c.bounds['USD'] >>> first_date datetime.date(1999, 1, 4) >>> last_date # doctest: +SKIP datetime.date(2016, 11, 14)
>>> c.currencies # doctest: +SKIP set(['SGD', 'CAD', 'SEK', 'GBP', . >>> 'AAA' in c.currencies False >>> c.convert(100, 'AAA') Traceback (most recent call last): ValueError: AAA is not a supported currency
About
A Python currency converter using the European Central Bank data.
Currency Converter with Rate
This is a simple python package for currency conversion between any two or to multiple currency. Also, to get latest and historical currency exchange rates & crypto exchange rates. This is based on the free API exchangerate.
Features
For all or some specific currency:
- Get latest rates
- Get historical rates. Historical means on a speficifc date
- Get timeseries rates, between any two dates
- Get fluctuation rates, between any two dates
Sources [Currency symbols, Bank sources, Language code formatter, crypto-currencies]
For all currency symbols, language code for locale formatter, bank source codes, crypto-currency codes, check this Symbol & Code Sources
Usage Example
The followings are examples when you will use it as a package:
1. Currency Conversion
from currency_converter_with_rate import currency currency = currency.Currency() data = currency.convert().base('USD').target('EUR').get()
from currency_converter_with_rate import converter currency = converter.CurrencyConverter() data = currency.base('USD').target('EUR').get()
The output will be like this:
here, base() and target() is required method. Other available methods you will see in following examples are optional.
You can find all the currency symbols to pass in base() and target() method for conversion in the above source link, under the file named currency-symbols.txt
The following examples are given using main Currency() class
- Multiple targets: In above example, target currency is only one. If you want to convert to multiple targets, use like following: (send multiple currency code as a list)
data = currency.convert().base('USD').target(['EUR', 'BDT', 'CZK']).get()
Available methods:
- amount() : By default, base amount is 1.00, you can change it by using amount()
data = currency.convert().base('USD').target('EUR').amount(10).get()
- places() : for rounding currency value upto specific decimal point. The following example will round to 2 decimal point of the target currency value
data = currency.convert().base('USD').target('EUR').amount(100).places(2).get()
- format_language() : to format the price according to a locale formatter, such as if you want to format currency as United Kingdom’s format.
data = currency.convert().base('USD').target('EUR').amount(10).places(2).format_language('en-gb').get()
You can find all language code for locale format, in the above source link, under the file named language-codes-list.txt
- date() : to convert currency on a rate of a specific date. In this method, date paremeter must be in YYYY-MM-DD format
data = currency.convert().base('USD').target('EUR').amount(10).date('2019-08-01').get()
- source() : There are 2 different types of source.
- Bank source: Currency conversion according to a bank reference. In this case, you have to pass the bank’s code in the source(). You can find all bank source code, in the above source link, under the file named bank_sources.txt
data = currency.convert().base('USD').target('EUR').amount(10).source('ecb').get()
- Crypto Currency: Untill now, we are converting physical currency. If you want conversion between two crypto-currency, you have to use source(crypto) for all kind of crypto currency.
And in both base() and target() method, you have to pass any crypto-currency symbols, instead of physical currency, like following:
data = currency.convert().base('BTC').target('PAC').amount(10).source('crypto').get()
The above example convert from bitcoin (BTC) to PAC Protocol (PAC). You can find all crypto-currency symbols, in the above source link, under the file named crypto-currencies.txt
Note: You don’t have to follow any chaining sequence to use or call the optional methods. Just make sure, you call any of them before get() . get() must be last method in the chain.
Such as, you can call places() before amount() , like,
data = currency.convert().base('USD').target('EUR').places(2).amount(100).get()
2. Rates
There are 4 types of rates option.
- 2.1 For latest rate: use latest()
- 2.2 for historical rate: use historical(«2021-05-24») : here date is required, and format must be YYYY-MM-DD
- 2.3 for fluctuations rate: use fluctuations(«2021-01-01», «2021-01-03») : here start_date & end_date is required, and both format must be YYYY-MM-DD
- 2.4 for timeseries rate: use timeseries(«2021-01-01», «2021-01-03») : here start_date & end_date is required, and both format must be YYYY-MM-DD
from currency_converter_with_rate import currency currency = currency.Currency() data = currency.rates().latest().get()
from currency_converter_with_rate import rates currency = rates.Rates() data = currency.latest().get()
Based on which type of rates, you want to get, the methods will be different. But all these rate types, have some common available optional methods. These are following:
1. base(): for changing the base currency (by default EUR, i.e. euro): Enter 3-letter currency code for preferred base currency 2. target(): for target currency: Enter 3-letter currency code, to get multiple target currency, send a comma separated string with 3 digit symbol, without any whitespace, like currency.rates().latest().target("USD,EUR,BDT").get() 3. amount(): to set amount to convert, default it is 1.00 from EURO 4. places(): to set how many decimal point to show for target amount 5. source(): to set source for bank view or crypto-currency - for bank, give any bank code (3 letter code) as a cource - for crypto, source will always be `crypto`, like, source('crypto') This is same as above source() method in currency conversion. 6. format(): to set local currency format language code
Note: You don’t have to follow any chaining sequence to use or call the optional methods. Just make sure, you call any of them before get(). Just like explained above examples
The following examples are given using main Currency() class.
2.1 Get Latest Rates
rate = currency.rates().latest().get()
The output will be like this:
rate = currency.rates().latest().base('USD').target("EUR").get()
rate = currency.rates().latest().base('USD').target("EUR,CZK,USD").amount(100).places(2).source('ecb').format('en-za').get()
2.2 Get Historical Rates
rate = currency.rates().historical("2021-08-30").base('USD').target("USD,EUR,CZK").amount(100).places(2).source('ecb').format('en').get()
as explained above, a date must be passed as a parameter inside historical() in YYYY-MM-DD format. This date indicates that, the rates were for that specific day.
Output will look like following:
2.3 Get Fluctuation Rates
- an example with all available optional methods, multiple target symbol 2 required parameters for fluctuations() : start_date: in YYYY-MM-DD format end_date: in YYYY-MM-DD format
rate = currency.rates().fluctuations("2021-01-01", "2021-01-03").base('USD').target("EUR,CZK").amount(100).places(2).source('ecb').format('en').get()
Output will look like following:
2.4 Get Timeseries Rates
- an example with all available optional methods, multiple target symbol 2 required parameters: start_date: in YYYY-MM-DD format end_date: in YYYY-MM-DD format
rate = currency.rates().timeseries("2021-01-01", "2021-01-03").base('USD').target("EUR,CZK").amount(100).places(3).source('ecb').format('en').get()
Output will look like following:
< "rates": < "2021-01-01": < "CZK": "$2,138.54", "EUR": "$81.49", "USD": "$100.00" >, "2021-01-02": < "CZK": "$2,138.54", "EUR": "$81.49", "USD": "$100.00" >, "2021-01-03": < "CZK": "$2,138.54", "EUR": "$81.49", "USD": "$100.00" >>, "success": true >
If you send wrong parameter/s, in the available methods, or if there is any error in API, you will get an error response like following: