|
From: Dima <dim...@go...> - 2010-09-17 09:41:37
|
I have some code which might help:
boost::shared_ptr<YieldTermStructure> curvePtr=getBondYieldCurve();
// this function constructs the yield curve
Handle<YieldTermStructure> curve(curvePtr);
Natural settlementDays = 1;
Calendar cal=UnitedStates(UnitedStates::GovernmentBond);
Real faceAmount = 100.0;
Date today(14,Sep,2009);
Settings::instance().evaluationDate() = today;
Date settlementDate=cal.advance(today,settlementDays,Days);
Date maturityDate(15,Nov,2017);
DayCounter dc=ActualActual(ActualActual::Bond);
Date issueDate(15,Nov,2007);
BusinessDayConvention bdc=Unadjusted;
Schedule bondSchedule=MakeSchedule(issueDate,maturityDate,
Period(Semiannual), cal, bdc).backwards();
std::vector<Date> couponDates=bondSchedule.dates();
FixedRateBond fixedRateBond(
settlementDays,
faceAmount,
bondSchedule,
std::vector<Rate>(1, 0.0425),
dc,bdc,faceAmount,issueDate);
boost::shared_ptr<PricingEngine> bondEngine(new
DiscountingBondEngine(curve));
fixedRateBond.setPricingEngine(bondEngine);
std::cout << "Clean Price:"<< fixedRateBond.cleanPrice() << std::endl;
std::cout << "Dirty Price:"<< fixedRateBond.dirtyPrice() << std::endl;
Real accruedInterest= fixedRateBond.dirtyPrice()-
fixedRateBond.cleanPrice();
std::cout << "Accrued Interest:" << accruedInterest << std::endl;
Rate yield=fixedRateBond.yield(dc,Compounded,Semiannual);
InterestRate yieldRate=InterestRate(yield,dc,Compounded,Semiannual);
std::cout << "Bond Yield:"<< yield << std::endl;
Real
durationMod=CashFlows::duration(fixedRateBond.cashflows(),yieldRate,
Duration::Modified);
std::cout << "Duration:" << - durationMod << std::endl;
std::cout << "BPV:" <<
CashFlows::basisPointValue(fixedRateBond.cashflows(),yieldRate)*100 <<
std::endl;
Real
convexity=CashFlows::convexity(fixedRateBond.cashflows(),yieldRate);
std::cout << "Convexity:" << convexity << std::endl;
Luigi Ballabio schrieb:
> On Wed, 2010-09-15 at 18:26 -0700, mdp788 wrote:
>
>> I am new to Quantlib, and using the Java binding. I am interested in YTM,
>> and duration calculation of Fixed as well floating rate coupon bond.
>> Unfortunately I can't find any examples or docs on how to do so. Could you
>> please point me to where I can find some example, or some other reference
>> documents on how to do what I stated earlier.
>>
>
> Unfortunately, I have no example ready; however, once you have
> instantiated your bond, you can extract its cashflows (by calling
> bond.cashflows()) and pass them to the yield(...) and duration(...)
> static methods of the CashFlows class.
>
> Luigi
>
>
>
|