Guest User

Untitled

a guest
Jan 22nd, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.49 KB | None | 0 0
  1. #include <ql/qldefines.hpp>
  2. #include <ql/time/date.hpp>
  3. #include <ql/instruments/creditdefaultswap.hpp>
  4. #include <ql/pricingengines/credit/isdacdsengine.hpp>
  5. #include <ql/termstructures/credit/piecewisedefaultcurve.hpp>
  6. #include <ql/termstructures/credit/defaultprobabilityhelpers.hpp>
  7. #include <ql/termstructures/yield/piecewiseyieldcurve.hpp>
  8. #include <ql/termstructures/yield/ratehelpers.hpp>
  9. #include <ql/time/calendars/weekendsonly.hpp>
  10. #include <ql/time/daycounters/thirty360.hpp>
  11. #include <ql/time/daycounters/actual365fixed.hpp>
  12. #include <ql/time/daycounters/actual360.hpp>
  13. #include <ql/currencies/america.hpp>
  14. #include <ql/quotes/simplequote.hpp>
  15. #include <boost/make_shared.hpp>
  16. #include <iostream>
  17. #include <iomanip>
  18.  
  19. using namespace QuantLib;
  20.  
  21. void example() {
  22.  
  23. // this is the example from Apdx E in pricing and risk management of CDS, OpenGamma
  24.  
  25. Date tradeDate(29, December, 2017);
  26.  
  27. Settings::instance().evaluationDate() = tradeDate;
  28. std::vector<double> depositRates = {0.015678, 0.016219, 0.016947,
  29. 0.018436, 0.021063};
  30. std::vector<int> tenors = {1, 2, 3, 6, 12};
  31. std::vector<boost::shared_ptr<RateHelper> > isdaYieldHelpers;
  32. int i = 0;
  33. for(auto r: depositRates) {
  34. isdaYieldHelpers.push_back(
  35. boost::make_shared<DepositRateHelper>(r, tenors[i] * Months, 2,
  36. WeekendsOnly(), ModifiedFollowing,
  37. false, Actual360()));
  38. i++;
  39. }
  40.  
  41. // this index is probably not important since we are not using
  42. // QL_USE_INDEXED_COUPON - define it "isda compliant" anyway
  43. boost::shared_ptr<IborIndex> libor3m = boost::make_shared<IborIndex>(
  44. "IsdaIbor", 3 * Months, 2, USDCurrency(), WeekendsOnly(),
  45. ModifiedFollowing, false, Actual360());
  46.  
  47. std::vector<double> swapRates = {0.020825, 0.02176, 0.02227,
  48. 0.022625, 0.02298, 0.02335, 0.023555,
  49. 0.02385, 0.024145, 0.02452, 0.025025,
  50. 0.02536, 0.025455, 0.02546};
  51. std::vector<int> swapTenors = {2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 15, 20, 25, 30};
  52. i = 0;
  53. for(auto r: swapRates) {
  54. isdaYieldHelpers.push_back(boost::make_shared<SwapRateHelper>(
  55. r, swapTenors[i] * Years, WeekendsOnly(), Semiannual,
  56. ModifiedFollowing, Thirty360(), libor3m));
  57. i++;
  58. }
  59.  
  60. // build yield curve
  61. Handle<YieldTermStructure> isdaYts = Handle<YieldTermStructure>(
  62. boost::make_shared<PiecewiseYieldCurve<Discount, LogLinear> >(
  63. 0, WeekendsOnly(), isdaYieldHelpers, Actual365Fixed()));
  64. isdaYts->enableExtrapolation();
  65.  
  66.  
  67. CreditDefaultSwap::PricingModel model = CreditDefaultSwap::ISDA;
  68. boost::shared_ptr<CdsHelper> cds6m(new SpreadCdsHelper(
  69. 0.0248, 6 * Months, 1, WeekendsOnly(), Quarterly, Following,
  70. DateGeneration::CDS2015, Actual360(), 0.4, isdaYts, true, true, Date(),
  71. Actual360(true), true, model));
  72. boost::shared_ptr<CdsHelper> cds5y(new SpreadCdsHelper(
  73. 0.0248, 5 * Years, 1, WeekendsOnly(), Quarterly, Following,
  74. DateGeneration::CDS2015, Actual360(), 0.4, isdaYts, true, true, Date(),
  75. Actual360(true), true, model));
  76. std::vector<boost::shared_ptr<DefaultProbabilityHelper> > isdaCdsHelpers;
  77.  
  78. //isdaCdsHelpers.push_back(cds6m);
  79. isdaCdsHelpers.push_back(cds5y);
  80.  
  81. // build credit curve
  82. Handle<DefaultProbabilityTermStructure> isdaCts =
  83. Handle<DefaultProbabilityTermStructure>(boost::make_shared<
  84. PiecewiseDefaultCurve<SurvivalProbability, LogLinear> >(
  85. 0, WeekendsOnly(), isdaCdsHelpers, Actual365Fixed(), 1e-12));
  86.  
  87. // set up isda engine
  88. boost::shared_ptr<IsdaCdsEngine> isdaPricer =
  89. boost::make_shared<IsdaCdsEngine>(
  90. isdaCts, 0.4, isdaYts);
  91.  
  92. // check the curves
  93. std::cout << "ISDA yield curve:" << std::endl;
  94. std::cout << "date;time;zeroyield" << std::endl;
  95. for (Size i = 0; i < isdaYieldHelpers.size(); i++) {
  96. Date d = isdaYieldHelpers[i]->latestDate();
  97. Real t = isdaYts->timeFromReference(d);
  98. std::cout << d << ";" << t << ";"
  99. << isdaYts->zeroRate(d, Actual365Fixed(), Continuous).rate()
  100. << std::endl;
  101. }
  102.  
  103. std::cout << "ISDA credit curve:" << std::endl;
  104. std::cout << "date;time;survivalprob" << std::endl;
  105. for (Size i = 0; i < isdaCdsHelpers.size(); i++) {
  106. Date d = isdaCdsHelpers[i]->latestDate();
  107. Real t = isdaCts->timeFromReference(d);
  108. std::cout << d << ";" << t << ";" << isdaCts->survivalProbability(d)
  109. << std::endl;
  110. }
  111. Schedule cdsSchedule(tradeDate + 1, Date(20, December, 2022), 3 * Months, WeekendsOnly(), Following,
  112. Unadjusted, DateGeneration::CDS, false, Date(), Date());
  113.  
  114. Real nominal = 10000000;
  115. Date upfrontPaymentDate = WeekendsOnly().advance(tradeDate, 3 * Days);
  116. CreditDefaultSwap trade(Protection::Buyer, nominal, 0., 0.01,
  117. cdsSchedule, Following, Actual360(), true, true,
  118. tradeDate + 1, upfrontPaymentDate,
  119. boost::shared_ptr<Claim>(),
  120. Actual360(true));
  121. trade.setPricingEngine(isdaPricer);
  122. std::cout << trade.fairUpfront() * nominal << std::endl;
  123. std::cout << trade.NPV() / isdaYts->discount(upfrontPaymentDate) << std::endl;
  124. }
  125.  
  126. int main() {
  127. example();
  128. return 0;
  129. }
Add Comment
Please, Sign In to add comment