Skip to content

Commit

Permalink
Yield curves for #9 and #10
Browse files Browse the repository at this point in the history
  • Loading branch information
epogrebnyak committed Oct 2, 2022
1 parent c827b48 commit b34c136
Show file tree
Hide file tree
Showing 7 changed files with 1,870 additions and 2,676 deletions.
11 changes: 11 additions & 0 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,14 @@ def bonds_dataframe():

CURRENCIES
# %%

from finec.yield_curve import YieldCurve, get_yields_from_cbr

y = YieldCurve("2022-09-28")
r1 = y.rate(t=1)
# 830.2383903307176

rs = get_yields_from_cbr("2022-09-28")
# {'0.25': 8.2, '0.50': 8.19, '0.75': 8.23, '1.00': 8.3, '2.00': 8.74, '3.00': 9.22, '5.00': 9.91, '7.00': 10.27, '10.00': 10.5, '15.00': 10.69, '20.00': 10.8, '30.00': 10.9}

# %%
2 changes: 0 additions & 2 deletions finec/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@
def start():
return 1
52 changes: 45 additions & 7 deletions finec/yield_curve.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,34 @@
from dataclasses import dataclass
import numpy as np
from finec.moex import Endpoint

from typing import Dict, Optional

#%%
a1 = 0
a2 = 0.6
k = 1.6


def a(n: int):
def fa(n: int):
if n == 1:
return a1
if n == 2:
return a2
return a(n - 1) + a(2) * k ** (n - 2)
return fa(n - 1) + fa(2) * k ** (n - 2)


def b(n: int):
def fb(n: int):
if n == 1:
return a(2)
return fa(2)
else:
return b(n - 1) * k
return fb(n - 1) * k


def s(t, params):
res = 0
for i in range(1, 9 + 1):
g = params[f"g{i}"]
res += g * np.exp(-((t - a(i)) ** 2) / b(i) ** 2)
res += g * np.exp(-((t - fa(i)) ** 2) / fb(i) ** 2)
return res


Expand Down Expand Up @@ -65,3 +65,41 @@ def yield_curve(date: str, t: float):
params = yield_curve_parameters(date)[-1]
return Y(t, params)


@dataclass
class YieldCurve:
iso_date: str
params: Optional[Dict] = None

def __post_init__(self):
self.params = yield_curve_parameters(self.iso_date)

@property
def last(self):
return self.params[-1]

def rate(self, t: float):
return Y(t, self.last)


#%%
import pandas as pd
from datetime import datetime


def make_date(iso_date: str):
return datetime.strptime(iso_date, "%Y-%m-%d").strftime("%d.%m.%Y")


def make_url(iso_date: str):
return "https://www.cbr.ru/hd_base/zcyc_params/zcyc/?DateTo={}".format(
make_date(iso_date)
)


def get_yields_from_cbr(iso_date: str) -> Dict[str, float]:
df = pd.read_html(make_url(iso_date))[0]
return df.iloc[:, 1:].T.to_dict()[0]


# %%
Loading

0 comments on commit b34c136

Please sign in to comment.