forked from domokane/FinancePy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestFinBondFRN.py
429 lines (339 loc) · 13.2 KB
/
TestFinBondFRN.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
###############################################################################
# Copyright (C) 2018, 2019, 2020 Dominic O'Kane
###############################################################################
import sys
sys.path.append("..")
from financepy.products.funding.FinIborSingleCurve import FinIborSingleCurve
from financepy.products.bonds.FinBondFRN import FinBondFRN
from financepy.finutils.FinFrequency import FinFrequencyTypes
from financepy.finutils.FinDayCount import FinDayCountTypes
from financepy.finutils.FinDate import FinDate
from financepy.products.funding.FinIborSwap import FinIborSwap
from financepy.products.funding.FinIborDeposit import FinIborDeposit
from financepy.finutils.FinGlobalTypes import FinSwapTypes
from FinTestCases import FinTestCases, globalTestCaseMode
testCases = FinTestCases(__file__, globalTestCaseMode)
##########################################################################
def buildIborCurve(valuationDate):
depoDCCType = FinDayCountTypes.THIRTY_E_360_ISDA
depos = []
payFixed = FinSwapTypes.PAY
spotDays = 2
settlementDate = valuationDate.addWeekDays(spotDays)
depositRate = 0.050
maturityDate = settlementDate.addMonths(1)
depo1 = FinIborDeposit(
settlementDate,
maturityDate,
depositRate,
depoDCCType)
maturityDate = settlementDate.addMonths(3)
depo2 = FinIborDeposit(
settlementDate,
maturityDate,
depositRate,
depoDCCType)
maturityDate = settlementDate.addMonths(6)
depo3 = FinIborDeposit(
settlementDate,
maturityDate,
depositRate,
depoDCCType)
maturityDate = settlementDate.addMonths(9)
depo4 = FinIborDeposit(
settlementDate,
maturityDate,
depositRate,
depoDCCType)
maturityDate = settlementDate.addMonths(12)
depo5 = FinIborDeposit(
settlementDate,
maturityDate,
depositRate,
depoDCCType)
depos.append(depo1)
depos.append(depo2)
depos.append(depo3)
depos.append(depo4)
depos.append(depo5)
fras = []
fixedDCCType = FinDayCountTypes.ACT_365F
fixedFreqType = FinFrequencyTypes.SEMI_ANNUAL
swaps = []
swapRate = 0.05
maturityDate = settlementDate.addMonths(24)
swap1 = FinIborSwap(
settlementDate,
maturityDate,
swapRate,
payFixed,
fixedFreqType,
fixedDCCType)
swaps.append(swap1)
maturityDate = settlementDate.addMonths(36)
swap2 = FinIborSwap(
settlementDate,
maturityDate,
swapRate,
payFixed,
fixedFreqType,
fixedDCCType)
swaps.append(swap2)
maturityDate = settlementDate.addMonths(48)
swap3 = FinIborSwap(
settlementDate,
maturityDate,
swapRate,
payFixed,
fixedFreqType,
fixedDCCType)
swaps.append(swap3)
maturityDate = settlementDate.addMonths(60)
swap4 = FinIborSwap(
settlementDate,
maturityDate,
swapRate,
payFixed,
fixedFreqType,
fixedDCCType)
swaps.append(swap4)
maturityDate = settlementDate.addMonths(72)
swap5 = FinIborSwap(
settlementDate,
maturityDate,
swapRate,
payFixed,
fixedFreqType,
fixedDCCType)
swaps.append(swap5)
maturityDate = settlementDate.addMonths(84)
swap6 = FinIborSwap(
settlementDate,
maturityDate,
swapRate,
payFixed,
fixedFreqType,
fixedDCCType)
swaps.append(swap6)
maturityDate = settlementDate.addMonths(96)
swap7 = FinIborSwap(
settlementDate,
maturityDate,
swapRate,
payFixed,
fixedFreqType,
fixedDCCType)
swaps.append(swap7)
maturityDate = settlementDate.addMonths(108)
swap8 = FinIborSwap(
settlementDate,
maturityDate,
swapRate,
payFixed,
fixedFreqType,
fixedDCCType)
swaps.append(swap8)
maturityDate = settlementDate.addMonths(120)
swap9 = FinIborSwap(
settlementDate,
maturityDate,
swapRate,
payFixed,
fixedFreqType,
fixedDCCType)
swaps.append(swap9)
liborCurve = FinIborSingleCurve(valuationDate,
depos,
fras,
swaps)
if 1 == 0:
import numpy as np
numSteps = 40
dt = 10 / numSteps
times = np.linspace(0.0, 10.0, numSteps + 1)
df0 = 1.0
for t in times[1:]:
df1 = liborCurve.df(t)
fwd = (df0 / df1 - 1.0) / dt
print(t, df1, fwd)
df0 = df1
return liborCurve
##########################################################################
def test_FinBondFRN():
# https://data.bloomberglp.com/bat/sites/3/2017/07/SF-2017_Paul-Fjeldsted.pdf
# I have a day out problem on the accrued interest - should be 71 and not 72 days
# Other than that agreement on the DM is very good.
##########################################################################
# CITIGROUP FRN SCREENSHOT
##########################################################################
testCases.banner("BLOOMBERG CITIGROUP FRN EXAMPLE")
issueDate = FinDate(10, 11, 2010)
maturityDate = FinDate(10, 11, 2021)
quotedMargin = 0.0025
freqType = FinFrequencyTypes.QUARTERLY
accrualType = FinDayCountTypes.THIRTY_E_360
face = 1000000
bond = FinBondFRN(issueDate,
maturityDate,
quotedMargin,
freqType,
accrualType,
face)
testCases.header("FIELD", "VALUE")
cleanPrice = 96.793
resetIbor = 0.0143456 - quotedMargin
currentIbor = 0.0120534
futureIbors = 0.0130522
settlementDate = FinDate(21, 7, 2017)
dm = bond.discountMargin(settlementDate,
resetIbor,
currentIbor,
futureIbors,
cleanPrice)
testCases.print("Discount Margin (bp) = ", dm * 10000)
fullPrice = bond.fullPriceFromDM(settlementDate,
resetIbor,
currentIbor,
futureIbors,
dm)
testCases.print("Full Price = ", fullPrice)
lastCouponDt = bond._pcd
testCases.print("Last Coupon Date = ", str(lastCouponDt))
accddays = bond._accruedDays
testCases.print("Accrued Days = ", accddays)
accdAmount = bond._accruedInterest
testCases.print("Accrued Amount = ", accdAmount)
principal = bond.principal(settlementDate,
resetIbor,
currentIbor,
futureIbors,
dm)
testCases.print("Dollar Principal = ", principal)
duration = bond.dollarDuration(settlementDate,
resetIbor,
currentIbor,
futureIbors,
dm)
testCases.print("Dollar Rate Duration = ", duration)
modifiedDuration = bond.modifiedRateDuration(settlementDate,
resetIbor,
currentIbor,
futureIbors,
dm)
testCases.print("Modified Rate Duration = ", modifiedDuration)
macauleyDuration = bond.macauleyRateDuration(settlementDate,
resetIbor,
currentIbor,
futureIbors,
dm)
testCases.print("Macauley Duration = ", macauleyDuration)
convexity = bond.convexityFromDM(settlementDate,
resetIbor,
currentIbor,
futureIbors,
dm)
testCases.print("Convexity = ", convexity)
duration = bond.dollarCreditDuration(settlementDate,
resetIbor,
currentIbor,
futureIbors,
dm)
testCases.print("Dollar Credit Duration = ", duration)
modifiedDuration = bond.modifiedCreditDuration(settlementDate,
resetIbor,
currentIbor,
futureIbors,
dm)
testCases.print("Modified Credit Duration = ", modifiedDuration)
##########################################################################
# EXAMPLE
# https://ebrary.net/14293/economics/actual_floater
##########################################################################
testCases.banner("BLOOMBERG CITIGROUP FRN EXAMPLE II")
issueDate = FinDate(28, 3, 2000)
settlementDate = FinDate(28, 3, 2014)
maturityDate = FinDate(3, 2, 2021)
quotedMargin = 0.0020
freqType = FinFrequencyTypes.SEMI_ANNUAL
accrualType = FinDayCountTypes.THIRTY_E_360_ISDA
face = 1000000.0
bond = FinBondFRN(issueDate,
maturityDate,
quotedMargin,
freqType,
accrualType,
face)
testCases.header("FIELD", "VALUE")
cleanPrice = 93.08
resetIbor = 0.00537 - quotedMargin
currentIbor = 0.027558
futureIbors = 0.03295
dm = bond.discountMargin(settlementDate,
resetIbor,
currentIbor,
futureIbors,
cleanPrice)
testCases.print("Discount Margin (bp) = ", dm * 10000)
fullPrice = bond.fullPriceFromDM(settlementDate,
resetIbor,
currentIbor,
futureIbors,
dm)
testCases.print("Full Price = ", fullPrice)
lastCouponDt = bond._pcd
testCases.print("Last Coupon Date = ", str(lastCouponDt))
accddays = bond._accruedDays
testCases.print("Accrued Days = ", accddays)
accdAmount = bond._accruedInterest
testCases.print("Accrued Amount = ", accdAmount)
principal = bond.principal(settlementDate,
resetIbor,
currentIbor,
futureIbors,
dm)
testCases.print("Dollar Principal = ", principal)
duration = bond.dollarDuration(settlementDate,
resetIbor,
currentIbor,
futureIbors,
dm)
testCases.print("Dollar Rate Duration = ", duration)
modifiedDuration = bond.modifiedRateDuration(settlementDate,
resetIbor,
currentIbor,
futureIbors,
dm)
testCases.print("Modified Rate Duration = ", modifiedDuration)
macauleyDuration = bond.macauleyRateDuration(settlementDate,
resetIbor,
currentIbor,
futureIbors,
dm)
testCases.print("Macauley Duration = ", macauleyDuration)
convexity = bond.convexityFromDM(settlementDate,
resetIbor,
currentIbor,
futureIbors,
dm)
testCases.print("Convexity = ", convexity)
principal = bond.principal(settlementDate,
resetIbor,
currentIbor,
futureIbors,
dm)
testCases.print("Principal = ", principal)
duration = bond.dollarCreditDuration(settlementDate,
resetIbor,
currentIbor,
futureIbors,
dm)
testCases.print("Dollar Credit Duration = ", duration)
modifiedDuration = bond.modifiedCreditDuration(settlementDate,
resetIbor,
currentIbor,
futureIbors,
dm)
testCases.print("Modified Credit Duration = ", modifiedDuration)
##########################################################################
test_FinBondFRN()
testCases.compareTestCases()