# Would it possible to use quantlib finance library to calculate folowing measures?

I am working on a project which addresses finances and I am curious if I could use qunatlib for it. I have already looked it a documentation about it but since I am definitely not an expert in finances, could not conclude if the quantlib suits well for the measures I have.

For example, I am calculating coupon / dividend payment on top of a bond / stock on a certain day. Lets have a look at the coupon example. On the specified day a client has 10 Bonds (each has face amount of 100 USD and coupon rate of 7%), by the simple multiplication I calculate that the client gets 70 USD on that day.

As well as I am calculating book and market value of assets along with total and relative price return measures also on top of asset. I am curious of any of those would be possible to calculate by using qunatlib. I see that it is generally widely used and offers a hand full of features and the further motivation for this is the fact that I would not like to reinvent the wheel if there is a library which can do it for me.

If anyone could tell me if it is possible to calculate any of those measures using qunatlib and ideally provide a sample of the measurement it would be great. I'll appreciate any help.

• Are you saying you have 10 fixed-rate bonds with 7% fixed-rate, the reset dates are like monthly (or yearly, doesn't matter)? You want to get the NPV of your portfolio? You'll use those the NPV to calculate returns in investment (eg: relative to how much you've paid to buy the bonds)? – SmallChess Aug 25 '16 at 12:59
• If your answer to my previous comment is yes, QuantLib can easily manage it. I think I can write you some sample code. – SmallChess Aug 25 '16 at 13:01
• In the system I work on, there are fixed-rate, convertible as well as floating rate bonds but yes fixed-rate bonds could be taken as an example. Reset days, if the frequency is meant by reset dates, annual could be an example. A client has a portfolio (list of instruments) it can contain bonds as well as other instruments (i.e. stocks). Lets take bonds for showcase. I'm not sure how NPV could be used to calculate these measures and I would appreciate if you could write some samples and provide more info. Thx a lof for the reply and if you need more information from my side let me know. – amsalk Aug 25 '16 at 13:28
• What specific return measure are you interested? Yield? Yield-to-maturity? – SmallChess Aug 25 '16 at 13:39
• Well as I've said I'm definetly not an expert in this area. If I understood it correctly yield describes the amount of money that returns to the owner of an instrument (bond) and yield to maturity includes also the purchase price. If I am correct, both of them would be interesting. I hope that it isn't so much work. – amsalk Aug 25 '16 at 13:47

Is this sufficient for you? Creating a fixed-bond and it's clean price, dirty price and YTM.

const Date t0 = Date(8, July, 2015);    // July 8th, 2015
const Date t1 = Date(10, August, 2015); // August 10th, 2015
const Date t2 = t0 + Period(2, Months); // September 8th, 2015
const Date t3 = t0 + Period(3, Months); // October 8th, 2015

// Make sure we're evaluating at t0
Settings::instance().evaluationDate() = t0;

Schedule sch(t0,
t3,
Period(Monthly),
UnitedStates(),
DateGeneration::Backward,
false);

const auto dc = Actual360();

FixedRateBond bond(0, 1000, sch, std::vector<Rate> { 0.07, 0.07, 0.07 }, dc);

// We have four payments (three coupons + one nominal)
std::cout << bond.cashflows().size() << std::endl;

// Coupon for the first payment date
std::cout << bond.cashflows()->amount() << std::endl;

// Coupon for the second payment date
std::cout << bond.cashflows()->amount() << std::endl;

// Coupon for the final payment date
std::cout << bond.cashflows()->amount() << std::endl;

// Nominal at the end of the bond
std::cout << bond.cashflows()->amount() << std::endl;

Handle<YieldTermStructure> zero(flatRate(t0, 0.05, Actual360()));

boost::shared_ptr<PricingEngine> engine = boost::shared_ptr<PricingEngine>(
new DiscountingBondEngine(Handle<YieldTermStructure>(zero)));
bond.setPricingEngine(engine);

std::cout << bond.dirtyPrice() << std::endl;
std::cout << bond.cleanPrice() << std::endl;
std::cout << bond.yield(dc, Simple, Monthly) << std::endl;

• First of all, thank you very much for the answer. It is definitely helpful. I've executed the code with some values from the db and it looks fine. Anyway there is something which I still don't understand correctly and that's rate array. So, why are there exactly three coupons (0.07) in the array? Shouldn't just one be enough? – amsalk Aug 26 '16 at 11:38
• @amsalk It's possible for each coupon to have a different coupon rate. In QuantLib, this is done by giving a std::vector. In this case, we have three coupons, so we have three values. If your input has a different size, QuantLib will crash and report an error message. (Try it!) – SmallChess Aug 26 '16 at 11:39