Skip to content

Commit

Permalink
Merge pull request #1 from mbk-dev/master
Browse files Browse the repository at this point in the history
Sink
  • Loading branch information
fzhulitov authored Oct 25, 2022
2 parents f40a3ad + 5221a1f commit cfe8c33
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 39 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
# ecb
[![Python](https://img.shields.io/badge/python-v3-brightgreen.svg)](https://www.python.org/)
[![License](https://img.shields.io/badge/license-MIT-blue)](https://opensource.org/licenses/MIT)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)

# ecb: Python interface to European Central Bank (ECB) API
Several functions to det financial data from European Central Bank (ECB) database.

## Available functions

- `get_gdp_q()` Euro area GDP at market price quarterly timeseries (from 1995)
**ECB key rates (from 1999)**:
- `get_refinancing_rate()` ECB Main refinancing operations key rate time series
- `get_deposit_rate()` ECB key rate on the deposit facility
- `get_marginal_rate()` ECB key rate on marginal lending facility
11 changes: 6 additions & 5 deletions ecb/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from importlib.metadata import version
from ecb.kr import (get_main_rate,
get_deposit_rate,
get_marginal_rate,
)
from ecb.kr import (
get_refinancing_rate,
get_deposit_rate,
get_marginal_rate,
)
from ecb.gdp import get_gdp_q
from ecb.hicp import get_hicp
from ecb.request_data import get_data_frame

# __version__ = version("ecb")
__version__ = version("ecb")
18 changes: 14 additions & 4 deletions ecb/gdp.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,18 @@
import ecb


def get_gdp_q(startperiod: str = '1900-01-01', endperiod: str = None) -> pd.Series:
s = ecb.request_data.get_data_frame(agency='MNA',code='Q.N.I8.W2.S1.S1.B.B1GQ._Z._Z._Z.EUR.V.N',
freq='Q', startperiod=startperiod, endperiod=endperiod)
s.rename("GBP", inplace=True)
def get_gdp_q(start_period: str = "1900-01-01", end_period: str = None) -> pd.Series:
"""
Get Euro area GDP at market price quarterly timeseries.
GDP data is available from 1995.
"""
s = ecb.request_data.get_data_frame(
agency="MNA",
code="Q.N.I8.W2.S1.S1.B.B1GQ._Z._Z._Z.EUR.V.N",
freq="Q",
start_period=start_period,
end_period=end_period,
)
s.rename("gdp", inplace=True)
return s
11 changes: 8 additions & 3 deletions ecb/hicp.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@
today = date.today()


def get_hicp(startperiod: str = '1900-01-01', endperiod: str = None) -> pd.Series:
s = ecb.request_data.get_data_frame("ICP", "M.U2.N.000000.4.INX", "M",
startperiod=startperiod, endperiod=endperiod)
def get_hicp(start_period: str = "1900-01-01", end_period: str = None) -> pd.Series:
s = ecb.request_data.get_data_frame(
"ICP",
"M.U2.N.000000.4.INX",
"M",
start_period=start_period,
end_period=end_period,
)
s.rename("HICP", inplace=True)
return s
29 changes: 23 additions & 6 deletions ecb/kr.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,37 @@
import ecb


def get_main_rate() -> pd.Series:
s = ecb.request_data.get_data_frame('FM', 'D.U2.EUR.4F.KR.MRR_FR.LEV')
def get_refinancing_rate() -> pd.Series:
"""
Get ECB main refinancing operations key rate monthly time series.
"""
s = ecb.request_data.get_data_frame("FM", "D.U2.EUR.4F.KR.MRR_FR.LEV")
s.rename("main_rate", inplace=True)
return s / 100


def get_deposit_rate() -> pd.Series:
s = ecb.request_data.get_data_frame('FM', 'D.U2.EUR.4F.KR.DFR.LEV')
"""
Get ECB key rate on the deposit facility monthly time series.
On deposit facility rate banks can make overnight deposits with the Eurosystem.
"""
s = ecb.request_data.get_data_frame("FM", "D.U2.EUR.4F.KR.DFR.LEV")
s.rename("deposit_rate", inplace=True)
return s / 100


def get_marginal_rate(startperiod: str = '1900-01-01', endperiod: str = None) -> pd.Series:
s = ecb.request_data.get_data_frame('FM', 'D.U2.EUR.4F.KR.MLFR.LEV',
startperiod=startperiod, endperiod=endperiod)
def get_marginal_rate(start_period: str = "1900-01-01", end_period: str = None) -> pd.Series:
"""
Get ECB key rate on marginal lending facility monthly time seriesю
On marginal lending facility rate ECB may offer overnight credit to banks from the Eurosystem.
"""
s = ecb.request_data.get_data_frame(
"FM",
"D.U2.EUR.4F.KR.MLFR.LEV",
start_period=start_period,
end_period=end_period,
)
s.rename("marginal_rate", inplace=True)
return s / 100
51 changes: 34 additions & 17 deletions ecb/request_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,43 @@

from requests import Response

URL_begin = "https://sdw-wsrest.ecb.europa.eu/service/data/"
URL_end = "/"
URL_BASE = "https://sdw-wsrest.ecb.europa.eu/service/data/"
URL_END = "/"


def get_data_frame(agency: str = 'FM', code: str = 'D.U2.EUR.4F.KR.MRR_FR.LEV', freq: str = 'D',
detail: str = "dataonly", format: str = "csvdata",
startperiod: str = '1900-01-01', endperiod: str = None) -> pd.Series:
request_url = URL_begin + agency + URL_end + code
params = {'detail': detail,
'format': format,
'startPeriod': startperiod,
'endPeriod': endperiod
}
abc: Response = requests.get(request_url, params=params)
def get_data_frame(
agency: str = "FM",
code: str = "D.U2.EUR.4F.KR.MRR_FR.LEV",
freq: str = "D",
detail: str = "dataonly",
format: str = "csvdata",
start_period: str = "1900-01-01",
end_period: str = None,
) -> pd.Series:
request_url = URL_BASE + agency + URL_END + code
params = {
"detail": detail,
"format": format,
"start_period": start_period,
"end_period": end_period,
}
try:
abc: Response = requests.get(request_url, params=params)
except requests.exceptions.HTTPError as err:
raise requests.exceptions.HTTPError(
f"HTTP error fetching data for {code}:",
abc.status_code,
abc.reason,
URL_BASE,
) from err
jresp = abc.text
df = pd.read_csv(StringIO(jresp),
usecols=['TIME_PERIOD', 'OBS_VALUE'],
dtype={'OBS_VALUE': float},
parse_dates=['TIME_PERIOD'])
df = pd.read_csv(
StringIO(jresp),
usecols=["TIME_PERIOD", "OBS_VALUE"],
dtype={"OBS_VALUE": float},
parse_dates=["TIME_PERIOD"],
)
df.rename(columns={df.columns[0]: "date"}, inplace=True)
df.set_index('date', inplace=True)
df.set_index("date", inplace=True)
df.index = df.index.to_period(freq=freq)
return df.squeeze()
5 changes: 2 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import pandas as pd
import ecb

abc = ecb.kr.get_main_rate()
abc = ecb.kr.get_refinancing_rate()
print(abc)

abc = ecb.kr.get_deposit_rate()
Expand All @@ -16,4 +15,4 @@
abc = ecb.hicp.get_hicp()
print(abc)


print(ecb.__version__)
5 changes: 5 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ python = "^3.8"
pandas = "^1.5.0"
requests = "^2.28.1"

[tool.poetry.group.dev.dependencies]
black = "^22.10.0"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

[tool.black]
line-length = 120

0 comments on commit cfe8c33

Please sign in to comment.