File: bonds.R

package info (click to toggle)
quantlib-swig 1.40-3
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 2,276 kB
  • sloc: python: 6,024; java: 1,552; cs: 774; makefile: 309; sh: 22
file content (279 lines) | stat: -rw-r--r-- 11,255 bytes parent folder | download | duplicates (2)
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
## bonds.R -- following what bonds.py does for the QuantLib bindings for Python

suppressMessages(library(QuantLib))

## global data
calendar <- TARGET()

settlementDate <- Date(18, "September", 2008)
settlementDate <- Calendar_adjust(calendar, settlementDate)

fixingDays <- 3
settlementDays <- 3
todaysDate <- Calendar_advance(calendar, settlementDate, -fixingDays, "Days")
invisible(Settings_instance()$setEvaluationDate(d=todaysDate))

cat('Today          : ', todaysDate$`__str__`(), "\n")
cat('Settlement Date: ', settlementDate$`__str__`(), "\n")

## market quotes
## constructing bond yield curve
zcQuotes <- list(rates=c(0.0096, 0.0145, 0.0194),
                 tenor=c(Period(3, "Months"),
                 Period(6, "Months"),
                 Period(1, "Years")))

zcBondsDayCounter <- Actual365Fixed()

bondInstruments <- RateHelperVector()

for (i in 1:3) {
    r <- zcQuotes[["rates"]][i]
    tenor <- zcQuotes[["tenor"]][[i]]
    drh <- DepositRateHelper(QuoteHandle(SimpleQuote(r)),
                             tenor,
                             fixingDays,
                             calendar,
                             "ModifiedFollowing",
                             TRUE,
                             zcBondsDayCounter)
    RateHelperVector_push_back(bondInstruments, drh)
}

## setup bonds
redemption <- 100.0
numberOfBonds <- 5

bondQuotes <- list(list(Date(15,"March",2005),
                        Date(31,"August",2010),
                        0.02375, 100.390625),
                   list(Date(15,"June",2005),
                        Date(31,"August",2011),
                        0.04625, 106.21875),
                   list(Date(30,"June",2006),
                        Date(31,"August",2013),
                        0.03125, 100.59375),
                   list(Date(15,"November",2002),
                        Date(15,"August",2018),
                        0.04000, 101.6875),
                   list(Date(15,"May",1987),
                        Date (15,"May",2038),
                        0.04500, 102.140625)
                   )

# Definition of the rate helpers
for (i in 1:5) {
    issueDate <- bondQuotes[[i]][[1]]
    maturity <- bondQuotes[[i]][[2]]
    couponRate <- bondQuotes[[i]][[3]]
    marketQuote <- bondQuotes[[i]][[4]]
    schedule <- Schedule(issueDate, maturity, Period("Semiannual"),
                         UnitedStates("GovernmentBond"),
                         "Unadjusted", "Unadjusted",
                         copyToR(DateGeneration(), "Backward"),
                         FALSE)

    bh <- FixedRateBondHelper(QuoteHandle(SimpleQuote(marketQuote)),
                              settlementDays,
                              100.0,
                              schedule,
                              couponRate,
                              ActualActual("Bond"),
                              "Unadjusted",
                              redemption,
                              issueDate)
    RateHelperVector_push_back(bondInstruments, bh)
}
termStructureDayCounter <-  ActualActual("ISDA")

# not needed as defined in the interface file:  tolerance = 1.0e-15

bondDiscountingTermStructure <- PiecewiseFlatForward(settlementDate,
                                                     bondInstruments,
                                                     termStructureDayCounter)


# Building of the Libor forecasting curve
# deposits
dQuotes <- list(list(0.043375,  Period(1,"Weeks")),
                list(0.031875,  Period(1,"Months")),
                list(0.0320375, Period(3,"Months")),
                list(0.03385,   Period(6,"Months")),
                list(0.0338125, Period(9,"Months")),
                list(0.0335125, Period(1,"Years")))
sQuotes <- list(list(0.0295, Period(2,"Years")),
                list(0.0323, Period(3,"Years")),
                list(0.0359, Period(5,"Years")),
                list(0.0412, Period(10,"Years")),
                list(0.0433, Period(15,"Years")))

## deposits
depositDayCounter <- Actual360()
depoSwapInstruments <- RateHelperVector()

for (i in 1:length(dQuotes)) {
    rate  <- dQuotes[[i]][[1]]
    tenor <- dQuotes[[i]][[2]]
    drh <- DepositRateHelper(QuoteHandle(SimpleQuote(rate)),
                             tenor, fixingDays,
                             calendar, "ModifiedFollowing",
                             TRUE, depositDayCounter)
    RateHelperVector_push_back(depoSwapInstruments, drh)
}


## swaps
swFixedLegFrequency <- "Annual"
swFixedLegConvention <- "Unadjusted"
swFixedLegDayCounter <- Thirty360("European")
swFloatingLegIndex <- Euribor6M()
forwardStart <- Period(1,"Days")
for (i in 1:length(sQuotes)) {
    rate  <- sQuotes[[i]][[1]]
    tenor <- sQuotes[[i]][[2]]
    srh <- SwapRateHelper(QuoteHandle(SimpleQuote(rate)), tenor,
                          calendar, swFixedLegFrequency,
                          swFixedLegConvention, swFixedLegDayCounter,
                          swFloatingLegIndex, QuoteHandle(),forwardStart)
    RateHelperVector_push_back(depoSwapInstruments, srh)
}

depoSwapTermStructure <- PiecewiseFlatForward(settlementDate, depoSwapInstruments,
                                              termStructureDayCounter)

## Term structures that will be used for pricing:
## the one used for discounting cash flows

