Using QuantLib to Generate Bond Cash Flows and Calculate Prices and Yield
Summary
The answer demonstrates how a fixed-rate bond can be represented in QuantLib with an evaluation date, payment schedule, day-count convention, face amount, and coupon rates. The resulting cash-flow list contains coupon payments and the final principal repayment, and individual payment amounts can be queried. This offers a framework for calculating scheduled bond income rather than relying only on a simple coupon multiplication, which may omit schedule and day-count details.
The example also attaches a flat yield curve and a discounting pricing engine, then requests dirty price, clean price, and yield to maturity. It illustrates basic bond cash-flow and valuation capabilities, but it is a short illustrative setup rather than a general treatment of dividends, book value, market value for all assets, or total and relative returns. Actual results depend on dates, conventions, schedules, and market inputs chosen for a bond.
Key ideas
- A bond schedule and coupon terms can be used to generate payment cash flows.
- QuantLib cash-flow objects expose coupon amounts and principal repayment amounts.
- A discounting engine with a yield curve supports dirty price, clean price, and yield calculations.
- Bond valuation outputs depend on the evaluation date, day-count convention, schedule, and curve assumptions.
Tags
Full text
# Would it possible to use quantlib finance library to calculate folowing measures?
# 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.
Thx in advance
## Answer by SmallChess (score 3, accepted)
https://quant.stackexchange.com/a/29847
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(),
Unadjusted,
Unadjusted,
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()[0]->amount() << std::endl;
// Coupon for the second payment date
std::cout << bond.cashflows()[1]->amount() << std::endl;
// Coupon for the final payment date
std::cout << bond.cashflows()[2]->amount() << std::endl;
// Nominal at the end of the bond
std::cout << bond.cashflows()[3]->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;
```Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.