Bonds Helper class...

Posted by Toyin Akin on
URL: http://quantlib.414.s1.nabble.com/Bonds-Helper-class-tp3959.html

Hi guys,

What do you guys think of this first implementation of a Bonds Helper class
in order to strip a bonds curve.
(Deposits+Futures+Bonds)

I'm not too sure whether both the issueDate and datedDate are needed.

What exactly is the dated date?

Header file :

#############################################################################################

#ifndef quantlib_bondhelper_hpp
#define quantlib_bondhelper_hpp

#include <ql/Instruments/FixedCouponBond.hpp>
#include <ql/TermStructures/ratehelpers.hpp>

namespace QuantLib
{
    class BondHelper : public RateHelper
        {
      public:

        BondHelper(const Handle<Quote>& Price,
                const Date& issueDate,
                        const Date& datedDate,
                        const Date& maturityDate,
                        Integer settlementDays,
                        const std::vector<Rate>& coupons,
                        Frequency couponFrequency,
                        const DayCounter& dayCounter,
                        const Calendar& calendar,
                        BusinessDayConvention convention = Following,
                        Real redemption = 100.0,
                        const Date& stub = Date(),
                        bool fromEnd = true);

        Real impliedQuote() const;
        // implementing discountGuess() is not worthwhile,
        // and may not avoid the root-finding process
        Date latestDate() const;
        void setTermStructure(YieldTermStructure*);

      protected:

        Integer settlementDays_;
        Calendar calendar_;
        BusinessDayConvention businessDayConvention_;
        DayCounter dayCount_;

        Date stub_;
        bool fromEnd_;
        std::vector<Rate> coupons_;

        Date issueDate_, datedDate_, maturityDate_;
        Frequency frequency_;
        Real redemption_;
        Date settlement_, latestDate_;

        boost::shared_ptr<FixedCouponBond> bond_;
        Handle<YieldTermStructure> termStructureHandle_;
    };
}


#endif

#############################################################################################


cpp file :

#############################################################################################

#include <ql/TermStructures/BondHelper.hpp>

namespace QuantLib
{

    namespace
        {
        void no_deletion(YieldTermStructure*) {}
    }

    BondHelper::BondHelper(const Handle<Quote>& Price,
                             const Date& issueDate,
                             const Date& datedDate,
                             const Date& maturityDate,
                             Integer settlementDays,
                             const std::vector<Rate>& coupons,
                             Frequency couponFrequency,
                             const DayCounter& dayCounter,
                             const Calendar& calendar,
                             BusinessDayConvention convention,
                             Real redemption,
                             const Date& stub, bool fromEnd)
        : RateHelper(Price)
        {

                coupons_ = coupons;
                settlementDays_ = settlementDays;
                businessDayConvention_ = convention;
                calendar_ = calendar;
                dayCount_ = dayCounter;

                stub_ = stub;
                fromEnd_ = fromEnd;

                issueDate_ = issueDate;
        datedDate_ = datedDate;
                maturityDate_ = maturityDate;
        frequency_ = couponFrequency;
                redemption_ = redemption;

                registerWith(Settings::instance().evaluationDate());

    }



    void BondHelper::setTermStructure(YieldTermStructure* t)
        {
        // do not set the relinkable handle as an observer -
        // force recalculation when needed
        termStructureHandle_.linkTo(
                         
boost::shared_ptr<YieldTermStructure>(t,no_deletion),
                         false);

        RateHelper::setTermStructure(t);
        Date today = Settings::instance().evaluationDate();
        settlement_ = calendar_.advance(today,settlementDays_,Days);

        bond_ = boost::shared_ptr<FixedCouponBond>(
                   new FixedCouponBond(issueDate_, datedDate_,
maturityDate_,
                                  settlementDays_, coupons_,
                                  frequency_, dayCount_, calendar_,
businessDayConvention_,
                                  redemption_, termStructureHandle_,
                                                                  stub_, fromEnd_));

        latestDate_ = maturityDate_;
    }

    Date BondHelper::latestDate() const
        {
        QL_REQUIRE(termStructure_ != 0, "null term structure set");
        return latestDate_;
    }

    Real BondHelper::impliedQuote() const
        {
        QL_REQUIRE(termStructure_ != 0, "term structure not set");
        // we didn't register as observers - force calculation
        bond_->recalculate();
        return bond_->NPV();
    }
}


#############################################################################################

If you find it useful, can you merge this into the Quantlib source?

Best Regards,
Toy out.