Calculating a CDS

With all the mechanics of message-publishing and subscribing out of the way, let's talk about the actual CDS calculation. Our quant library does an excellent job of abstracting the many details away from us when it comes to quantitative finance. All we have to do is decide what we want and call the proper functions. This makes our microservice very business-specific, which is one of our key objectives. Since this is not a book on quantitative finance, I will not delve into the details of what is happening here; the important message is that you don't care. To you, it's a white or black box, the same as any other third-party toolkit that you use. Sometimes you have the source and know what's going on under the hood (in this case you do), and sometimes you don't.

The following is the code for calculating a CDS:

public bool CalcCDS(ref CreditDefaultSwapRequestMessage msg, double fixedRate, double notional, double recoveryRate)
{
// Testing fair-spread calculation for credit-default swaps...
using (SavedSettings backup = new SavedSettings())
{
// Initialize curves
Calendar calendar = new TARGET();
Date today = calendar.adjust(Date.Today);
Settings.setEvaluationDate(today);
Handle<Quote> hazardRate = new Handle<Quote>(new SimpleQuote(0.01234));
RelinkableHandle<DefaultProbabilityTermStructure> probabilityCurve = new RelinkableHandle<DefaultProbabilityTermStructure>();
probabilityCurve.linkTo(new FlatHazardRate(0, calendar, hazardRate, new Actual360()));
RelinkableHandle<YieldTermStructure> discountCurve =
new RelinkableHandle<YieldTermStructure>();
discountCurve.linkTo(new FlatForward(today, 0.06, new Actual360()));
// Build the schedule
Date issueDate = calendar.advance(today, -1, TimeUnit.Years);
Date maturity = calendar.advance(issueDate, 10, TimeUnit.Years);
BusinessDayConvention convention = BusinessDayConvention.Following;
Schedule schedule = new MakeSchedule().from(issueDate)
.to(maturity)
.withFrequency(Frequency.Quarterly)
.withCalendar(calendar)
.withTerminationDateConvention(convention)
.withRule(DateGeneration.Rule.TwentiethIMM).value();
// Build the CDS
DayCounter dayCount = new Actual360();
IPricingEngine engine = new MidPointCdsEngine(probabilityCurve, recoveryRate, discountCurve);
CreditDefaultSwap cds = new CreditDefaultSwap(Protection.Side.Seller, notional, fixedRate,
schedule, convention, dayCount, true, true);
cds.setPricingEngine(engine);
double fairRate = cds.fairSpread();
CreditDefaultSwap fairCds = new CreditDefaultSwap(Protection.Side.Seller, notional, fairRate,
schedule, convention, dayCount, true, true);
fairCds.setPricingEngine(engine);
double fairNPV = fairCds.NPV();
double tolerance = 1e-10;
msg.fairRate = fairRate;
msg.fairNPV = fairNPV;
return (Math.Abs(fairNPV) <= tolerance);
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset