avatar Bite 301. Exchange rates

In this Bite you have to provide the currency exchange rates from EUR to USD and GBP for every day in a given date range.

In the end, you can conveniently look up exchange rates for every required date. The date range is specified by the parameters start_date and end_date of the function exchange_rates. Both parameters are a string in the format "Year-Month-Day".

Exchange rates data from the exchangeratesapi.io API are given as shown below:

{
  "start_at": "2019-01-01",
  "end_at": "2020-09-01",
  "base": "EUR",
  "rates": {
      "2019-06-28": {
          "USD": 1.138,
          "GBP": 0.89655
      },
      "2020-05-19": {
          "USD": 1.095,
          "GBP": 0.89535
      },
      "...": {}
  }
}

However, this data only contains exchange rates for days on which stock exchange is open. Usually this excludes weekends and several holidays. Therefore, you have to extract this information and derive exchange rates for days that are not available in this data set.

For days where the stock exchange is closed, you have to find the date that precedes a given date when the stock exchange was open. For example, when taking the exchange rates on 2020-04-03 (Friday), the exchange rates for 2020-04-04 (Saturday) and 2020-04-05 (Sunday) are equal to the exchange rates on 2020-04-03 (Friday).

Additionaly, you have to provide the "Base Date" of the exchange rates. The "Base Date" is the date to which the exchange rates are related to originally. So for the above example the Base Dates look like the following:

Date Base Date USD GBP
2020-04-03 2020-04-03 1.0785 0.8785
2020-04-04 2020-04-03 1.0785 0.8785
2020-04-05 2020-04-03 1.0785 0.8785


To obtain all days to be considered, complete the get_all_days function. This function returns a list of all days in date format. Complete the function match_daily_rates which returns a match between a given day and an entry in the original dataset.

The exchange_rates function should return the exchange rates for every day in the date range as shown below:

result = {
  date(2020, 4, 5): {
      "Base Date": date(2020, 4, 3),
      "USD": 1.0785,
      "GBP": 0.8785,
  }
}

Finally two additional requirements:

- Create the result in such a way that it is (chronologically) ordered according to the date keys. You may want to take a look at Python 3.7 changes or at the collections module.

- Check if the given start_date and end_date for exchange_rates are compatible to the provided dataset. If either the start_date lies before start of the dataset range or end_date lies after the end of the dataset range throw a ValueError and inform the user of the particular problem.

Good luck and keep calm and code in Python!

Login and get coding
go back Intermediate level
Bitecoin 3X

39 out of 45 users completed this Bite.
Will you be Pythonista #40 to crack this Bite?
Resolution time: ~68 min. (avg. submissions of 5-240 min.)
Pythonistas rate this Bite 6.33 on a 1-10 difficulty scale.
» Up for a challenge? 💪

Focus on this Bite hiding sidebars, turn on Focus Mode.

Ask for Help