Pricing a vanilla swap with a given zero curve instead of boostrapping

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Pricing a vanilla swap with a given zero curve instead of boostrapping

semiparametric
This post was updated on .
Dear All:

I am a newbie to quantlib. I run below code to price a vanilla swap with a given zero curve. The code can be complied smoothly in VS2010, however, when I tried to run the programme, something went wrong, and the NPV couldn't be generated. Could anyone give me a hint on how to revise the code? Thanks in advance.


#include <fstream>
#include <vector>
#include <ql/quantlib.hpp>

using namespace QuantLib;

#if defined(QL_ENABLE_SESSIONS)
namespace QuantLib {

    Integer sessionId() { return 0; }

}
#endif

int main(int, char*[])

{       /////////////////////////////////// Settings  ///////////////////////////////

        Calendar calendar = TARGET();
        Date settlementDate(15, October, 2013);
        // must be a business day
        settlementDate = calendar.adjust(settlementDate);

        Integer fixingDays = 0;
        Date todaysDate = calendar.advance(settlementDate, -fixingDays, Days);
        // nothing to do with Date::todaysDate
        Settings::instance().evaluationDate() = todaysDate;
 

        // LIBOR Curve

        std::vector<Date> dates;                                    std::vector<Rate> rates;
        dates.push_back(settlementDate);                            rates.push_back(0.26/100.0);
        dates.push_back(calendar.advance(settlementDate,3,Months)); rates.push_back(0.26/100.0);
        dates.push_back(calendar.advance(settlementDate,6,Months)); rates.push_back(0.412/100.0);
                dates.push_back(calendar.advance(settlementDate,9,Months)); rates.push_back(0.572/100.0);
        dates.push_back(calendar.advance(settlementDate,1,Years)); rates.push_back(0.735/100.0);
        dates.push_back(calendar.advance(settlementDate,2,Years)); rates.push_back(0.715/100.0);
        dates.push_back(calendar.advance(settlementDate,3,Years)); rates.push_back(1.173/100.0);
        dates.push_back(calendar.advance(settlementDate,4,Years)); rates.push_back(1.621/100.0);
        dates.push_back(calendar.advance(settlementDate,5,Years)); rates.push_back(2.027/100.0);
        dates.push_back(calendar.advance(settlementDate,6,Years)); rates.push_back(2.372/100.0);
        dates.push_back(calendar.advance(settlementDate,7,Years)); rates.push_back(2.654/100.0);
        dates.push_back(calendar.advance(settlementDate,8,Years)); rates.push_back(2.884/100.0);
        dates.push_back(calendar.advance(settlementDate,9,Years)); rates.push_back(3.072/100.0);
        dates.push_back(calendar.advance(settlementDate,10,Years)); rates.push_back(3.227/100.0);

                //daycounter for the curve inputed.
            DayCounter dayCounter = Actual360();
        boost::shared_ptr<YieldTermStructure> LiborCurvePtr(
                new InterpolatedZeroCurve<LogLinear>(dates, rates, dayCounter));
               
               
                Handle<YieldTermStructure> liborTS(LiborCurvePtr);
        boost::shared_ptr<IborIndex> index(
                                                       new USDLibor(Period(3, Months), liborTS));
                       
        /*********************
        * SWAPS TO BE PRICED *
        **********************/

        // constant nominal 1,000,000 Euro
        Real nominal = 1000000.0;
        // fixed leg
        Frequency fixedLegFrequency =Quarterly;
        BusinessDayConvention fixedLegConvention = Unadjusted;
        BusinessDayConvention floatingLegConvention = ModifiedFollowing;
        DayCounter fixedLegDayCounter = Thirty360(Thirty360::European);
        Rate fixedRate = 0.04;
        DayCounter floatingLegDayCounter = Actual360();

        // floating leg
        Frequency floatingLegFrequency = Quarterly;
        Spread spread = 0.0;

        Integer lenghtInYears = 5;
        VanillaSwap::Type swapType = VanillaSwap::Payer;

        Date maturity = settlementDate + lenghtInYears*Years;
        Schedule fixedSchedule(settlementDate, maturity,
                               Period(fixedLegFrequency),
                               calendar, fixedLegConvention,
                               fixedLegConvention,
                               DateGeneration::Forward,false);
        Schedule floatSchedule(settlementDate, maturity,
                               Period(floatingLegFrequency),
                               calendar, floatingLegConvention,
                               floatingLegConvention,
                               DateGeneration::Forward,false);
                VanillaSwap spot5YearSwap(swapType, nominal,
            fixedSchedule, fixedRate, fixedLegDayCounter,
            floatSchedule, index, spread,
            floatingLegDayCounter);

             spot5YearSwap.setPricingEngine(boost::shared_ptr<PricingEngine>(
                                                         new DiscountingSwapEngine(liborTS,false,settlementDate,settlementDate)));
                 /***************
        * SWAP PRICING *
        ****************/

        // utilities for reporting
        std::vector<std::string> headers(4);
        headers[0] = "term structure";
        headers[1] = "net present value";
        headers[2] = "fair spread";
        headers[3] = "fair fixed rate";
        std::string separator = " | ";
        Size width = headers[0].size() + separator.size()
                   + headers[1].size() + separator.size()
                   + headers[2].size() + separator.size()
                   + headers[3].size() + separator.size() - 1;
        std::string rule(width, '-'), dblrule(width, '=');
        std::string tab(8, ' ');

        Real NPV;
        Rate fairRate;
        Spread fairSpread;
               
        NPV = spot5YearSwap.NPV();
        fairSpread = spot5YearSwap.fairSpread();
        fairRate = spot5YearSwap.fairRate();

        std::cout << std::setw(headers[0].size())
                  << "Spot-swap" << separator;
        std::cout << std::setw(headers[1].size())
                  << std::fixed << std::setprecision(2) << NPV << separator;
        std::cout << std::setw(headers[2].size())
                  << io::rate(fairSpread) << separator;
        std::cout << std::setw(headers[3].size())
                  << io::rate(fairRate) << separator;
        std::cout << std::endl;

                 std::cin.get();

 return 0;
 
}