discountingTermStructure <- RelinkableYieldTermStructureHandle()

## the one used for forward rate forecasting
forecastingTermStructure <- RelinkableYieldTermStructureHandle()

########################################
##        BONDS TO BE PRICED           #
########################################

## common data

faceAmount <- 100

## pricing engine
bondEngine <- DiscountingBondEngine(discountingTermStructure)

## zero coupon bond
zeroCouponBond <- ZeroCouponBond(settlementDays,
                                 UnitedStates("GovernmentBond"),
                                 faceAmount,
                                 Date(15,"August",2013),
                                 "Following",
                                 116.92,
                                 Date(15,"August",2003))

invisible(Instrument_setPricingEngine(zeroCouponBond, bondEngine))

## fixed 4.5% US Treasury note

fixedBondSchedule <- Schedule(Date(15, "May", 2007),
                              Date(15, "May",2017), Period("Semiannual"),
                              UnitedStates("GovernmentBond"),
                              "Unadjusted", "Unadjusted",
                              copyToR(DateGeneration(), "Backward"), FALSE)

fixedRateBond <- FixedRateBond(settlementDays,
                               faceAmount,
                               fixedBondSchedule,
                               0.045,
                               ActualActual("Bond"),
                               "ModifiedFollowing",
                               100.0, Date(15, "May", 2007))
invisible(Instrument_setPricingEngine(fixedRateBond, bondEngine))

## Floating rate bond (3M USD Libor + 0.1%)
## Should and will be priced on another curve later...

liborTermStructure <- RelinkableYieldTermStructureHandle()

libor3m <- USDLibor(Period(3,"Months"),liborTermStructure)
invisible(Index_addFixing(libor3m, Date(17, "July", 2008), 0.0278625))

floatingBondSchedule <- Schedule(Date(21, "October", 2005),
                                 Date(21, "October", 2010), Period("Quarterly"),
                                 UnitedStates("NYSE"),
                                 "Unadjusted", "Unadjusted",
                                 copyToR(DateGeneration(), "Backward"), TRUE)

floatingRateBond <- FloatingRateBond(settlementDays,
                                     faceAmount,
                                     floatingBondSchedule,
                                     libor3m,
                                     Actual360(),
                                     "ModifiedFollowing",
                                     2,
                                     1.0,         # Gearings
                                     0.001,       # Spreads
                                     numeric(0),  #[],      # Caps
                                     numeric(0),  #[],      # Floors
                                     TRUE,    # Fixing in arrears
                                     100.0,
                                     Date(21, "October", 2005))

invisible(Instrument_setPricingEngine(floatingRateBond, bondEngine))

## coupon pricers

pricer <- BlackIborCouponPricer()

## optionlet volatilities
volatility <- 0.0
vol <- ConstantOptionletVolatility(settlementDays,
                                   calendar,
                                   "ModifiedFollowing",
                                   volatility,
                                   Actual365Fixed())

invisible(IborCouponPricer_setCapletVolatility(pricer, OptionletVolatilityStructureHandle(vol)))
invisible(setCouponPricer(Bond_cashflows(floatingRateBond), pricer))


## Yield curve bootstrapping
invisible(RelinkableYieldTermStructureHandle_linkTo(forecastingTermStructure, depoSwapTermStructure))
invisible(RelinkableYieldTermStructureHandle_linkTo(discountingTermStructure, bondDiscountingTermStructure))

## We are using the depo & swap curve to estimate the future Libor rates
invisible(RelinkableYieldTermStructureHandle_linkTo(liborTermStructure, depoSwapTermStructure))

##
df <- data.frame(zeroCoupon=c(Instrument_NPV(zeroCouponBond),
                   Bond_cleanPrice(zeroCouponBond),
                   Bond_dirtyPrice(zeroCouponBond),
                   Bond_accruedAmount(zeroCouponBond),
                   NA,
                   NA,
                   100*Bond_yield(zeroCouponBond, Actual360(), "Compounded", "Annual")),
                 fixedRate=c(Instrument_NPV(fixedRateBond),
                   Bond_cleanPrice(fixedRateBond),
                   Bond_dirtyPrice(fixedRateBond),
                   Bond_accruedAmount(fixedRateBond),
                   100*Bond_previousCouponRate(fixedRateBond),
                   100*Bond_nextCouponRate(fixedRateBond),
                   100*Bond_yield(fixedRateBond, Actual360(), "Compounded", "Annual")),
                 floatingRate=c(Instrument_NPV(floatingRateBond),
                   Bond_cleanPrice(floatingRateBond),
                   Bond_dirtyPrice(floatingRateBond),
                   Bond_accruedAmount(floatingRateBond),
                   100*Bond_previousCouponRate(floatingRateBond),
                   100*Bond_nextCouponRate(floatingRateBond),
                   100*Bond_yield(floatingRateBond, Actual360(), "Compounded", "Annual")),
                 row.names=c("NPV", "Clean Price", "Dirty Price",
                 "Accrued Amount", "Previous Coupon", "Next Coupon", "Yield"))
cat("\nResults:\n")
print(df, digits=5)

# Other computations

cat("\nSample indirect computations (for the floating rate bond):\n")
yld <- Bond_yield(floatingRateBond, Actual360(), "Compounded", "Annual")
clnPrc <- Bond_cleanPrice(floatingRateBond, yld, Actual360(), "Compounded", "Annual", settlementDate)
cat("Yield to Clean Price: ", clnPrc, "\n")
yld <- Bond_yield(floatingRateBond, BondPrice(clnPrc, BondPrice_Clean_get()), Actual360(), "Compounded", "Annual",settlementDate)
cat("Clean Price to Yield: ", yld, "\n")