First I'll go quickly through building the bond, copying some code from the bonds example in the QuantLib release (also, disclaimer: I haven't tried compiling the code below). For more details on the classes involved, see the QuantLib documentation.
Let's assume annual payments as you said:
Schedule schedule(startDate, maturityDate, Period(Annual),
calendar, convention, convention,
DateGeneration::Backward, true);
and for illustration, let's assume we'll use an USD Libor index. Its future fixing are forecast based on an interest-rate term structure we'll set later.
RelinkableHandle<YieldTermStructure> liborTermStructure;
boost::shared_ptr<IborIndex> libor(
new USDLibor(Period(1,Years),liborTermStructure));
Now build the bond, adding the margin as a spread on the LIBOR rate:
FloatingRateBond bond(settlementDays, faceAmount,
schedule, libor, dayCounter,
convention, fixingDays,
// gearings
std::vector<Real>(1, 1.0),
// spreads
std::vector<Rate>(1, 0.001));
Now to get the coupons you want, you'll just set the corresponding market data. To set the rate of the first cash flow, store the past fixing of the Libor index:
libor->addFixing(resetDate, 0.01);
And to set the future cash flows, create a flat interest-rate curve with the desired rate (minding the conventions so that they match those of the Libor index):
boost::shared_ptr<YieldTermStructure> flatRate(
new FlatForward(today, 0.01, dayCounter, Simple, Annual));
liborTermStructure.linkTo(flatRate);
(You're not necessarily limited to a flat rate; if you can bootstrap a Libor curve, you can use that one to get realistic estimates for future coupons.)
At this point, you should be able to extract the bond coupons and check that they're as expected:
std::vector<boost::shared_ptr<CashFlow> > cashflows = bond.cashflows();
for (std::size_t i=0; i < cashflows.size(); ++i)
std::cout << cashflows[i]->date() << " "
<< cashflows[i]->amount() << "\n";
If you also want to call methods such as bond.cleanPrice()
, you'll need to tell the bond how to discount the cash flows:
RelinkableHandle<YieldTermStructure> discountingTermStructure;
boost::shared_ptr<PricingEngine> bondEngine(
new DiscountingBondEngine(discountingTermStructure));
bond.setPricingEngine(bondEngine);
You can use for discounting the same curve you're using for forecasting...
discountingTermStructure.linkTo(flatRate);
...or create and use a different one.