Reply | Threaded
Open this post in threaded view
|

Re: Pricing a vanilla swap with a given zero curve instead of boostrapping

cheng li
I haven' t run your code. However I saw that you have set fixing days = 0. Since as far as I know quantlib's convention for libor is not 0 for fixing days, the error may be due to that. Quantlib will complain no historical fixing has been given.

发自我的 iPad

> 在 2014年2月19日,8:59,semiparametric <[hidden email]> 写道:
>
> Dear All:
>
> I am a newbie to quantlib. I run below code to price a vanilla swap with a
> given zero curve. The code can be complied smoothly, however, when I tried
> to run the programme, something went wrong, and the NPV couldn't be
> generated. Could anyone give me a hint on how to revise the code? Thanks in
> advance.
>
>
> #include <fstream>
> #include <vector>
> #include <ql/quantlib.hpp>
>
> using namespace QuantLib;
>
> #if defined(QL_ENABLE_SESSIONS)
> namespace QuantLib {
>
>    Integer sessionId() { return 0; }
>
> }
> #endif
>
> int main(int, char*[])
>
> {       /////////////////////////////////// Settings
> ///////////////////////////////
>
>        Calendar calendar = TARGET();
>        Date settlementDate(15, October, 2013);
>        // must be a business day
>        settlementDate = calendar.adjust(settlementDate);
>
>        Integer fixingDays = 0;
>        Date todaysDate = calendar.advance(settlementDate, -fixingDays,
> Days);
>        // nothing to do with Date::todaysDate
>        Settings::instance().evaluationDate() = todaysDate;
>
>
>        // LIBOR Curve
>
>        std::vector<Date> dates;                                  
> std::vector<Rate> rates;
>        dates.push_back(settlementDate);                          
> rates.push_back(0.26/100.0);
>        dates.push_back(calendar.advance(settlementDate,3,Months));
> rates.push_back(0.26/100.0);
>        dates.push_back(calendar.advance(settlementDate,6,Months));
> rates.push_back(0.412/100.0);
>        dates.push_back(calendar.advance(settlementDate,9,Months));
> rates.push_back(0.572/100.0);
>        dates.push_back(calendar.advance(settlementDate,1,Years));
> rates.push_back(0.735/100.0);
>        dates.push_back(calendar.advance(settlementDate,2,Years));
> rates.push_back(0.715/100.0);
>        dates.push_back(calendar.advance(settlementDate,3,Years));
> rates.push_back(1.173/100.0);
>        dates.push_back(calendar.advance(settlementDate,4,Years));
> rates.push_back(1.621/100.0);
>        dates.push_back(calendar.advance(settlementDate,5,Years));
> rates.push_back(2.027/100.0);
>        dates.push_back(calendar.advance(settlementDate,6,Years));
> rates.push_back(2.372/100.0);
>        dates.push_back(calendar.advance(settlementDate,7,Years));
> rates.push_back(2.654/100.0);
>        dates.push_back(calendar.advance(settlementDate,8,Years));
> rates.push_back(2.884/100.0);
>        dates.push_back(calendar.advance(settlementDate,9,Years));
> rates.push_back(3.072/100.0);
>        dates.push_back(calendar.advance(settlementDate,10,Years));
> rates.push_back(3.227/100.0);
>
>        //daycounter for the curve inputed.
>        DayCounter dayCounter = Actual360();
>        boost::shared_ptr<YieldTermStructure> LiborCurvePtr(
>                new InterpolatedZeroCurve<LogLinear>(dates, rates,
> dayCounter));
>        
>        
>        Handle<YieldTermStructure> liborTS(LiborCurvePtr);
>        boost::shared_ptr<IborIndex> index(
>                                           new USDLibor(Period(3, Months), liborTS));
>            
>        /*********************
>        * SWAPS TO BE PRICED *
>        **********************/
>
>        // constant nominal 1,000,000 Euro
>        Real nominal = 1000000.0;
>        // fixed leg
>        Frequency fixedLegFrequency =Quarterly;
>        BusinessDayConvention fixedLegConvention = Unadjusted;
>        BusinessDayConvention floatingLegConvention = ModifiedFollowing;
>        DayCounter fixedLegDayCounter = Thirty360(Thirty360::European);
>        Rate fixedRate = 0.04;
>        DayCounter floatingLegDayCounter = Actual360();
>
>        // floating leg
>        Frequency floatingLegFrequency = Quarterly;
>        Spread spread = 0.0;
>
>        Integer lenghtInYears = 5;
>        VanillaSwap::Type swapType = VanillaSwap::Payer;
>
>        Date maturity = settlementDate + lenghtInYears*Years;
>        Schedule fixedSchedule(settlementDate, maturity,
>                               Period(fixedLegFrequency),
>                               calendar, fixedLegConvention,
>                               fixedLegConvention,
>                               DateGeneration::Forward,false);
>        Schedule floatSchedule(settlementDate, maturity,
>                               Period(floatingLegFrequency),
>                               calendar, floatingLegConvention,
>                               floatingLegConvention,
>                               DateGeneration::Forward,false);
>        VanillaSwap spot5YearSwap(swapType, nominal,
>            fixedSchedule, fixedRate, fixedLegDayCounter,
>            floatSchedule, index, spread,
>            floatingLegDayCounter);
>
>         spot5YearSwap.setPricingEngine(boost::shared_ptr<PricingEngine>(
>                                             new
> DiscountingSwapEngine(liborTS,false,settlementDate,settlementDate)));
>         /***************
>        * SWAP PRICING *
>        ****************/
>
>        // utilities for reporting
>        std::vector<std::string> headers(4);
>        headers[0] = "term structure";
>        headers[1] = "net present value";
>        headers[2] = "fair spread";
>        headers[3] = "fair fixed rate";
>        std::string separator = " | ";
>        Size width = headers[0].size() + separator.size()
>                   + headers[1].size() + separator.size()
>                   + headers[2].size() + separator.size()
>                   + headers[3].size() + separator.size() - 1;
>        std::string rule(width, '-'), dblrule(width, '=');
>        std::string tab(8, ' ');
>
>        Real NPV;
>        Rate fairRate;
>        Spread fairSpread;
>        
>        NPV = spot5YearSwap.NPV();
>        fairSpread = spot5YearSwap.fairSpread();
>        fairRate = spot5YearSwap.fairRate();
>
>        std::cout << std::setw(headers[0].size())
>                  << "Spot-swap" << separator;
>        std::cout << std::setw(headers[1].size())
>                  << std::fixed << std::setprecision(2) << NPV << separator;
>        std::cout << std::setw(headers[2].size())
>                  << io::rate(fairSpread) << separator;
>        std::cout << std::setw(headers[3].size())
>                  << io::rate(fairRate) << separator;
>        std::cout << std::endl;
>
>         std::cin.get();
>
> return 0;
>
> }
>
>
>
>
>
>
> --
> View this message in context: http://quantlib.10058.n7.nabble.com/Pricing-a-vanilla-swap-with-a-given-zero-curve-instead-of-boostrapping-tp14987.html
> Sent from the quantlib-users mailing list archive at Nabble.com.
>
> ------------------------------------------------------------------------------
> Managing the Performance of Cloud-Based Applications
> Take advantage of what the Cloud has to offer - Avoid Common Pitfalls.
> Read the Whitepaper.
> http://pubads.g.doubleclick.net/gampad/clk?id=121054471&iu=/4140/ostg.clktrk
> _______________________________________________
> QuantLib-users mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/quantlib-users

------------------------------------------------------------------------------
Managing the Performance of Cloud-Based Applications
Take advantage of what the Cloud has to offer - Avoid Common Pitfalls.
Read the Whitepaper.
http://pubads.g.doubleclick.net/gampad/clk?id=121054471&iu=/4140/ostg.clktrk
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Pricing a vanilla swap with a given zero curve instead of boostrapping

semiparametric
This post was updated on .
Thanks, it does work. I also noted that if I changed the USDLibor index to Eurobor index, FixingDays=0 could be used.