I'm trying to understand forex API through python.The code that I am posting below worked for me on friday and I received all the conversion rates for the dates as desired. Strangely when I run the code today for some reason it says
Currency Rates source not ready.
Why is this happening?
from forex_python.converter import CurrencyRates
import pandas as pd
c = CurrencyRates()
from forex_python.converter import CurrencyRates
c = CurrencyRates()
df = pd.DataFrame(pd.date_range(start='8/16/2021 10:00:00', end='8/22/2021 11:00:00', freq='600min'), columns=['DateTime'])
def get_rate(x):
try:
op = c.get_rate('CAD', 'USD', x)
except Exception as re:
print(re)
op=None
return op
df['Rate'] = df['DateTime'].apply(get_rate)
Currency Rates Source Not Ready
Currency Rates Source Not Ready
df
Out[17]:
DateTime Rate
0 2021-08-16 10:00:00 0.796374
1 2021-08-16 20:00:00 0.796374
2 2021-08-17 06:00:00 0.793031
3 2021-08-17 16:00:00 0.793031
4 2021-08-18 02:00:00 0.792469
5 2021-08-18 12:00:00 0.792469
6 2021-08-18 22:00:00 0.792469
7 2021-08-19 08:00:00 0.783967
8 2021-08-19 18:00:00 0.783967
9 2021-08-20 04:00:00 0.774504
10 2021-08-20 14:00:00 0.774504
11 2021-08-21 00:00:00 NaN
12 2021-08-21 10:00:00 NaN
13 2021-08-21 20:00:00 NaN
14 2021-08-22 06:00:00 NaN
How do I fix this issue? Is there a way to ignore NaN
while making calls itself? I feel that the API only gives results for Monday to Friday from 10 am to 5pm. So is there a way to just get those results.
The python_forex SDK uses The Forex Api, which gets its data from The European Central Bank. The European Central Bank updates the rates on a daily interval (at approximately 16:00 CET) on every working day, except on TARGET closing days. This means that you can only get one value for currency rates, on working days.
To get those rates you could do
df = pd.DataFrame(pd.date_range(start='8/16/2021 10:00:00', end='8/22/2021 11:00:00', freq='B'), columns=['DateTime'])
where freq="B"
means only business days (source). When running the rest of your code this results in:
DateTime Rate
0 2021-08-16 10:00:00 0.796374
1 2021-08-17 10:00:00 0.793031
2 2021-08-18 10:00:00 0.792469
3 2021-08-19 10:00:00 0.783967
4 2021-08-20 10:00:00 0.774504
You could also do it for business hours with freq="BH"
, but you would not get different rates.
Also, note that in the python_forex SDK they say that you should use version 1.6 to avoid RatesNotAvailableError.
NB! I am not planning on maintaining it. I lied.
Due to exchangerate.host adding API keys, rate limit, and paid tier, I have changed the source to directly get data from the European Central Bank in version 0.2.
Install:
pip install easy-exchange-rates
Usage:
from easy_exchange_rates import API
api = API()
df = api.get_exchange_rates(
base_currency="EUR",
start_date="2024-03-12",
end_date="2024-04-21",
targets=["USD","CAD"]
)
print(df.head(5))
>>>
CAD USD
TIME_PERIOD
2024-03-12 1.4739 1.0916
2024-03-13 1.4756 1.0939
2024-03-14 1.4725 1.0925
2024-03-15 1.4731 1.0892
2024-03-18 1.4745 1.0892