-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfrn.py
357 lines (254 loc) · 11.8 KB
/
frn.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
'''
QuantLib with python example
Copyright (C) 2014 John Orford
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
#######################################################################
## 1) Global data
## 2) Date setup
## 3) Construct yield term structure
## 4) Setup initial bond
## 5) Calibrate and find spread
## 6) Collate results
from QuantLib import *
from volkills.models import FRN
class modelFrn():
#######################################################################
###global data inputs
def __init__( self,
payment_frequency,
valuation_date,
maturity_date,
term_structure_1m,
term_structure_3m,
term_structure_6m,
term_structure_1y,
term_structure_2y,
term_structure_3y,
term_structure_5y,
term_structure_7y,
term_structure_10y,
term_structure_20y,
term_structure_30y,
face,
coupon,
market_value):
self.valuation_date = Date(valuation_date.day,valuation_date.month,valuation_date.year)
self.selmaturity_date = Date(maturity_date.day,maturity_date.month,maturity_date.year)
self.term_structure_1m = term_structure_1m
self.term_structure_3m = term_structure_3m
self.term_structure_6m = term_structure_6m
self.term_structure_1y = term_structure_1y
self.term_structure_2y = term_structure_2y
self.term_structure_3y = term_structure_3y
self.term_structure_5y = term_structure_5y
self.term_structure_7y = term_structure_7y
self.term_structure_10y = term_structure_10y
self.term_structure_20y = term_structure_20y
self.term_structure_30y = term_structure_30y
self.reference_index_term_structure_1m = reference_index_term_structure_1m
self.reference_index_term_structure_3m = reference_index_term_structure_3m
self.reference_index_term_structure_6m = reference_index_term_structure_6m
self.reference_index_term_structure_1y = reference_index_term_structure_1y
self.reference_index_term_structure_2y = reference_index_term_structure_2y
self.reference_index_term_structure_3y = reference_index_term_structure_3y
self.reference_index_term_structure_5y = reference_index_term_structure_5y
self.reference_index_term_structure_7y = reference_index_term_structure_7y
self.reference_index_term_structure_10y = reference_index_term_structure_10y
self.reference_index_term_structure_20y = reference_index_term_structure_20y
self.reference_index_term_structure_30y = reference_index_term_structure_30y
self.face =face
self.market_value = market_value
#e.g. 10% = 0.1 here
self.spread = [spread]
self.current_floating_rate = current_floating_rate
if payment_frequency == 'once':
self.payment_frequency = QuantLib.Once
elif payment_frequency == 'annual':
self.payment_frequency = QuantLib.Annual
elif payment_frequency == 'semiannual':
self.payment_frequency = QuantLib.Semiannual
elif payment_frequency == 'quarterly':
self.payment_frequency = QuantLib.Quarterly
elif payment_frequency == 'bimonthly':
self.payment_frequency = QuantLib.Bimonthly
elif payment_frequency == 'monthly':
self.payment_frequency = QuantLib.Monthly
elif payment_frequency == 'weekly':
self.payment_frequency = QuantLib.Weekly
elif payment_frequency == 'daily':
self.payment_frequency = QuantLib.Daily
#######################################################################
###collate results
def collateResults(self):
#Initial instance
original_results = self.modelFrn(0,0)
###Duration Calc
#Shock and reprice
shocked_up_results = self.modelFrn(original_results['Z Spread'],0.0001)
#Modified duration
mod_duration = -1 * (shocked_up_results['NPV'] - original_results['NPV']) * (10000 / original_results['NPV'])
#Update original duration result
original_results['Duration'] = mod_duration
###Convexity Calc
#Shock and reprice
shocked_down_results = self.modelFrn(original_results['Z Spread'],-0.0001)
#Shock rates down 1bp
mod_duration_2 = -1 * (shocked_down_results['NPV'] - original_results['NPV']) * (10000 / original_results['NPV'])
#Update original convexity result
original_results['Convexity'] = (mod_duration_2 + mod_duration) / (2 * 0.0001)
return original_results
def modelFrn(self,z_spread,basis_point_shock):
#######################################################################
### Global assumptions
calendar = TARGET()
fixingDays = 3
settlementDays = 3
redemption = 100
dayCounter = ActualActual(ActualActual.ISDA)
#######################################################################
###Date setup
settlementDate = calendar.advance(self.valuation_date,settlementDays, Days)
todaysDate = calendar.adjust(self.valuation_date)
Settings.instance().evaluationDate = todaysDate
issueDate = calendar.advance(self.valuation_date,-1, Years)
#######################################################################
## Construct zero coupon bond yield curve
zcQuotes = [ (self.term_structure_1m, Period(1,Months)),
(self.term_structure_3m, Period(3,Months)),
(self.term_structure_6m, Period(6,Months)),
(self.term_structure_1y, Period(1,Years)),
(self.term_structure_2y, Period(2,Years)),
(self.term_structure_3y, Period(3,Years)),
(self.term_structure_5y, Period(5,Years)),
(self.term_structure_7y, Period(7,Years)),
(self.term_structure_10y, Period(10,Years)),
(self.term_structure_20y, Period(20,Years)),
(self.term_structure_30y, Period(30,Years))
]
zcHelpers = [ DepositRateHelper(QuoteHandle(SimpleQuote(r + basis_point_shock)),
tenor, fixingDays,
calendar, ModifiedFollowing,
True, dayCounter)
for (r,tenor) in zcQuotes ]
bondDiscountingTermStructure = PiecewiseFlatForward( self.valuation_date,
zcHelpers,
dayCounter)
discountingTermStructure = RelinkableYieldTermStructureHandle()
discountingTermStructure.linkTo(bondDiscountingTermStructure)
#######################################################################
## Libor/Reference curve set up
zcQuotes2 = [ (self.reference_index_term_structure_1m, Period(1,Months)),
(self.reference_index_term_structure_3m, Period(3,Months)),
(self.reference_index_term_structure_6m, Period(6,Months)),
(self.reference_index_term_structure_1y, Period(1,Years)),
(self.reference_index_term_structure_2y, Period(2,Years)),
(self.reference_index_term_structure_3y, Period(3,Years)),
(self.reference_index_term_structure_5y, Period(5,Years)),
(self.reference_index_term_structure_7y, Period(7,Years)),
(self.reference_index_term_structure_10y, Period(10,Years)),
(self.reference_index_term_structure_20y, Period(20,Years)),
(self.reference_index_term_structure_30y, Period(30,Years))
]
zcHelpers2 = [ DepositRateHelper(QuoteHandle(SimpleQuote(r + basis_point_shock)),
tenor, fixingDays,
calendar, ModifiedFollowing,
True, dayCounter)
for (r,tenor) in zcQuotes2 ]
bondDiscountingTermStructure2 = PiecewiseFlatForward(
self.valuation_date, zcHelpers2,
dayCounter)
liborTermStructure = RelinkableYieldTermStructureHandle()
liborTermStructure.linkTo(bondDiscountingTermStructure2)
#######################################################################
## Bond set up
floatingBondSchedule = Schedule(issueDate,
self.maturity_date,
Period(self.payment_frequency),
calendar,
Unadjusted,
Unadjusted,
DateGeneration.Backward,
True);
#Should move to global data???
libor3m = USDLibor(Period(self.payment_frequency),liborTermStructure)
#need to fix!
#libor3m.addFixing(Date(2, August, 2006),0.0278625)
#libor3m.addFixing(Date(2, February, 2006),0.0278625)
libor3m.addFixing(issueDate,self.current_floating_rate)
floatingRateBond = FloatingRateBond(settlementDays,
self.face,
floatingBondSchedule,
libor3m,
dayCounter,
ModifiedFollowing,
2,
[1.0], # Gearings
self.spread, # Spreads
[], # Caps
[], # Floors
True, # Fixing in arrears
redemption,
issueDate)
############################
##Dummy but necessary(?) code...
# coupon pricers
pricer = BlackIborCouponPricer()
# optionlet volatilities
volatility = 0.0;
vol = ConstantOptionletVolatility( settlementDays,
calendar,
ModifiedFollowing,
volatility,
dayCounter)
pricer.setCapletVolatility(OptionletVolatilityStructureHandle(vol))
setCouponPricer(floatingRateBond.cashflows(),pricer)
###########################
# pricing engine
bondEngine = DiscountingBondEngine(discountingTermStructure)
floatingRateBond.setPricingEngine(bondEngine)
#########################################################################
#Calculate Z spread and reprice
#find accrued - because z spread is calibrated to npv
accrued = floatingRateBond.accruedAmount()
if z_spread == 0:
#calbrate the market value + accrued (assuming mv is quoted clean)
z_spread = CashFlows.zSpread(floatingRateBond.cashflows(),
self.market_value + accrued,
bondDiscountingTermStructure,
dayCounter,
QuantLib.Compounded,
self.payment_frequency,
False)
#add spread to term structure
zSpreadQuoteHandle = QuoteHandle( SimpleQuote(z_spread) )
zSpreadedTermStructure = ZeroSpreadedTermStructure(discountingTermStructure,zSpreadQuoteHandle)
#set engine to use z spreaded term structure
zSpreadRelinkableHandle = RelinkableYieldTermStructureHandle()
zSpreadRelinkableHandle.linkTo(zSpreadedTermStructure)
bondEngine_w_credit_spread = DiscountingBondEngine(zSpreadRelinkableHandle)
floatingRateBond.setPricingEngine(bondEngine_w_credit_spread)
#######################################################################
##Collate results
yieldx = floatingRateBond.bondYield(dayCounter,QuantLib.Compounded,self.payment_frequency,0.0001,100)
#Yield needs to be an interest rate object
y = InterestRate(yieldx,dayCounter,Compounded,Annual)
return {
'Accrued' : accrued,
'NPV' : floatingRateBond.NPV(),
'Z Spread' : z_spread,
'Yield' : yieldx,
'Convexity' : BondFunctions.convexity(floatingRateBond,y),
'BPS' : BondFunctions.bps(floatingRateBond,y),
'Basis Pt Val' : BondFunctions.basisPointValue(floatingRateBond,y),
'Yield Val BP' : BondFunctions.yieldValueBasisPoint(floatingRateBond,y)
}