!pip install requests
!pip install pandas
!pip install plotly.express
#| ouput: falseCurrency Exchange from API with Python
Setup
- Packages
- Imports
from datetime import datetime, date, timedelta
import time
import requests
import pandas as pd
import plotly.express as px
import plotly.io as pio- Constants
backdating_weeks = 52
currencies = {
"eur": "EUR",
"usd": "USD",
"chf": "CHF",
"gbp": "GBP",
"jpy": "JPY"
}
pio.renderers.default = "notebook_connected"Fetching Data from API
- Calculating Start Date
start_date_list = str(datetime.now() - timedelta(weeks = backdating_weeks))
start_date = start_date_list.split(" ")[0]- API
url_eur_chf = f"https://api.frankfurter.dev/v2/rates?base={currencies["eur"]}&from={start_date}"es={currencies["chf"]}"
url_eur_usd = f"https://api.frankfurter.dev/v2/rates?base={currencies["eur"]}&from={start_date}"es={currencies["usd"]}"
url_eur_gbp = f"https://api.frankfurter.dev/v2/rates?base={currencies["eur"]}&from={start_date}"es={currencies["gbp"]}"
url_eur_jpy = f"https://api.frankfurter.dev/v2/rates?base={currencies["eur"]}&from={start_date}"es={currencies["jpy"]}"- Overview Of Fetched Data
The response from the API is a list of JSON-Objects.
Applying the json() method on the response you can convert the list of JSON-Objects into a list of dictionaries.
response_eur_chf = requests.get(url_eur_chf)
response_eur_usd = requests.get(url_eur_usd)
response_eur_gbp = requests.get(url_eur_gbp)
response_eur_jpy = requests.get(url_eur_jpy)
data_eur_chf = response_eur_chf.json()
data_eur_usd = response_eur_usd.json()
data_eur_gbp = response_eur_gbp.json()
data_eur_jpy = response_eur_jpy.json()
print(data_eur_chf[0])
print(data_eur_usd[0])
print(data_eur_gbp[0])
print(data_eur_jpy[0]){'date': '2025-05-19', 'base': 'EUR', 'quote': 'CHF', 'rate': 0.93839}
{'date': '2025-05-19', 'base': 'EUR', 'quote': 'USD', 'rate': 1.1226}
{'date': '2025-05-19', 'base': 'EUR', 'quote': 'GBP', 'rate': 0.84196}
{'date': '2025-05-19', 'base': 'EUR', 'quote': 'JPY', 'rate': 163.1}
Adding Data To Pandas DataFrame
df_eur_chf = pd.DataFrame(data_eur_chf)
df_eur_usd = pd.DataFrame(data_eur_usd)
df_eur_gbp = pd.DataFrame(data_eur_gbp)
df_eur_jpy = pd.DataFrame(data_eur_jpy)- Overview of DataFrames
df_eur_chf.head()| date | base | quote | rate | |
|---|---|---|---|---|
| 0 | 2025-05-19 | EUR | CHF | 0.93839 |
| 1 | 2025-05-19 | EUR | CHF | 0.93839 |
| 2 | 2025-05-20 | EUR | CHF | 0.93840 |
| 3 | 2025-05-21 | EUR | CHF | 0.93648 |
| 4 | 2025-05-22 | EUR | CHF | 0.93507 |
df_eur_usd.head()| date | base | quote | rate | |
|---|---|---|---|---|
| 0 | 2025-05-19 | EUR | USD | 1.1226 |
| 1 | 2025-05-19 | EUR | USD | 1.1226 |
| 2 | 2025-05-20 | EUR | USD | 1.1254 |
| 3 | 2025-05-21 | EUR | USD | 1.1304 |
| 4 | 2025-05-22 | EUR | USD | 1.1309 |
df_eur_gbp.head()| date | base | quote | rate | |
|---|---|---|---|---|
| 0 | 2025-05-19 | EUR | GBP | 0.84196 |
| 1 | 2025-05-19 | EUR | GBP | 0.84196 |
| 2 | 2025-05-20 | EUR | GBP | 0.84205 |
| 3 | 2025-05-21 | EUR | GBP | 0.84381 |
| 4 | 2025-05-22 | EUR | GBP | 0.84327 |
df_eur_jpy.head()| date | base | quote | rate | |
|---|---|---|---|---|
| 0 | 2025-05-19 | EUR | JPY | 163.10 |
| 1 | 2025-05-19 | EUR | JPY | 163.10 |
| 2 | 2025-05-20 | EUR | JPY | 162.98 |
| 3 | 2025-05-21 | EUR | JPY | 162.97 |
| 4 | 2025-05-22 | EUR | JPY | 162.51 |
Plot Charts
- EUR <-> CHF - Exchange Rate
fig_eur_chf = px.line(df_eur_chf,
x = "date", y = [df_eur_chf["rate"], 1/df_eur_chf["rate"]],
title = f"{currencies["eur"]} <-> {currencies["chf"]} - Exchange Rate",
labels = {"date": "Date", "value": "Exchange Rate"},
height = 400, width = 800
)
fig_eur_chf.data[0].name = f"{currencies["eur"]} / {currencies["chf"]}"
fig_eur_chf.data[1].name = f"{currencies["chf"]} / {currencies["eur"]}"
fig_eur_chf.show()- Summary
The chart shows the EUR/CHF exchange rate together with its inverse CHF/EUR over the last 52 weeks.
- EUR/CHF (blue) trends slightly downward overall (from about 0.94 to 0.916), meaning 1 EUR buys fewer CHF than at the start of the period.
- CHF/EUR (red, inverse) trends upward accordingly (from about 1.066 to 1.092), meaning 1 CHF buys more EUR over time.
- The movement is relatively smooth with short-term fluctuations, but the broader pattern suggests a gradual strengthening of CHF against EUR during the observed period.
- EUR <-> USD - Exchange Rate
fig_eur_usd = px.line(df_eur_usd,
x = "date", y = [df_eur_usd["rate"], 1/df_eur_usd["rate"]],
title = f"{currencies["eur"]} <-> {currencies["usd"]} - Exchange Rate",
labels = {"date": "Date", "value": "Exchange Rate"},
height = 400, width = 800
)
fig_eur_usd.data[0].name = f"{currencies["eur"]} / {currencies["usd"]}"
fig_eur_usd.data[1].name = f"{currencies["usd"]} / {currencies["eur"]}"
fig_eur_usd.show()- Summary
The chart shows the EUR/USD exchange rate together with its inverse USD/EUR over the last 52 weeks.
- EUR/USD (blue) trends upward overall (from about 1.12 to 1.17), meaning 1 EUR buys more USD than at the start of the period.
- USD/EUR (red, inverse) trends downward accordingly (from about 0.89 to 0.855), meaning 1 USD buys fewer EUR over time.
- The movement shows a gradual weakening of the USD against EUR during the observed period, with some short-term volatility but a clear overall direction.
- EUR <-> GBP - Exchange Rate
fig_eur_gbp = px.line(df_eur_gbp,
x = "date", y = [df_eur_gbp["rate"], 1/df_eur_gbp["rate"]],
title = f"{currencies["eur"]} <-> {currencies["gbp"]} - Exchange Rate",
labels = {"date": "Date", "value": "Exchange Rate"},
height = 400, width = 800
)
fig_eur_gbp.data[0].name = f"{currencies["eur"]} / {currencies["gbp"]}"
fig_eur_gbp.data[1].name = f"{currencies["gbp"]} / {currencies["eur"]}"
fig_eur_gbp.show()- Summary
The chart shows the EUR/GBP exchange rate together with its inverse GBP/EUR over the last 52 weeks.
- EUR/GBP (blue) trends upward overall (from about 0.842 to 0.870), meaning 1 EUR buys more GBP than at the start of the period.
- GBP/EUR (red, inverse) trends downward accordingly (from about 1.188 to 1.150), meaning 1 GBP buys fewer EUR over time.
- The movement suggests a gradual weakening of GBP against EUR during the observed period, with a noticeable acceleration upward in the EUR/GBP rate starting around mid-June.
- EUR <-> JPY - Exchange Rate
fig_eur_jpy = px.line(df_eur_jpy,
x = "date", y = [df_eur_jpy["rate"], 1/df_eur_jpy["rate"]],
title = f"{currencies["eur"]} <-> {currencies["jpy"]} - Exchange Rate",
labels = {"date": "Date", "value": "Exchange Rate"},
height = 400, width = 800
)
fig_eur_jpy.data[0].name = f"{currencies["eur"]} / {currencies["jpy"]}"
fig_eur_jpy.data[1].name = f"{currencies["jpy"]} / {currencies["eur"]}"
fig_eur_jpy.show()- Summary
The chart shows the EUR/JPY exchange rate together with its inverse JPY/EUR over the last 52 weeks.
- EUR/JPY (blue) trends strongly upward overall (from about 163 to 185), meaning 1 EUR buys significantly more JPY than at the start of the period.
- JPY/EUR (red, inverse) trends downward accordingly (from about 0.00613 to 0.00541), meaning 1 JPY buys fewer EUR over time.
- The movement indicates a consistent and notable weakening of JPY against EUR during the observed period, with a steady upward climb in the EUR/JPY rate and relatively few reversals.
Export
!quarto render index.ipynb --to html
#| output: false