Login  Register

IborIndex and valuation of swaps

Posted by dhoorens on Jul 14, 2009; 11:11am
URL: http://quantlib.414.s1.nabble.com/IborIndex-and-valuation-of-swaps-tp7810.html

Hi
Is there an easy way to determine the floating rate of a swap instead of specifying an ibor Index?
I try to make my computations with NullCalendar and SimpleDayCounter and I face the problems that often my settlement is on sunday => error..


What I tried (to give as input my floating rate) is the following, but I don't know if it is
* efficient
* robust

I think my problem is in the 6 last lines of this code

boost::shared_ptr<QuantLib::VanillaSwap> toVanillaSwap( char* type,
                                                                                                                double nominal,
                                                                                                                QuantLib::Date issueDate,
                                                                                                                QuantLib::Date maturityDate,
                                                                                                                QuantLib::Calendar calendar,
                                                                                                                QuantLib::DayCounter dayCounter,
                                                                                                                double spread,
                                                                                                                double fixRate,
                                                                                                                QuantLib::Frequency fixFrequency,
                                                                                                                QuantLib::Calendar fixCalendar,
                                                                                                                QuantLib::DayCounter fixDayCounter,
                                                                                                                double fltRate,
                                                                                                                int fltIndexTenor,
                                                                                                                QuantLib::Frequency fltFrequency,
                                                                                                                QuantLib::Calendar fltCalendar,
                                                                                                                QuantLib::DayCounter fltDayCounter,
                                                                                                                QuantLib::Handle<QuantLib::YieldTermStructure> yTS){

        BusinessDayConvention fixConvention = Unadjusted;
        BusinessDayConvention fixTerminationDateConvention = Unadjusted;
        DateGeneration::Rule fixRule = DateGeneration::Backward;
        bool fixEndOfMonth = false;

        BusinessDayConvention fltConvention = Unadjusted;
        BusinessDayConvention fltTerminationDateConvention = Unadjusted;
        DateGeneration::Rule fltRule = DateGeneration::Backward;
        bool fltEndOfMonth = false;


        Schedule fixSchedule(issueDate,
                                                 maturityDate,
                                                 Period(fixFrequency),
                                                 fixCalendar,
                                                 fixConvention,
                                                 fixTerminationDateConvention,
                                                 fixRule,
                                                 fixEndOfMonth);
        Schedule fltSchedule(issueDate,
                                                 maturityDate,
                                                 Period(fltFrequency),
                                                 fltCalendar,
                                                 fltConvention,
                                                 fltTerminationDateConvention,
                                                 fltRule,
                                                 fltEndOfMonth);

        std::string familyName = "";
        Period tenor (fltIndexTenor, Months);
        int settlementDays = 0;
        Currency currency = QuantLib::EURCurrency();
        Calendar fixingCalendar = fltCalendar;
        BusinessDayConvention convention = Unadjusted;
        bool endOfMonth = false;



        boost::shared_ptr<IborIndex> iborIndex ;
        iborIndex =  boost::shared_ptr<IborIndex>(
                new IborIndex(familyName,
                                          tenor,
                                          settlementDays,
                                          currency,
                                          fixingCalendar,
                                          convention,
                                          endOfMonth,
                                          fltDayCounter,
                                          yTS));

        std::string s(type);
        s = s.substr(0,1);
        std::transform(s.begin(), s.end(), s.begin(), ::toupper);

        QuantLib::VanillaSwap::Type typ;
        if (s == "R")
                typ = QuantLib::VanillaSwap::Receiver;
        else if (s == "P")
                typ = QuantLib::VanillaSwap::Payer;
        else
                throw (std::string("unknwown type of Swap"));

        boost::shared_ptr<VanillaSwap>sw;
        sw = boost::shared_ptr<VanillaSwap>(
                new VanillaSwap(typ,
                                                nominal,
                                                fixSchedule,
                                                fixRate,
                                                fixDayCounter,
                                                fltSchedule,
                                                iborIndex,
                                                spread,
                                                fltDayCounter));



        boost::shared_ptr<YieldTermStructure> yts =yTS.currentLink();
        Date refDate = yts->referenceDate();

        Date prevFixDate = (*(CashFlows::previousCashFlow(sw->floatingLeg(), refDate)))->date();
        if(!iborIndex->isValidFixingDate(prevFixDate))
                prevFixDate = fltCalendar.adjust(prevFixDate, Preceding);
        iborIndex->addFixing(prevFixDate, fltRate, true);

        return sw;


}