Skip to content

Commit

Permalink
Add in Euribor Rates and Fix Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JerBouma committed Apr 27, 2024
1 parent 8dbf7c8 commit cb8a046
Show file tree
Hide file tree
Showing 11 changed files with 321 additions and 229 deletions.
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2661,6 +2661,10 @@ The marginal lending facility rate is the interest rate banks pay when they borr
The deposit facility rate is one of the three interest rates the ECB sets every six weeks as part of its monetary policy. The rate defines the interest banks receive for depositing money with the central bank overnight. Find the documentation [here](https://www.jeroenbouma.com/projects/financetoolkit/docs/fixedincome#get_european_central_bank_rates).

> **Euribor Rates**
The Euro Interbank Offered Rate (Euribor) is a daily reference rate based on the averaged interest rates at which Eurozone banks offer to lend unsecured funds to other banks in the euro wholesale money market. It is widely used as the base rate for a variety of financial products, including mortgages, savings accounts, and derivatives. Find the documentation [here](https://www.jeroenbouma.com/projects/financetoolkit/docs/fixedincome#get_euribor_rates).

> **Effective Federal Funds Rate**
The effective federal funds rate (EFFR) is calculated as a volume-weighted median of overnight federal funds transactions reported in the FR 2420 Report of Selected Money Market Rates. Find the documentation [here](https://www.jeroenbouma.com/projects/financetoolkit/docs/fixedincome#get_federal_reserve_rates).
Expand All @@ -2677,7 +2681,7 @@ The TGCR is calculated as a volume-weighted median of transaction-level tri-part
The BGCR is calculated as a volume-weighted median of transaction-level tri-party repo data collected from the Bank of New York Mellon as well as GCF Repo transaction data obtained from the U.S. Department of the Treasury’s Office of Financial Research (OFR). Find the documentation [here](https://www.jeroenbouma.com/projects/financetoolkit/docs/fixedincome#get_federal_reserve_rates).

> **Secured Overnight Financing Rate**
> **Secured Overnight Financing Rate (SOFR)**
The SOFR is calculated as a volume-weighted median of transaction-level tri-party repo data collected from the Bank of New York Mellon as well as GCF Repo transaction data and data on bilateral Treasury repo transactions cleared through FICC’s DVP service, which are obtained from the U.S. Department of the Treasury’s Office of Financial Research (OFR). Find the documentation [here](https://www.jeroenbouma.com/projects/financetoolkit/docs/fixedincome#get_federal_reserve_rates).

Expand All @@ -2686,29 +2690,29 @@ The SOFR is calculated as a volume-weighted median of transaction-level tri-part
<details>
<summary><b>Government Bonds</b></summary>

It is possible to view both short-term (3-month) and long-term (10-year) interest rates for each of the available countries. These rates relate to the interest rates at which countries issue government bonds.
It is possible to view both short-term (3-month) and long-term (10-year) interest rates for each of the available countries. These rates relate to the interest rates at which countries issue government bonds and are used as a benchmark for other interest rates in the economy. For example, the German government bond yield is an overall indicator of the European economy.

These interest rates can be obtained with `get_long_term_interest_rate` or `get_short_term_interest_rate`. As an example:
These interest rates can be obtained with `get_government_bond_yield`. As an example:

```python
from financetoolkit import FixedIncome

fixedincome = FixedIncome()

fixedincome.get_long_term_interest_rate()
fixedincome.get_government_bond_yield()
```

> **Long Term Interest Rates (10 year)**
Long-term interest rates refer to government bonds maturing in ten years. Rates are mainly determined by the price charged by the lender, the risk from the borrower and the fall in the capital value. Long-term interest rates are generally averages of daily rates, measured as a percentage. These interest rates are implied by the prices at which the government bonds are traded on financial markets, not the interest rates at which the loans were issued.

In all cases, they refer to bonds whose capital repayment is guaranteed by governments. Long-term interest rates are one of the determinants of business investment. Low long term interest rates encourage investment in new equipment and high interest rates discourage it. Investment is, in turn, a major source of economic growth. Find the documentation [here](https://www.jeroenbouma.com/projects/financetoolkit/docs/fixedincome#get_long_term_interest_rate).
In all cases, they refer to bonds whose capital repayment is guaranteed by governments. Long-term interest rates are one of the determinants of business investment. Low long term interest rates encourage investment in new equipment and high interest rates discourage it. Investment is, in turn, a major source of economic growth. Find the documentation [here](https://www.jeroenbouma.com/projects/financetoolkit/docs/fixedincome#get_government_bond_yield).

> **Short Term Interest Rates (3 month)**
Short-term interest rates are the rates at which short-term borrowings are effected between financial institutions or the rate at which short-term government paper is issued or traded in the market. Short-term interest rates are generally averages of daily rates, measured as a percentage.

Short-term interest rates are based on three-month money market rates where available. Typical standardised names are “money market rate” and “treasury bill rate”. Find the documentation [here](https://www.jeroenbouma.com/projects/financetoolkit/docs/fixedincome#get_short_term_interest_rate).
Short-term interest rates are based on three-month money market rates where available. Typical standardised names are “money market rate” and “treasury bill rate”. Find the documentation [here](https://www.jeroenbouma.com/projects/financetoolkit/docs/fixedincome#get_government_bond_yield).

</details>

Expand Down
20 changes: 10 additions & 10 deletions financetoolkit/fixedincome/derivative_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


def get_black_price(
fixed_rate: float,
forward_rate: float,
strike_rate: float,
volatility: float,
years_to_maturity: float,
Expand All @@ -25,7 +25,7 @@ def get_black_price(
For more information, see: https://en.wikipedia.org/wiki/Black_model
Args:
fixed_rate (float): Fixed rate of the underlying swap.
forward_rate (float): Forward rate of the underlying swap.
strike_rate (float): Strike rate of the swaption.
volatility (float): Volatility of the underlying swap.
years_to_maturity (float): years to maturity of the swaption.
Expand All @@ -38,14 +38,14 @@ def get_black_price(
tuple[float, float]: A tuple containing the price of the swaption and the payoff of the underlying option.
"""
d1 = (
np.log(fixed_rate / strike_rate) + 0.5 * volatility**2 * years_to_maturity
np.log(forward_rate / strike_rate) + 0.5 * volatility**2 * years_to_maturity
) / (volatility * np.sqrt(years_to_maturity))
d2 = d1 - volatility * np.sqrt(years_to_maturity)

if is_receiver:
payoff = -fixed_rate * norm.cdf(-d1) + strike_rate * norm.cdf(-d2)
payoff = -forward_rate * norm.cdf(-d1) + strike_rate * norm.cdf(-d2)
else:
payoff = fixed_rate * norm.cdf(d1) - strike_rate * norm.cdf(d2)
payoff = forward_rate * norm.cdf(d1) - strike_rate * norm.cdf(d2)

present_value = np.exp(-risk_free_rate * years_to_maturity)
swaption_price = notional * present_value * payoff
Expand All @@ -54,7 +54,7 @@ def get_black_price(


def get_bachelier_price(
fixed_rate: float,
forward_rate: float,
strike_rate: float,
volatility: float,
years_to_maturity: float,
Expand All @@ -71,7 +71,7 @@ def get_bachelier_price(
For more information, see: https://en.wikipedia.org/wiki/Bachelier_model
Args:
fixed_rate (float): Fixed rate of the underlying swap.
forward_rate (float): Forward rate of the underlying swap.
strike_rate (float): Strike rate of the swaption.
volatility (float): Volatility of the underlying swap.
years_to_maturity (float): years to maturity of the swaption.
Expand All @@ -82,14 +82,14 @@ def get_bachelier_price(
Returns:
tuple[float, float]: A tuple containing the price of the swaption and the payoff of the underlying option.
"""
d = (fixed_rate - strike_rate) / (volatility * np.sqrt(years_to_maturity))
d = (forward_rate - strike_rate) / (volatility * np.sqrt(years_to_maturity))

if is_receiver:
payoff = (strike_rate - fixed_rate) * norm.cdf(-d) + volatility * np.sqrt(
payoff = (strike_rate - forward_rate) * norm.cdf(-d) + volatility * np.sqrt(
years_to_maturity
) * norm.pdf(-d)
else:
payoff = (fixed_rate - strike_rate) * norm.cdf(d) + volatility * np.sqrt(
payoff = (forward_rate - strike_rate) * norm.cdf(d) + volatility * np.sqrt(
years_to_maturity
) * norm.pdf(d)

Expand Down
39 changes: 10 additions & 29 deletions financetoolkit/fixedincome/ecb_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,7 @@

import pandas as pd

BASE_URL = "https://data-api.ecb.europa.eu/service/data/FM/"
EXTENSIONS = "?format=csvdata"


def collect_ecb_data(ecb_data_string: str) -> pd.DataFrame:
"""
Collect the data from the ECB API and return it as a DataFrame.
Args:
ecb_data_string (str): The string that is appended to the base URL to
get the data from the ECB API.
Returns:
pd.DataFrame: A DataFrame containing the data from the ECB API.
"""
ecb_data = pd.read_csv(f"{BASE_URL}{ecb_data_string}{EXTENSIONS}")

ecb_data = ecb_data.set_index("TIME_PERIOD")

ecb_data.index = pd.PeriodIndex(data=ecb_data.index, freq="D")

ecb_data.index.name = None

ecb_data = ecb_data["OBS_VALUE"]

return ecb_data
from financetoolkit.fixedincome.helpers import collect_ecb_data


def get_main_refinancing_operations() -> pd.DataFrame:
Expand All @@ -42,7 +17,9 @@ def get_main_refinancing_operations() -> pd.DataFrame:
"""
ecb_data_string = "D.U2.EUR.4F.KR.MRR_RT.LEV"

main_refinancing_operations = collect_ecb_data(ecb_data_string)
main_refinancing_operations = collect_ecb_data(
ecb_data_string=ecb_data_string, dataset="FM", frequency="D"
)

# Convert to percentage
main_refinancing_operations = main_refinancing_operations / 100
Expand All @@ -64,7 +41,9 @@ def get_marginal_lending_facility() -> pd.DataFrame:
"""
ecb_data_string = "D.U2.EUR.4F.KR.MLFR.LEV"

marginal_lending_facility = collect_ecb_data(ecb_data_string)
marginal_lending_facility = collect_ecb_data(
ecb_data_string=ecb_data_string, dataset="FM", frequency="D"
)

# Convert to percentage
marginal_lending_facility = marginal_lending_facility / 100
Expand All @@ -83,7 +62,9 @@ def get_deposit_facility() -> pd.DataFrame:
"""
ecb_data_string = "D.U2.EUR.4F.KR.DFR.LEV"

deposit_facility = collect_ecb_data(ecb_data_string)
deposit_facility = collect_ecb_data(
ecb_data_string=ecb_data_string, dataset="FM", frequency="D"
)

# Convert to percentage
deposit_facility = deposit_facility / 100
Expand Down
33 changes: 33 additions & 0 deletions financetoolkit/fixedincome/euribor_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""ECB Model"""
__docformat__ = "google"

import pandas as pd

from financetoolkit.fixedincome.helpers import collect_ecb_data


def get_euribor_rate(maturity: str, nominal: bool = True) -> pd.DataFrame:
"""
Get the Main Refinancing Operations from the European Central Bank over
time. The Main Refinancing Operations are the rate at which banks can
borrow money from the central bank for the duration of one week.
Returns:
pd.DataFrame: A DataFrame containing the Main Refinancing Operations over time.
"""
if maturity not in ["1M", "3M", "6M", "1Y"]:
raise ValueError("Invalid maturity. Please choose from 1M, 3M, 6M, 1Y.")

if nominal:
ecb_data_string = f"M.U2.EUR.RT.MM.EURIBOR{maturity}D_.HSTA"
else:
ecb_data_string = f"M.U2.EUR.4F.MM.R_EURIBOR{maturity}D_.HSTA"

euribor_rate = collect_ecb_data(
ecb_data_string=ecb_data_string, dataset="FM", frequency="M"
)

# Convert to percentage
euribor_rate = euribor_rate / 100

return euribor_rate
Loading

0 comments on commit cb8a046

Please sign in to comment.