Skip to content

Commit a0f40be

Browse files
committed
practice 2
1 parent 187767f commit a0f40be

File tree

3 files changed

+155
-0
lines changed

3 files changed

+155
-0
lines changed

new Season/Yield_Curve.txt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2016-03-16 0.00380511
2+
2016-03-17 0.00380511
3+
2016-03-25 0.00402156
4+
2016-04-18 0.0044341
5+
2016-06-20 0.00644756
6+
2016-07-20 0.00635242
7+
2016-08-18 0.0067311
8+
2016-09-15 0.00725112
9+
2016-10-20 0.00730461
10+
2016-12-21 0.00789583
11+
2017-03-21 0.00847526
12+
2017-06-15 0.00896107
13+
2017-09-21 0.00947457
14+
2017-12-20 0.00992643
15+
2020-03-18 0.0130351
16+
2021-03-18 0.01416591
17+
2022-03-18 0.01521747
18+
2023-03-20 0.01613158
19+
2024-03-18 0.0169586
20+
2025-03-18 0.01767249
21+
2026-03-18 0.01835405
22+
2027-03-18 0.01898949
23+
2028-03-20 0.01950475
24+
2031-03-18 0.02076085
25+
2036-03-18 0.02203265
26+
2041-03-18 0.02262565
27+
2046-03-19 0.0229835
28+
2056-03-20 0.02298593
29+
2061-03-18 0.02292559

new Season/practice2.docx

61 KB
Binary file not shown.

new Season/swap_pricing_.py

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
import datetime as dt
2+
from dateutil.relativedelta import relativedelta
3+
from scipy.interpolate import interp1d
4+
import math
5+
import numpy as np
6+
7+
#yield curve loading
8+
data = open("Yield_Curve.txt","r").readlines()
9+
data = [a.rstrip("\n").split("\t") for a in data]
10+
days, yields, dates = [], [], []
11+
for d, y in data:
12+
date = dt.datetime.strptime(d,"%Y-%m-%d")
13+
dates.append(date)
14+
days.append((date-dates[0]).days)
15+
yields.append(float(y))
16+
today = dates[0]
17+
18+
#functions
19+
def act365(date1, date2):
20+
return (date2-date1).days / 365
21+
def act360(date1, date2):
22+
return (date2-date1).days / 360
23+
def spotRate(date, yieldCurve):
24+
days = (date-today).days
25+
return yieldCurve(days)
26+
def df(date, yieldCurve):
27+
return math.exp(-spotRate(date,yieldCurve)
28+
* act365(today,date))
29+
def fwdRate(date1, date2, daycounter, yieldCurve):
30+
return 1/daycounter(date1,date2)*(df(date1,yieldCurve) / df(date2,yieldCurve) -1)
31+
32+
def irs_pricing(today, swaptype, notional, effectiveDate, terminationDate,
33+
fixedRate, fixedTenor, fixedDC,
34+
indexTenor, spread, floatingTenor, floatingDC, lastFixing,
35+
yieldCurve):
36+
c = 1 if swaptype.upper()=="PAY" else -1
37+
#fixed leg
38+
d = effectiveDate
39+
d1 = d + fixedTenor
40+
fixedCF = []
41+
npv = 0
42+
while d1<=terminationDate:
43+
if d1>today:
44+
cf = notional * fixedDC(d,d1) * fixedRate
45+
fixedCF.append([d1, cf])
46+
npv -= cf * df(d1, yieldCurve)
47+
d = d1
48+
d1 = d + fixedTenor
49+
#floating leg
50+
d = effectiveDate
51+
d1 = d + floatingTenor
52+
floatingCF = []
53+
while d1<=terminationDate:
54+
if d1>today:
55+
rate = lastFixing if d<today else fwdRate(d,d1,floatingDC,yieldCurve)
56+
cf = notional * floatingDC(d,d1) * rate
57+
floatingCF.append([d1, cf])
58+
npv += cf * df(d1, yieldCurve)
59+
d = d1
60+
d1 = d + floatingTenor
61+
return (c*npv, fixedCF, floatingCF)
62+
63+
64+
65+
66+
#input dates should be converted to the number of days from evaluation date
67+
#a fake curve
68+
yieldCurve = interp1d(days, yields)
69+
70+
df_vec = np.vectorize(df)
71+
dfs = df_vec(dates,yieldCurve)
72+
#yield curve plotting
73+
'''
74+
import matplotlib.pyplot as plt
75+
fig, ax = plt.subplots(2,1,figsize=(8,8))
76+
ax[0].plot(dates,yields,"s-")
77+
ax[0].set_title("Yield Curve")
78+
ax[1].plot(dates,dfs,"d-")
79+
ax[1].set_title("Discount Factor")
80+
fig.show()
81+
'''
82+
83+
84+
#swap sample pricing
85+
notional = 10000
86+
swaptype = "Rec"
87+
today = dt.datetime(2016, 3, 16)
88+
effectiveDate = dt.datetime(2014, 1, 5)
89+
terminationDate = dt.datetime(2029, 1, 5)
90+
fixedRate = 0.02
91+
fixedDC = act365
92+
fixedTenor = relativedelta(months = 3)
93+
indexTenor = relativedelta(months = 6)
94+
spread = 0.001
95+
floatingDC = act360
96+
floatingTenor = relativedelta(months = 6)
97+
lastFixing = 0.005
98+
99+
price = irs_pricing(today, swaptype, notional, effectiveDate, terminationDate,
100+
fixedRate, fixedTenor, fixedDC,
101+
indexTenor, spread, floatingTenor, floatingDC, lastFixing,
102+
yieldCurve)
103+
print("price = %.3f" % price[0])
104+
105+
import matplotlib.pyplot as plt
106+
fig, ax = plt.subplots(2,1,figsize=(8,8))
107+
fixed = np.array(price[1])
108+
floating = np.array(price[2])
109+
ax[0].bar(fixed[:,0],fixed[:,1], width=60)
110+
ax[1].bar(floating[:,0], floating[:,1], width=60)
111+
fig.show()
112+
113+
114+
115+
116+
117+
118+
119+
120+
121+
122+
123+
124+
125+
126+

0 commit comments

Comments
 (0)