This example shows how to calculate credit value adjustment for an interest rate swap.
#if !defined(BOOST_ALL_NO_LIB) && defined(BOOST_MSVC)
#endif
#include <iostream>
#include <iomanip>
int main(int, char* []) {
try {
std::cout << std::endl;
Date todaysDate(10, March, 2004);
todaysDate = calendar.
adjust(todaysDate);
Settings::instance().evaluationDate() = todaysDate;
auto yieldIndx = ext::make_shared<Euribor3M>();
Size tenorsSwapMkt[] = {5, 10, 15, 20, 25, 30};
Rate ratesSwapmkt[] = {.03249, .04074, .04463, .04675, .04775, .04811};
vector<ext::shared_ptr<RateHelper>> swapHelpers;
for(
Size i=0; i<
sizeof(tenorsSwapMkt)/
sizeof(
Size); i++)
swapHelpers.push_back(ext::make_shared<SwapRateHelper>(
makeQuoteHandle(ratesSwapmkt[i]),
tenorsSwapMkt[i] * Years,
Quarterly,
ModifiedFollowing,
yieldIndx));
auto swapTS = ext::make_shared<PiecewiseYieldCurve<Discount, LogLinear>>(
swapTS->enableExtrapolation();
auto riskFreeEngine = ext::make_shared<DiscountingSwapEngine>(
std::vector<Handle<DefaultProbabilityTermStructure>>
defaultIntensityTS;
Size defaultTenors[] = {0, 12, 36, 60, 84, 120, 180, 240, 300,
360};
Real intensitiesLow[] = {0.0036, 0.0036, 0.0065, 0.0099, 0.0111,
0.0177, 0.0177, 0.0177, 0.0177, 0.0177,
0.0177};
Real intensitiesMedium[] = {0.0202, 0.0202, 0.0231, 0.0266, 0.0278,
0.0349, 0.0349, 0.0349, 0.0349, 0.0349,
0.0349};
Real intensitiesHigh[] = {0.0534, 0.0534, 0.0564, 0.06, 0.0614, 0.0696,
0.0696, 0.0696, 0.0696, 0.0696, 0.0696};
Real ctptyRRLow = 0.4, ctptyRRMedium = 0.35, ctptyRRHigh = 0.3;
std::vector<Date> defaultTSDates;
std::vector<Real> intesitiesVLow, intesitiesVMedium, intesitiesVHigh;
for(
Size i=0; i<
sizeof(defaultTenors)/
sizeof(
Size); i++) {
Period(defaultTenors[i], Months)));
intesitiesVLow.push_back(intensitiesLow[i]);
intesitiesVMedium.push_back(intensitiesMedium[i]);
intesitiesVHigh.push_back(intensitiesHigh[i]);
}
defaultIntensityTS.emplace_back(
defaultIntensityTS.emplace_back(
defaultIntensityTS.emplace_back(
auto ctptySwapCvaLow = ext::make_shared<CounterpartyAdjSwapEngine>(
blackVol,
defaultIntensityTS[0],
ctptyRRLow);
auto ctptySwapCvaMedium = ext::make_shared<CounterpartyAdjSwapEngine>(
blackVol,
defaultIntensityTS[1],
ctptyRRMedium);
auto ctptySwapCvaHigh = ext::make_shared<CounterpartyAdjSwapEngine>(
blackVol,
defaultIntensityTS[2],
ctptyRRHigh);
defaultIntensityTS[0]->enableExtrapolation();
defaultIntensityTS[1]->enableExtrapolation();
defaultIntensityTS[2]->enableExtrapolation();
std::vector<VanillaSwap> riskySwaps;
for(
Size i=0; i<
sizeof(tenorsSwapMkt)/
sizeof(
Size); i++)
yieldIndxS,
ratesSwapmkt[i],
0*Days)
cout << "-- Correction in the contract fix rate in bp --" << endl;
for(
Size i=0; i<riskySwaps.size(); i++) {
cout << fixed << setprecision(3);
cout << setw(4);
riskySwaps[i].setPricingEngine(riskFreeEngine);
Real nonRiskyFair = riskySwaps[i].fairRate();
cout << tenorsSwapMkt[i];
cout << setw(5);
cout << " | " << io::rate(nonRiskyFair);
cout << fixed << setprecision(2);
cout << setw(5);
riskySwaps[i].setPricingEngine(ctptySwapCvaLow);
cout << " | " << setw(6)
<< 10000.*(riskySwaps[i].fairRate() - nonRiskyFair);
riskySwaps[i].setPricingEngine(ctptySwapCvaMedium);
cout << " | " << setw(6)
<< 10000.*(riskySwaps[i].fairRate() - nonRiskyFair);
riskySwaps[i].setPricingEngine(ctptySwapCvaHigh);
cout << " | " << setw(6)
<< 10000.*(riskySwaps[i].fairRate() - nonRiskyFair);
cout << endl;
}
cout << endl;
return 0;
} catch (exception& e) {
cerr << e.what() << endl;
return 1;
} catch (...) {
cerr << "unknown error" << endl;
return 1;
}
}
Actual/360 day count convention.
Date adjust(const Date &, BusinessDayConvention convention=Following) const
Date advance(const Date &, Integer n, TimeUnit unit, BusinessDayConvention convention=Following, bool endOfMonth=false) const
Shared handle to an observable.
DefaultProbabilityTermStructure based on interpolation of hazard rates.
MakeVanillaSwap & withFixedLegConvention(BusinessDayConvention bdc)
MakeVanillaSwap & withFixedLegTerminationDateConvention(BusinessDayConvention bdc)
MakeVanillaSwap & withFixedLegCalendar(const Calendar &cal)
MakeVanillaSwap & withSettlementDays(Natural settlementDays)
MakeVanillaSwap & withFixedLegDayCount(const DayCounter &dc)
MakeVanillaSwap & withNominal(Real n)
MakeVanillaSwap & withFloatingLegCalendar(const Calendar &cal)
MakeVanillaSwap & withType(Swap::Type type)
MakeVanillaSwap & withFixedLegTenor(const Period &t)
Frequency
Frequency of events.
BusinessDayConvention
Business Day conventions.
Real Volatility
volatility
std::size_t Size
size of a container
interpolated hazard-rate term structure
Helper class to instantiate standard market swaps.
piecewise-interpolated term structure
Global definitions and compiler switches.
deposit, FRA, futures, and various swap rate helpers
Simple fixed-rate vs Libor swap.