[go: up one dir, main page]

1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2008 Jose Aparicio
5 Copyright (C) 2014 Peter Caspers
6
7 This file is part of QuantLib, a free-software/open-source library
8 for financial quantitative analysts and developers - http://quantlib.org/
9
10 QuantLib is free software: you can redistribute it and/or modify it
11 under the terms of the QuantLib license. You should have received a
12 copy of the license along with this program; if not, please email
13 <quantlib-dev@lists.sf.net>. The license is also available online at
14 <http://quantlib.org/license.shtml>.
15
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the license for more details.
19*/
20
21#include <ql/qldefines.hpp>
22#if !defined(BOOST_ALL_NO_LIB) && defined(BOOST_MSVC)
23# include <ql/auto_link.hpp>
24#endif
25#include <ql/cashflows/fixedratecoupon.hpp>
26#include <ql/cashflows/iborcoupon.hpp>
27#include <ql/instruments/creditdefaultswap.hpp>
28#include <ql/indexes/ibor/euribor.hpp>
29#include <ql/pricingengines/credit/midpointcdsengine.hpp>
30#include <ql/pricingengines/credit/isdacdsengine.hpp>
31#include <ql/termstructures/credit/piecewisedefaultcurve.hpp>
32#include <ql/termstructures/credit/defaultprobabilityhelpers.hpp>
33#include <ql/termstructures/credit/flathazardrate.hpp>
34#include <ql/termstructures/yield/flatforward.hpp>
35#include <ql/termstructures/yield/piecewiseyieldcurve.hpp>
36#include <ql/termstructures/yield/ratehelpers.hpp>
37#include <ql/math/interpolations/backwardflatinterpolation.hpp>
38#include <ql/time/calendars/target.hpp>
39#include <ql/time/calendars/weekendsonly.hpp>
40#include <ql/time/daycounters/thirty360.hpp>
41#include <ql/time/daycounters/actual365fixed.hpp>
42#include <ql/time/daycounters/actual360.hpp>
43#include <ql/currencies/europe.hpp>
44#include <ql/quotes/simplequote.hpp>
45
46#include <iostream>
47#include <iomanip>
48
49using namespace std;
50using namespace QuantLib;
51
52void example01() {
53
54 std::cout << std::endl;
55
56 /*********************
57 *** MARKET DATA ***
58 *********************/
59
60 Calendar calendar = TARGET();
61 Date todaysDate(15, May, 2007);
62 // must be a business day
63 todaysDate = calendar.adjust(todaysDate);
64
65 Settings::instance().evaluationDate() = todaysDate;
66
67 // dummy curve
68 auto flatRate = ext::make_shared<SimpleQuote>(args: 0.01);
69 Handle<YieldTermStructure> tsCurve(
70 ext::make_shared<FlatForward>(
71 args&: todaysDate, args: Handle<Quote>(flatRate), args: Actual365Fixed()));
72
73 /*
74 In Lehmans Brothers "guide to exotic credit derivatives"
75 p. 32 there's a simple case, zero flat curve with a flat CDS
76 curve with constant market spreads of 150 bp and RR = 50%
77 corresponds to a flat 3% hazard rate. The implied 1-year
78 survival probability is 97.04% and the 2-years is 94.18%
79 */
80
81 // market
82 Natural settlementDays = 1;
83 Real recovery_rate = 0.5;
84 Real quoted_spreads[] = { 0.0150, 0.0150, 0.0150, 0.0150 };
85 vector<Period> tenors;
86 tenors.push_back(x: 3 * Months);
87 tenors.push_back(x: 6 * Months);
88 tenors.push_back(x: 1 * Years);
89 tenors.push_back(x: 2 * Years);
90
91 Date settlementDate = calendar.advance(todaysDate, n: settlementDays, unit: Days);
92 vector<Date> maturities;
93 for (Size i = 0; i < 4; i++) {
94 maturities.push_back(
95 x: calendar.adjust(settlementDate + tenors[i], convention: Following));
96 }
97
98 std::vector<ext::shared_ptr<DefaultProbabilityHelper>> instruments;
99 for (Size i = 0; i < 4; i++) {
100 instruments.push_back(x: ext::make_shared<SpreadCdsHelper>(
101 args: Handle<Quote>(ext::make_shared<SimpleQuote>(args&: quoted_spreads[i])),
102 args&: tenors[i], args&: settlementDays, args&: calendar, args: Quarterly, args: Following,
103 args: DateGeneration::TwentiethIMM, args: Actual365Fixed(),
104 args&: recovery_rate, args&: tsCurve));
105 }
106
107 // Bootstrap hazard rates
108 auto hazardRateStructure = ext::make_shared<PiecewiseDefaultCurve<HazardRate, BackwardFlat>>(
109 args&: todaysDate, args&: instruments, args: Actual365Fixed());
110 vector<pair<Date, Real>> hr_curve_data = hazardRateStructure->nodes();
111
112 cout << "Calibrated hazard rate values: " << endl;
113 for (auto& i : hr_curve_data) {
114 cout << "hazard rate on " << i.first << " is " << i.second << endl;
115 }
116 cout << endl;
117
118 cout << "Some survival probability values: " << endl;
119 cout << "1Y survival probability: "
120 << io::percent(x: hazardRateStructure->survivalProbability(d: todaysDate +
121 1 * Years))
122 << endl << " expected: " << io::percent(x: 0.9704) << endl;
123 cout << "2Y survival probability: "
124 << io::percent(x: hazardRateStructure->survivalProbability(d: todaysDate +
125 2 * Years))
126 << endl << " expected: " << io::percent(x: 0.9418) << endl;
127
128 cout << endl << endl;
129
130 // reprice instruments
131 Real nominal = 1000000.0;
132 Handle<DefaultProbabilityTermStructure> probability(hazardRateStructure);
133 auto engine = ext::make_shared<MidPointCdsEngine>(args&: probability, args&: recovery_rate, args&: tsCurve);
134
135 Schedule cdsSchedule = MakeSchedule()
136 .from(effectiveDate: settlementDate)
137 .to(terminationDate: maturities[0])
138 .withFrequency(Quarterly)
139 .withCalendar(calendar)
140 .withTerminationDateConvention(Unadjusted)
141 .withRule(DateGeneration::TwentiethIMM);
142 CreditDefaultSwap cds_3m(Protection::Seller, nominal, quoted_spreads[0],
143 cdsSchedule, Following, Actual365Fixed());
144
145 cdsSchedule = MakeSchedule()
146 .from(effectiveDate: settlementDate)
147 .to(terminationDate: maturities[1])
148 .withFrequency(Quarterly)
149 .withCalendar(calendar)
150 .withTerminationDateConvention(Unadjusted)
151 .withRule(DateGeneration::TwentiethIMM);
152 CreditDefaultSwap cds_6m(Protection::Seller, nominal, quoted_spreads[1],
153 cdsSchedule, Following, Actual365Fixed());
154
155 cdsSchedule = MakeSchedule()
156 .from(effectiveDate: settlementDate)
157 .to(terminationDate: maturities[2])
158 .withFrequency(Quarterly)
159 .withCalendar(calendar)
160 .withTerminationDateConvention(Unadjusted)
161 .withRule(DateGeneration::TwentiethIMM);
162 CreditDefaultSwap cds_1y(Protection::Seller, nominal, quoted_spreads[2],
163 cdsSchedule, Following, Actual365Fixed());
164
165 cdsSchedule = MakeSchedule()
166 .from(effectiveDate: settlementDate)
167 .to(terminationDate: maturities[3])
168 .withFrequency(Quarterly)
169 .withCalendar(calendar)
170 .withTerminationDateConvention(Unadjusted)
171 .withRule(DateGeneration::TwentiethIMM);
172 CreditDefaultSwap cds_2y(Protection::Seller, nominal, quoted_spreads[3],
173 cdsSchedule, Following, Actual365Fixed());
174
175 cds_3m.setPricingEngine(engine);
176 cds_6m.setPricingEngine(engine);
177 cds_1y.setPricingEngine(engine);
178 cds_2y.setPricingEngine(engine);
179
180 cout << "Repricing of quoted CDSs employed for calibration: " << endl;
181 cout << "3M fair spread: " << io::rate(r: cds_3m.fairSpread()) << endl
182 << " NPV: " << cds_3m.NPV() << endl
183 << " default leg: " << cds_3m.defaultLegNPV() << endl
184 << " coupon leg: " << cds_3m.couponLegNPV() << endl << endl;
185
186 cout << "6M fair spread: " << io::rate(r: cds_6m.fairSpread()) << endl
187 << " NPV: " << cds_6m.NPV() << endl
188 << " default leg: " << cds_6m.defaultLegNPV() << endl
189 << " coupon leg: " << cds_6m.couponLegNPV() << endl << endl;
190
191 cout << "1Y fair spread: " << io::rate(r: cds_1y.fairSpread()) << endl
192 << " NPV: " << cds_1y.NPV() << endl
193 << " default leg: " << cds_1y.defaultLegNPV() << endl
194 << " coupon leg: " << cds_1y.couponLegNPV() << endl << endl;
195
196 cout << "2Y fair spread: " << io::rate(r: cds_2y.fairSpread()) << endl
197 << " NPV: " << cds_2y.NPV() << endl
198 << " default leg: " << cds_2y.defaultLegNPV() << endl
199 << " coupon leg: " << cds_2y.couponLegNPV() << endl << endl;
200
201 cout << endl << endl;
202
203}
204
205void example02() {
206
207Date todaysDate(25, September, 2014);
208Settings::instance().evaluationDate() = todaysDate;
209
210Date termDate = TARGET().adjust(todaysDate+Period(2*Years), convention: Following);
211
212Schedule cdsSchedule =
213 MakeSchedule().from(effectiveDate: todaysDate).to(terminationDate: termDate)
214 .withFrequency(Quarterly)
215 .withCalendar(WeekendsOnly())
216 .withConvention(ModifiedFollowing)
217 .withTerminationDateConvention(ModifiedFollowing)
218 .withRule(DateGeneration::CDS);
219
220std::copy(first: cdsSchedule.begin(), last: cdsSchedule.end(),
221 result: std::ostream_iterator<Date>(cout, "\n"));
222
223 Date evaluationDate = Date(21, October, 2014);
224
225 Settings::instance().evaluationDate() = evaluationDate;
226
227 IborCoupon::Settings::instance().createAtParCoupons();
228
229 // set up ISDA IR curve helpers
230
231 auto dp1m = ext::make_shared<DepositRateHelper>(args: 0.000060, args: 1 * Months, args: 2,
232 args: TARGET(), args: ModifiedFollowing,
233 args: false, args: Actual360());
234 auto dp2m = ext::make_shared<DepositRateHelper>(args: 0.000450, args: 2 * Months, args: 2,
235 args: TARGET(), args: ModifiedFollowing,
236 args: false, args: Actual360());
237 auto dp3m = ext::make_shared<DepositRateHelper>(args: 0.000810, args: 3 * Months, args: 2,
238 args: TARGET(), args: ModifiedFollowing,
239 args: false, args: Actual360());
240 auto dp6m = ext::make_shared<DepositRateHelper>(args: 0.001840, args: 6 * Months, args: 2,
241 args: TARGET(), args: ModifiedFollowing,
242 args: false, args: Actual360());
243 auto dp9m = ext::make_shared<DepositRateHelper>(args: 0.002560, args: 9 * Months, args: 2,
244 args: TARGET(), args: ModifiedFollowing,
245 args: false, args: Actual360());
246 auto dp12m = ext::make_shared<DepositRateHelper>(args: 0.003370, args: 12 * Months, args: 2,
247 args: TARGET(), args: ModifiedFollowing,
248 args: false, args: Actual360());
249
250 // intentionally we do not provide a fixing for the euribor index used for
251 // bootstrapping in order to be compliant with the ISDA specification
252
253 auto euribor6m = ext::make_shared<Euribor>(args: Euribor(6 * Months));
254
255 DayCounter thirty360 = Thirty360(Thirty360::BondBasis);
256
257 auto sw2y = ext::make_shared<SwapRateHelper>(
258 args: 0.002230, args: 2 * Years, args: TARGET(), args: Annual, args: ModifiedFollowing, args&: thirty360,
259 args&: euribor6m);
260 auto sw3y = ext::make_shared<SwapRateHelper>(
261 args: 0.002760, args: 3 * Years, args: TARGET(), args: Annual, args: ModifiedFollowing, args&: thirty360,
262 args&: euribor6m);
263 auto sw4y = ext::make_shared<SwapRateHelper>(
264 args: 0.003530, args: 4 * Years, args: TARGET(), args: Annual, args: ModifiedFollowing, args&: thirty360,
265 args&: euribor6m);
266 auto sw5y = ext::make_shared<SwapRateHelper>(
267 args: 0.004520, args: 5 * Years, args: TARGET(), args: Annual, args: ModifiedFollowing, args&: thirty360,
268 args&: euribor6m);
269 auto sw6y = ext::make_shared<SwapRateHelper>(
270 args: 0.005720, args: 6 * Years, args: TARGET(), args: Annual, args: ModifiedFollowing, args&: thirty360,
271 args&: euribor6m);
272 auto sw7y = ext::make_shared<SwapRateHelper>(
273 args: 0.007050, args: 7 * Years, args: TARGET(), args: Annual, args: ModifiedFollowing, args&: thirty360,
274 args&: euribor6m);
275 auto sw8y = ext::make_shared<SwapRateHelper>(
276 args: 0.008420, args: 8 * Years, args: TARGET(), args: Annual, args: ModifiedFollowing, args&: thirty360,
277 args&: euribor6m);
278 auto sw9y = ext::make_shared<SwapRateHelper>(
279 args: 0.009720, args: 9 * Years, args: TARGET(), args: Annual, args: ModifiedFollowing, args&: thirty360,
280 args&: euribor6m);
281 auto sw10y = ext::make_shared<SwapRateHelper>(
282 args: 0.010900, args: 10 * Years, args: TARGET(), args: Annual, args: ModifiedFollowing, args&: thirty360,
283 args&: euribor6m);
284 auto sw12y = ext::make_shared<SwapRateHelper>(
285 args: 0.012870, args: 12 * Years, args: TARGET(), args: Annual, args: ModifiedFollowing, args&: thirty360,
286 args&: euribor6m);
287 auto sw15y = ext::make_shared<SwapRateHelper>(
288 args: 0.014970, args: 15 * Years, args: TARGET(), args: Annual, args: ModifiedFollowing, args&: thirty360,
289 args&: euribor6m);
290 auto sw20y = ext::make_shared<SwapRateHelper>(
291 args: 0.017000, args: 20 * Years, args: TARGET(), args: Annual, args: ModifiedFollowing, args&: thirty360,
292 args&: euribor6m);
293 auto sw30y = ext::make_shared<SwapRateHelper>(
294 args: 0.018210, args: 30 * Years, args: TARGET(), args: Annual, args: ModifiedFollowing, args&: thirty360,
295 args&: euribor6m);
296
297 std::vector<ext::shared_ptr<RateHelper>> isdaRateHelper;
298
299 isdaRateHelper.push_back(x: dp1m);
300 isdaRateHelper.push_back(x: dp2m);
301 isdaRateHelper.push_back(x: dp3m);
302 isdaRateHelper.push_back(x: dp6m);
303 isdaRateHelper.push_back(x: dp9m);
304 isdaRateHelper.push_back(x: dp12m);
305 isdaRateHelper.push_back(x: sw2y);
306 isdaRateHelper.push_back(x: sw3y);
307 isdaRateHelper.push_back(x: sw4y);
308 isdaRateHelper.push_back(x: sw5y);
309 isdaRateHelper.push_back(x: sw6y);
310 isdaRateHelper.push_back(x: sw7y);
311 isdaRateHelper.push_back(x: sw8y);
312 isdaRateHelper.push_back(x: sw9y);
313 isdaRateHelper.push_back(x: sw10y);
314 isdaRateHelper.push_back(x: sw12y);
315 isdaRateHelper.push_back(x: sw15y);
316 isdaRateHelper.push_back(x: sw20y);
317 isdaRateHelper.push_back(x: sw30y);
318
319 Handle<YieldTermStructure> rateTs(
320 ext::make_shared<PiecewiseYieldCurve<Discount, LogLinear>>(
321 args: 0, args: WeekendsOnly(), args&: isdaRateHelper, args: Actual365Fixed()));
322 rateTs->enableExtrapolation();
323
324 // output rate curve
325 std::cout << "ISDA rate curve: " << std::endl;
326 for (auto& i : isdaRateHelper) {
327 Date d = i->latestDate();
328 std::cout << d << "\t" << setprecision(6) <<
329 rateTs->zeroRate(d,resultDayCounter: Actual365Fixed(),comp: Continuous).rate() << "\t" <<
330 rateTs->discount(d) << std::endl;
331 }
332
333 // build reference credit curve (flat)
334 auto defaultTs0 = ext::make_shared<FlatHazardRate>(args: 0, args: WeekendsOnly(), args: 0.016739207493630, args: Actual365Fixed());
335
336 // reference CDS
337 Schedule sched( Date(22,September,2014), Date(20,December,2019), 3*Months,
338 WeekendsOnly(), Following, Unadjusted, DateGeneration::CDS, false, Date(), Date() );
339 auto trade = ext::make_shared<CreditDefaultSwap>(
340 args: Protection::Buyer, args: 100000000.0, args: 0.01, args&: sched,
341 args: Following, args: Actual360(), args: true, args: true,
342 args: Date(22,October,2014), args: ext::shared_ptr<Claim>(),
343 args: Actual360(true), args: true);
344
345 auto cp = ext::dynamic_pointer_cast<FixedRateCoupon>(r: trade->coupons()[0]);
346 std::cout << "first period = " << cp->accrualStartDate() << " to " << cp->accrualEndDate() <<
347 " accrued amount = " << cp->accruedAmount(Date(24,October,2014)) << std::endl;
348
349 // price with isda engine
350 auto engine = ext::make_shared<IsdaCdsEngine>(
351 args: Handle<DefaultProbabilityTermStructure>(defaultTs0), args: 0.4, args&: rateTs,
352 args: false, args: IsdaCdsEngine::Taylor, args: IsdaCdsEngine::NoBias, args: IsdaCdsEngine::Piecewise);
353
354 trade->setPricingEngine(engine);
355
356 std::cout << "reference trade NPV = " << trade->NPV() << std::endl;
357
358
359 // build credit curve with one cds
360 std::vector<ext::shared_ptr<DefaultProbabilityHelper>> isdaCdsHelper;
361
362 auto cds5y = ext::make_shared<SpreadCdsHelper>(
363 args: 0.00672658551, args: 4 * Years + 6 * Months, args: 1, args: WeekendsOnly(), args: Quarterly,
364 args: Following, args: DateGeneration::CDS, args: Actual360(), args: 0.4, args&: rateTs, args: true, args: true,
365 args: Date(), args: Actual360(true), args: true, args: CreditDefaultSwap::ISDA);
366
367 isdaCdsHelper.push_back(x: cds5y);
368
369 Handle<DefaultProbabilityTermStructure> defaultTs(ext::make_shared<
370 PiecewiseDefaultCurve<SurvivalProbability, LogLinear>>(
371 args: 0, args: WeekendsOnly(), args&: isdaCdsHelper, args: Actual365Fixed()));
372
373 std::cout << "ISDA credit curve: " << std::endl;
374 for (auto& i : isdaCdsHelper) {
375 Date d = i->latestDate();
376 Real pd = defaultTs->defaultProbability(d);
377 Real t = defaultTs->timeFromReference(d);
378 std::cout << d << ";" << pd << ";" << 1.0 - pd << ";" <<
379 -std::log(x: 1.0-pd)/t << std::endl;
380 }
381
382
383 // // set up sample CDS trade
384
385 // auto trade =
386 // MakeCreditDefaultSwap(5 * Years, 0.03);
387
388 // // set up isda engine
389
390 // // auto isdaPricer =
391 // // ext::make_shared<IsdaCdsEngine>(
392 // // isdaCdsHelper, 0.4, isdaRateHelper);
393 // auto isdaPricer =
394 // ext::make_shared<IsdaCdsEngine>(defaultTs,0.40,rateTs);
395
396 // check the curves built by the engine
397
398 // Handle<YieldTermStructure> isdaYts = isdaPricer->isdaRateCurve();
399 // Handle<DefaultProbabilityTermStructure> isdaCts = isdaPricer->isdaCreditCurve();
400
401 // std::cout << "isda rate 1m " << dp1m->latestDate() << " "
402 // << isdaYts->zeroRate(dp1m->latestDate(), Actual365Fixed(),
403 // Continuous) << std::endl;
404 // std::cout << "isda rate 3m " << dp3m->latestDate() << " "
405 // << isdaYts->zeroRate(dp3m->latestDate(), Actual365Fixed(),
406 // Continuous) << std::endl;
407 // std::cout << "isda rate 6m " << dp6m->latestDate() << " "
408 // << isdaYts->zeroRate(dp6m->latestDate(), Actual365Fixed(),
409 // Continuous) << std::endl;
410
411 // std::cout << "isda hazard 5y " << cds5y->latestDate() << " "
412 // << isdaCts->hazardRate(cds5y->latestDate()) << std::endl;
413
414 // price the trade
415
416 // trade->setPricingEngine(isdaPricer);
417
418 // Real npv = trade->NPV();
419
420 // std::cout << "Pricing of example trade with ISDA engine:" << std::endl;
421 // std::cout << "NPV = " << npv << std::endl;
422
423}
424
425void example03() {
426
427 // this is the example from Apdx E in pricing and risk management of CDS, OpenGamma
428
429 Date tradeDate(13,June,2011);
430
431 Settings::instance().evaluationDate() = tradeDate;
432
433 IborCoupon::Settings::instance().createAtParCoupons();
434
435 DayCounter actual360 = Actual360();
436 DayCounter thirty360 = Thirty360(Thirty360::BondBasis);
437
438 auto dp1m = ext::make_shared<DepositRateHelper>(args: 0.00445, args: 1 * Months, args: 2,
439 args: WeekendsOnly(), args: ModifiedFollowing,
440 args: false, args&: actual360);
441 auto dp2m = ext::make_shared<DepositRateHelper>(args: 0.00949, args: 2 * Months, args: 2,
442 args: WeekendsOnly(), args: ModifiedFollowing,
443 args: false, args&: actual360);
444 auto dp3m = ext::make_shared<DepositRateHelper>(args: 0.01234, args: 3 * Months, args: 2,
445 args: WeekendsOnly(), args: ModifiedFollowing,
446 args: false, args&: actual360);
447 auto dp6m = ext::make_shared<DepositRateHelper>(args: 0.01776, args: 6 * Months, args: 2,
448 args: WeekendsOnly(), args: ModifiedFollowing,
449 args: false, args&: actual360);
450 auto dp9m = ext::make_shared<DepositRateHelper>(args: 0.01935, args: 9 * Months, args: 2,
451 args: WeekendsOnly(), args: ModifiedFollowing,
452 args: false, args&: actual360);
453 auto dp1y = ext::make_shared<DepositRateHelper>(args: 0.02084, args: 12 * Months, args: 2,
454 args: WeekendsOnly(), args: ModifiedFollowing,
455 args: false, args&: actual360);
456
457 // this index is probably not important since we are not using
458 // IborCoupon::Settings::instance().usingAtParCoupons() == false
459 // - define it "isda compliant" anyway
460 auto euribor6m = ext::make_shared<IborIndex>(
461 args: "IsdaIbor", args: 6 * Months, args: 2, args: EURCurrency(), args: WeekendsOnly(),
462 args: ModifiedFollowing, args: false, args&: actual360);
463
464 auto sw2y = ext::make_shared<SwapRateHelper>(
465 args: 0.01652, args: 2 * Years, args: WeekendsOnly(), args: Annual, args: ModifiedFollowing, args&: thirty360,
466 args&: euribor6m);
467 auto sw3y = ext::make_shared<SwapRateHelper>(
468 args: 0.02018, args: 3 * Years, args: WeekendsOnly(), args: Annual, args: ModifiedFollowing, args&: thirty360,
469 args&: euribor6m);
470 auto sw4y = ext::make_shared<SwapRateHelper>(
471 args: 0.02303, args: 4 * Years, args: WeekendsOnly(), args: Annual, args: ModifiedFollowing, args&: thirty360,
472 args&: euribor6m);
473 auto sw5y = ext::make_shared<SwapRateHelper>(
474 args: 0.02525, args: 5 * Years, args: WeekendsOnly(), args: Annual, args: ModifiedFollowing, args&: thirty360,
475 args&: euribor6m);
476 auto sw6y = ext::make_shared<SwapRateHelper>(
477 args: 0.02696, args: 6 * Years, args: WeekendsOnly(), args: Annual, args: ModifiedFollowing, args&: thirty360,
478 args&: euribor6m);
479 auto sw7y = ext::make_shared<SwapRateHelper>(
480 args: 0.02825, args: 7 * Years, args: WeekendsOnly(), args: Annual, args: ModifiedFollowing, args&: thirty360,
481 args&: euribor6m);
482 auto sw8y = ext::make_shared<SwapRateHelper>(
483 args: 0.02931, args: 8 * Years, args: WeekendsOnly(), args: Annual, args: ModifiedFollowing, args&: thirty360,
484 args&: euribor6m);
485 auto sw9y = ext::make_shared<SwapRateHelper>(
486 args: 0.03017, args: 9 * Years, args: WeekendsOnly(), args: Annual, args: ModifiedFollowing, args&: thirty360,
487 args&: euribor6m);
488 auto sw10y = ext::make_shared<SwapRateHelper>(
489 args: 0.03092, args: 10 * Years, args: WeekendsOnly(), args: Annual, args: ModifiedFollowing, args&: thirty360,
490 args&: euribor6m);
491 auto sw11y = ext::make_shared<SwapRateHelper>(
492 args: 0.03160, args: 11 * Years, args: WeekendsOnly(), args: Annual, args: ModifiedFollowing, args&: thirty360,
493 args&: euribor6m);
494 auto sw12y = ext::make_shared<SwapRateHelper>(
495 args: 0.03231, args: 12 * Years, args: WeekendsOnly(), args: Annual, args: ModifiedFollowing, args&: thirty360,
496 args&: euribor6m);
497 auto sw15y = ext::make_shared<SwapRateHelper>(
498 args: 0.03367, args: 15 * Years, args: WeekendsOnly(), args: Annual, args: ModifiedFollowing, args&: thirty360,
499 args&: euribor6m);
500 auto sw20y = ext::make_shared<SwapRateHelper>(
501 args: 0.03419, args: 20 * Years, args: WeekendsOnly(), args: Annual, args: ModifiedFollowing, args&: thirty360,
502 args&: euribor6m);
503 auto sw25y = ext::make_shared<SwapRateHelper>(
504 args: 0.03411, args: 25 * Years, args: WeekendsOnly(), args: Annual, args: ModifiedFollowing, args&: thirty360,
505 args&: euribor6m);
506 auto sw30y = ext::make_shared<SwapRateHelper>(
507 args: 0.03412, args: 30 * Years, args: WeekendsOnly(), args: Annual, args: ModifiedFollowing, args&: thirty360,
508 args&: euribor6m);
509
510 std::vector<ext::shared_ptr<RateHelper>> isdaYieldHelpers;
511
512 isdaYieldHelpers.push_back(x: dp1m);
513 isdaYieldHelpers.push_back(x: dp2m);
514 isdaYieldHelpers.push_back(x: dp3m);
515 isdaYieldHelpers.push_back(x: dp6m);
516 isdaYieldHelpers.push_back(x: dp9m);
517 isdaYieldHelpers.push_back(x: dp1y);
518 isdaYieldHelpers.push_back(x: sw2y);
519 isdaYieldHelpers.push_back(x: sw3y);
520 isdaYieldHelpers.push_back(x: sw4y);
521 isdaYieldHelpers.push_back(x: sw5y);
522 isdaYieldHelpers.push_back(x: sw6y);
523 isdaYieldHelpers.push_back(x: sw7y);
524 isdaYieldHelpers.push_back(x: sw8y);
525 isdaYieldHelpers.push_back(x: sw9y);
526 isdaYieldHelpers.push_back(x: sw10y);
527 isdaYieldHelpers.push_back(x: sw11y);
528 isdaYieldHelpers.push_back(x: sw12y);
529 isdaYieldHelpers.push_back(x: sw15y);
530 isdaYieldHelpers.push_back(x: sw20y);
531 isdaYieldHelpers.push_back(x: sw25y);
532 isdaYieldHelpers.push_back(x: sw30y);
533
534 // build yield curve
535 Handle<YieldTermStructure> isdaYts = Handle<YieldTermStructure>(
536 ext::make_shared<PiecewiseYieldCurve<Discount, LogLinear>>(
537 args: 0, args: WeekendsOnly(), args&: isdaYieldHelpers, args: Actual365Fixed()));
538 isdaYts->enableExtrapolation();
539
540
541 CreditDefaultSwap::PricingModel model = CreditDefaultSwap::ISDA;
542 auto cds6m = ext::make_shared<SpreadCdsHelper>(
543 args: 0.007927, args: 6 * Months, args: 1, args: WeekendsOnly(), args: Quarterly, args: Following,
544 args: DateGeneration::CDS, args: Actual360(), args: 0.4, args&: isdaYts, args: true, args: true, args: Date(),
545 args: Actual360(true), args: true, args&: model);
546 auto cds1y = ext::make_shared<SpreadCdsHelper>(
547 args: 0.007927, args: 1 * Years, args: 1, args: WeekendsOnly(), args: Quarterly, args: Following,
548 args: DateGeneration::CDS, args: Actual360(), args: 0.4, args&: isdaYts, args: true, args: true, args: Date(),
549 args: Actual360(true), args: true, args&: model);
550 auto cds3y = ext::make_shared<SpreadCdsHelper>(
551 args: 0.012239, args: 3 * Years, args: 1, args: WeekendsOnly(), args: Quarterly, args: Following,
552 args: DateGeneration::CDS, args: Actual360(), args: 0.4, args&: isdaYts, args: true, args: true, args: Date(),
553 args: Actual360(true), args: true, args&: model);
554 auto cds5y = ext::make_shared<SpreadCdsHelper>(
555 args: 0.016979, args: 5 * Years, args: 1, args: WeekendsOnly(), args: Quarterly, args: Following,
556 args: DateGeneration::CDS, args: Actual360(), args: 0.4, args&: isdaYts, args: true, args: true, args: Date(),
557 args: Actual360(true), args: true, args&: model);
558 auto cds7y = ext::make_shared<SpreadCdsHelper>(
559 args: 0.019271, args: 7 * Years, args: 1, args: WeekendsOnly(), args: Quarterly, args: Following,
560 args: DateGeneration::CDS, args: Actual360(), args: 0.4, args&: isdaYts, args: true, args: true, args: Date(),
561 args: Actual360(true), args: true, args&: model);
562 auto cds10y = ext::make_shared<SpreadCdsHelper>(
563 args: 0.020860, args: 10 * Years, args: 1, args: WeekendsOnly(), args: Quarterly, args: Following,
564 args: DateGeneration::CDS, args: Actual360(), args: 0.4, args&: isdaYts, args: true, args: true, args: Date(),
565 args: Actual360(true), args: true, args&: model);
566
567 std::vector<ext::shared_ptr<DefaultProbabilityHelper>> isdaCdsHelpers;
568
569 isdaCdsHelpers.push_back(x: cds6m);
570 isdaCdsHelpers.push_back(x: cds1y);
571 isdaCdsHelpers.push_back(x: cds3y);
572 isdaCdsHelpers.push_back(x: cds5y);
573 isdaCdsHelpers.push_back(x: cds7y);
574 isdaCdsHelpers.push_back(x: cds10y);
575
576 // build credit curve
577 auto isdaCts =
578 Handle<DefaultProbabilityTermStructure>(ext::make_shared<
579 PiecewiseDefaultCurve<SurvivalProbability, LogLinear>>(
580 args: 0, args: WeekendsOnly(), args&: isdaCdsHelpers, args: Actual365Fixed()));
581
582 // set up isda engine
583 auto isdaPricer = ext::make_shared<IsdaCdsEngine>(args&: isdaCts, args: 0.4, args&: isdaYts);
584
585 // check the curves
586 std::cout << "ISDA yield curve:" << std::endl;
587 std::cout << "date;time;zeroyield" << std::endl;
588 for (auto& isdaYieldHelper : isdaYieldHelpers) {
589 Date d = isdaYieldHelper->latestDate();
590 Real t = isdaYts->timeFromReference(d);
591 std::cout << d << ";" << t << ";"
592 << isdaYts->zeroRate(d, resultDayCounter: Actual365Fixed(), comp: Continuous).rate()
593 << std::endl;
594 }
595
596 std::cout << "ISDA credit curve:" << std::endl;
597 std::cout << "date;time;survivalprob" << std::endl;
598 for (auto& isdaCdsHelper : isdaCdsHelpers) {
599 Date d = isdaCdsHelper->latestDate();
600 Real t = isdaCts->timeFromReference(d);
601 std::cout << d << ";" << t << ";" << isdaCts->survivalProbability(d)
602 << std::endl;
603 }
604}
605
606
607int main(int argc, char *argv[]) {
608
609 try {
610 Size example = 0;
611 if (argc == 2)
612 example = std::atoi(nptr: argv[1]);
613
614 if (example == 0 || example == 1) {
615 std::cout << "***** Running example #1 *****" << std::endl;
616 example01();
617 }
618
619 if (example == 0 || example == 2) {
620 std::cout << "***** Running example #2 *****" << std::endl;
621 example02();
622 }
623
624 if (example == 0 || example == 3) {
625 std::cout << "***** Running example #3 *****" << std::endl;
626 example03();
627 }
628
629 return 0;
630 }
631 catch (exception &e) {
632 cerr << e.what() << endl;
633 return 1;
634 }
635 catch (...) {
636 cerr << "unknown error" << endl;
637 return 1;
638 }
639}
640

source code of quantlib/Examples/CDS/CDS.cpp