I had to perform some research on bond curves fitting methods recently, thus looking for a decent library to do some bond pricing (vanilla stuff: fixed coupon bonds …), and calibrate an OLS of some model prices against market contributions.

I think that QuantLib stands as the open source reference for that kind of calculations, but since I was keener to do some quick implementation in C#, and expose the results in Excel (with Excel-DNA), I turned to QLNet, which is a direct conversion of the famous C++ library into C#.Net. I read on Wilmott forums that QLNet was somewhat outdated, but there are still some recent commits on their GitHub project actually and it is, I think, a good alternative to doing C++ to C++/CLI to C# plumbing (if find the courage I’ll do it or maybe just use XLW to expose the C++ to Excel directly).

In this post I share some C# code useful to compute some bond analytics out of a discount curve, which seems badly covered in Quantlib and QLNet examples. Indeed, in the examples, and test suite, bonds are often evaluated from yields, or flat rate models. Here I wanted to calibrate a Nelson Siegel Svensson so I needed to take into account the slope of my discount curve.

Installation of QLNet and Excel-DNA in our C# class library is straightforward using Nugget package manager:

Excel-DNA Nugget Installation




QLNet Nugget Installation



A very convenient aspect of having the Excel-DNA dependency installed locally at project level is that it’s naturally referenced, and at compile time, it automatically generates the 32 and 64-bit xll.

The function below is quite rough, and doesn’t enable much customization of cashflows, but it’s a good basis to move forward for further adjustments (additional inputs, outputs …).

The logic to create a piecewise zero coupon rate model is encapsulated in the following method, for more clarity:

The result:

Bond Pricing QLNet Excel-DNA

Attached an excel spreadsheet (NSS.xlsm) with a test that reconciles the calculated zero coupon and the original discount curve. In addition, you will find the project source files, .csproj, and  the generated XLL for 32 and 64-bit Excel.