swapvaluation.cpp

This is an example of using the QuantLib Term Structure for pricing a simple swap.

00001 /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 
00021 /*  This example shows how to set up a Term Structure and then price a simple
00022     swap.
00023 */
00024 
00025 // the only header you need to use QuantLib
00026 #include <ql/quantlib.hpp>
00027 #include <iostream>
00028 #include <iomanip>
00029 
00030 using namespace QuantLib;
00031 
00032 #if defined(QL_ENABLE_SESSIONS)
00033 namespace QuantLib {
00034 
00035     Integer sessionId() { return 0; }
00036 
00037 }
00038 #endif
00039 
00040 
00041 int main(int, char* [])
00042 {
00043     try {
00044         QL_IO_INIT
00045 
00046         /*********************
00047          ***  MARKET DATA  ***
00048          *********************/
00049 
00050         Calendar calendar = TARGET();
00051         // uncommenting the following line generates an error
00052         // calendar = Tokyo();
00053         Date settlementDate(22, September, 2004);
00054         // must be a business day
00055         settlementDate = calendar.adjust(settlementDate);
00056 
00057         Integer fixingDays = 2;
00058         Date todaysDate = calendar.advance(settlementDate, -fixingDays, Days);
00059         // nothing to do with Date::todaysDate
00060         Settings::instance().evaluationDate() = todaysDate;
00061 
00062 
00063         todaysDate = Settings::instance().evaluationDate();
00064         std::cout << "Today: " << todaysDate.weekday()
00065                   << ", " << todaysDate << std::endl;
00066 
00067         std::cout << "Settlement date: " << settlementDate.weekday()
00068                   << ", " << settlementDate << std::endl;
00069 
00070         // deposits
00071         Rate d1wQuote=0.0382;
00072         Rate d1mQuote=0.0372;
00073         Rate d3mQuote=0.0363;
00074         Rate d6mQuote=0.0353;
00075         Rate d9mQuote=0.0348;
00076         Rate d1yQuote=0.0345;
00077         // FRAs
00078         Rate fra3x6Quote=0.037125;
00079         Rate fra6x9Quote=0.037125;
00080         Rate fra6x12Quote=0.037125;
00081         // futures
00082         Real fut1Quote=96.2875;
00083         Real fut2Quote=96.7875;
00084         Real fut3Quote=96.9875;
00085         Real fut4Quote=96.6875;
00086         Real fut5Quote=96.4875;
00087         Real fut6Quote=96.3875;
00088         Real fut7Quote=96.2875;
00089         Real fut8Quote=96.0875;
00090         // swaps
00091         Rate s2yQuote=0.037125;
00092         Rate s3yQuote=0.0398;
00093         Rate s5yQuote=0.0443;
00094         Rate s10yQuote=0.05165;
00095         Rate s15yQuote=0.055175;
00096 
00097 
00098         /********************
00099          ***    QUOTES    ***
00100          ********************/
00101 
00102         // SimpleQuote stores a value which can be manually changed;
00103         // other Quote subclasses could read the value from a database
00104         // or some kind of data feed.
00105 
00106         // deposits
00107         boost::shared_ptr<Quote> d1wRate(new SimpleQuote(d1wQuote));
00108         boost::shared_ptr<Quote> d1mRate(new SimpleQuote(d1mQuote));
00109         boost::shared_ptr<Quote> d3mRate(new SimpleQuote(d3mQuote));
00110         boost::shared_ptr<Quote> d6mRate(new SimpleQuote(d6mQuote));
00111         boost::shared_ptr<Quote> d9mRate(new SimpleQuote(d9mQuote));
00112         boost::shared_ptr<Quote> d1yRate(new SimpleQuote(d1yQuote));
00113         // FRAs
00114         boost::shared_ptr<Quote> fra3x6Rate(new SimpleQuote(fra3x6Quote));
00115         boost::shared_ptr<Quote> fra6x9Rate(new SimpleQuote(fra6x9Quote));
00116         boost::shared_ptr<Quote> fra6x12Rate(new SimpleQuote(fra6x12Quote));
00117         // futures
00118         boost::shared_ptr<Quote> fut1Price(new SimpleQuote(fut1Quote));
00119         boost::shared_ptr<Quote> fut2Price(new SimpleQuote(fut2Quote));
00120         boost::shared_ptr<Quote> fut3Price(new SimpleQuote(fut3Quote));
00121         boost::shared_ptr<Quote> fut4Price(new SimpleQuote(fut4Quote));
00122         boost::shared_ptr<Quote> fut5Price(new SimpleQuote(fut5Quote));
00123         boost::shared_ptr<Quote> fut6Price(new SimpleQuote(fut6Quote));
00124         boost::shared_ptr<Quote> fut7Price(new SimpleQuote(fut7Quote));
00125         boost::shared_ptr<Quote> fut8Price(new SimpleQuote(fut8Quote));
00126         // swaps
00127         boost::shared_ptr<Quote> s2yRate(new SimpleQuote(s2yQuote));
00128         boost::shared_ptr<Quote> s3yRate(new SimpleQuote(s3yQuote));
00129         boost::shared_ptr<Quote> s5yRate(new SimpleQuote(s5yQuote));
00130         boost::shared_ptr<Quote> s10yRate(new SimpleQuote(s10yQuote));
00131         boost::shared_ptr<Quote> s15yRate(new SimpleQuote(s15yQuote));
00132 
00133 
00134         /*********************
00135          ***  RATE HELPERS ***
00136          *********************/
00137 
00138         // RateHelpers are built from the above quotes together with
00139         // other instrument dependant infos.  Quotes are passed in
00140         // relinkable handles which could be relinked to some other
00141         // data source later.
00142 
00143         // deposits
00144         DayCounter depositDayCounter = Actual360();
00145 
00146         boost::shared_ptr<RateHelper> d1w(new DepositRateHelper(
00147             Handle<Quote>(d1wRate),
00148             1, Weeks, fixingDays,
00149             calendar, ModifiedFollowing, depositDayCounter));
00150         boost::shared_ptr<RateHelper> d1m(new DepositRateHelper(
00151             Handle<Quote>(d1mRate),
00152             1, Months, fixingDays,
00153             calendar, ModifiedFollowing, depositDayCounter));
00154         boost::shared_ptr<RateHelper> d3m(new DepositRateHelper(
00155             Handle<Quote>(d3mRate),
00156             3, Months, fixingDays,
00157             calendar, ModifiedFollowing, depositDayCounter));
00158         boost::shared_ptr<RateHelper> d6m(new DepositRateHelper(
00159             Handle<Quote>(d6mRate),
00160             6, Months, fixingDays,
00161             calendar, ModifiedFollowing, depositDayCounter));
00162         boost::shared_ptr<RateHelper> d9m(new DepositRateHelper(
00163             Handle<Quote>(d9mRate),
00164             9, Months, fixingDays,
00165             calendar, ModifiedFollowing, depositDayCounter));
00166         boost::shared_ptr<RateHelper> d1y(new DepositRateHelper(
00167             Handle<Quote>(d1yRate),
00168             1, Years, fixingDays,
00169             calendar, ModifiedFollowing, depositDayCounter));
00170 
00171 
00172         // setup FRAs
00173         boost::shared_ptr<RateHelper> fra3x6(new FraRateHelper(
00174             Handle<Quote>(fra3x6Rate),
00175             3, 6, fixingDays, calendar, ModifiedFollowing,
00176             depositDayCounter));
00177         boost::shared_ptr<RateHelper> fra6x9(new FraRateHelper(
00178             Handle<Quote>(fra6x9Rate),
00179             6, 9, fixingDays, calendar, ModifiedFollowing,
00180             depositDayCounter));
00181         boost::shared_ptr<RateHelper> fra6x12(new FraRateHelper(
00182             Handle<Quote>(fra6x12Rate),
00183             6, 12, fixingDays, calendar, ModifiedFollowing,
00184             depositDayCounter));
00185 
00186 
00187         // setup futures
00188         Integer futMonths = 3;
00189         Date imm = Date::nextIMMdate(settlementDate);
00190         boost::shared_ptr<RateHelper> fut1(new FuturesRateHelper(
00191             Handle<Quote>(fut1Price),
00192             imm,
00193             futMonths, calendar, ModifiedFollowing,
00194             depositDayCounter));
00195         imm = Date::nextIMMdate(imm+1);
00196         boost::shared_ptr<RateHelper> fut2(new FuturesRateHelper(
00197             Handle<Quote>(fut1Price),
00198             imm,
00199             futMonths, calendar, ModifiedFollowing,
00200             depositDayCounter));
00201         imm = Date::nextIMMdate(imm+1);
00202         boost::shared_ptr<RateHelper> fut3(new FuturesRateHelper(
00203             Handle<Quote>(fut1Price),
00204             imm,
00205             futMonths, calendar, ModifiedFollowing,
00206             depositDayCounter));
00207         imm = Date::nextIMMdate(imm+1);
00208         boost::shared_ptr<RateHelper> fut4(new FuturesRateHelper(
00209             Handle<Quote>(fut1Price),
00210             imm,
00211             futMonths, calendar, ModifiedFollowing,
00212             depositDayCounter));
00213         imm = Date::nextIMMdate(imm+1);
00214         boost::shared_ptr<RateHelper> fut5(new FuturesRateHelper(
00215             Handle<Quote>(fut1Price),
00216             imm,
00217             futMonths, calendar, ModifiedFollowing,
00218             depositDayCounter));
00219         imm = Date::nextIMMdate(imm+1);
00220         boost::shared_ptr<RateHelper> fut6(new FuturesRateHelper(
00221             Handle<Quote>(fut1Price),
00222             imm,
00223             futMonths, calendar, ModifiedFollowing,
00224             depositDayCounter));
00225         imm = Date::nextIMMdate(imm+1);
00226         boost::shared_ptr<RateHelper> fut7(new FuturesRateHelper(
00227             Handle<Quote>(fut1Price),
00228             imm,
00229             futMonths, calendar, ModifiedFollowing,
00230             depositDayCounter));
00231         imm = Date::nextIMMdate(imm+1);
00232         boost::shared_ptr<RateHelper> fut8(new FuturesRateHelper(
00233             Handle<Quote>(fut1Price),
00234             imm,
00235             futMonths, calendar, ModifiedFollowing,
00236             depositDayCounter));
00237 
00238 
00239         // setup swaps
00240         Frequency swFixedLegFrequency = Annual;
00241         BusinessDayConvention swFixedLegConvention = Unadjusted;
00242         DayCounter swFixedLegDayCounter = Thirty360(Thirty360::European);
00243         Frequency swFloatingLegFrequency = Semiannual;
00244 
00245         boost::shared_ptr<RateHelper> s2y(new SwapRateHelper(
00246             Handle<Quote>(s2yRate),
00247             2, Years, fixingDays,
00248             calendar, swFixedLegFrequency,
00249             swFixedLegConvention, swFixedLegDayCounter,
00250             swFloatingLegFrequency, ModifiedFollowing));
00251         boost::shared_ptr<RateHelper> s3y(new SwapRateHelper(
00252             Handle<Quote>(s3yRate),
00253             3, Years, fixingDays,
00254             calendar, swFixedLegFrequency,
00255             swFixedLegConvention, swFixedLegDayCounter,
00256             swFloatingLegFrequency, ModifiedFollowing));
00257         boost::shared_ptr<RateHelper> s5y(new SwapRateHelper(
00258             Handle<Quote>(s5yRate),
00259             5, Years, fixingDays,
00260             calendar, swFixedLegFrequency,
00261             swFixedLegConvention, swFixedLegDayCounter,
00262             swFloatingLegFrequency, ModifiedFollowing));
00263         boost::shared_ptr<RateHelper> s10y(new SwapRateHelper(
00264             Handle<Quote>(s10yRate),
00265             10, Years, fixingDays,
00266             calendar, swFixedLegFrequency,
00267             swFixedLegConvention, swFixedLegDayCounter,
00268             swFloatingLegFrequency, ModifiedFollowing));
00269         boost::shared_ptr<RateHelper> s15y(new SwapRateHelper(
00270             Handle<Quote>(s15yRate),
00271             15, Years, fixingDays,
00272             calendar, swFixedLegFrequency,
00273             swFixedLegConvention, swFixedLegDayCounter,
00274             swFloatingLegFrequency, ModifiedFollowing));
00275 
00276 
00277         /*********************
00278          **  CURVE BUILDING **
00279          *********************/
00280 
00281         // Any DayCounter would be fine.
00282         // ActualActual::ISDA ensures that 30 years is 30.0
00283         DayCounter termStructureDayCounter =
00284             ActualActual(ActualActual::ISDA);
00285 
00286 
00287         double tolerance = 1.0e-15;
00288 
00289         // A depo-swap curve
00290         std::vector<boost::shared_ptr<RateHelper> > depoSwapInstruments;
00291         depoSwapInstruments.push_back(d1w);
00292         depoSwapInstruments.push_back(d1m);
00293         depoSwapInstruments.push_back(d3m);
00294         depoSwapInstruments.push_back(d6m);
00295         depoSwapInstruments.push_back(d9m);
00296         depoSwapInstruments.push_back(d1y);
00297         depoSwapInstruments.push_back(s2y);
00298         depoSwapInstruments.push_back(s3y);
00299         depoSwapInstruments.push_back(s5y);
00300         depoSwapInstruments.push_back(s10y);
00301         depoSwapInstruments.push_back(s15y);
00302         boost::shared_ptr<YieldTermStructure> depoSwapTermStructure(new
00303             PiecewiseFlatForward(settlementDate, depoSwapInstruments,
00304                                  termStructureDayCounter, tolerance));
00305 
00306 
00307         // A depo-futures-swap curve
00308         std::vector<boost::shared_ptr<RateHelper> > depoFutSwapInstruments;
00309         depoFutSwapInstruments.push_back(d1w);
00310         depoFutSwapInstruments.push_back(d1m);
00311         depoFutSwapInstruments.push_back(fut1);
00312         depoFutSwapInstruments.push_back(fut2);
00313         depoFutSwapInstruments.push_back(fut3);
00314         depoFutSwapInstruments.push_back(fut4);
00315         depoFutSwapInstruments.push_back(fut5);
00316         depoFutSwapInstruments.push_back(fut6);
00317         depoFutSwapInstruments.push_back(fut7);
00318         depoFutSwapInstruments.push_back(fut8);
00319         depoFutSwapInstruments.push_back(s3y);
00320         depoFutSwapInstruments.push_back(s5y);
00321         depoFutSwapInstruments.push_back(s10y);
00322         depoFutSwapInstruments.push_back(s15y);
00323         boost::shared_ptr<YieldTermStructure> depoFutSwapTermStructure(new
00324             PiecewiseFlatForward(settlementDate, depoFutSwapInstruments,
00325                                  termStructureDayCounter, tolerance));
00326 
00327 
00328         // A depo-FRA-swap curve
00329         std::vector<boost::shared_ptr<RateHelper> > depoFRASwapInstruments;
00330         depoFRASwapInstruments.push_back(d1w);
00331         depoFRASwapInstruments.push_back(d1m);
00332         depoFRASwapInstruments.push_back(d3m);
00333         depoFRASwapInstruments.push_back(fra3x6);
00334         depoFRASwapInstruments.push_back(fra6x9);
00335         depoFRASwapInstruments.push_back(fra6x12);
00336         depoFRASwapInstruments.push_back(s2y);
00337         depoFRASwapInstruments.push_back(s3y);
00338         depoFRASwapInstruments.push_back(s5y);
00339         depoFRASwapInstruments.push_back(s10y);
00340         depoFRASwapInstruments.push_back(s15y);
00341         boost::shared_ptr<YieldTermStructure> depoFRASwapTermStructure(new
00342             PiecewiseFlatForward(settlementDate, depoFRASwapInstruments,
00343                                  termStructureDayCounter, tolerance));
00344 
00345 
00346         // Term structures that will be used for pricing:
00347         // the one used for discounting cash flows
00348         Handle<YieldTermStructure> discountingTermStructure;
00349         // the one used for forward rate forecasting
00350         Handle<YieldTermStructure> forecastingTermStructure;
00351 
00352 
00353         /*********************
00354         * SWAPS TO BE PRICED *
00355         **********************/
00356 
00357         // constant nominal 1,000,000 Euro
00358         Real nominal = 1000000.0;
00359         // fixed leg
00360         Frequency fixedLegFrequency = Annual;
00361         BusinessDayConvention fixedLegConvention = Unadjusted;
00362         BusinessDayConvention floatingLegConvention = ModifiedFollowing;
00363         DayCounter fixedLegDayCounter = Thirty360(Thirty360::European);
00364         Rate fixedRate = 0.04;
00365 
00366         // floating leg
00367         Frequency floatingLegFrequency = Semiannual;
00368         boost::shared_ptr<Xibor> euriborIndex(new Euribor(6, Months,
00369             forecastingTermStructure)); // using the forecasting curve
00370         Spread spread = 0.0;
00371 
00372         Integer lenghtInYears = 5;
00373         bool payFixedRate = true;
00374 
00375         Date maturity = calendar.advance(settlementDate, lenghtInYears, Years,
00376                                          floatingLegConvention);
00377         Schedule fixedSchedule(calendar, settlementDate, maturity,
00378                                fixedLegFrequency, fixedLegConvention);
00379         Schedule floatSchedule(calendar, settlementDate, maturity,
00380                                floatingLegFrequency, floatingLegConvention);
00381         SimpleSwap spot5YearSwap(
00382             payFixedRate, nominal,
00383             fixedSchedule, fixedRate, fixedLegDayCounter,
00384             floatSchedule, euriborIndex, fixingDays, spread,
00385             discountingTermStructure);
00386 
00387         Date fwdStart = calendar.advance(settlementDate, 1, Years);
00388         Date fwdMaturity = calendar.advance(fwdStart, lenghtInYears, Years,
00389                                             floatingLegConvention);
00390         Schedule fwdFixedSchedule(calendar, fwdStart, fwdMaturity,
00391                                   fixedLegFrequency, fixedLegConvention);
00392         Schedule fwdFloatSchedule(calendar, fwdStart, fwdMaturity,
00393                                   floatingLegFrequency, floatingLegConvention);
00394         SimpleSwap oneYearForward5YearSwap(
00395             payFixedRate, nominal,
00396             fwdFixedSchedule, fixedRate, fixedLegDayCounter,
00397             fwdFloatSchedule, euriborIndex, fixingDays, spread,
00398             discountingTermStructure);
00399 
00400 
00401         /***************
00402         * SWAP PRICING *
00403         ****************/
00404 
00405         // utilities for reporting
00406         std::vector<std::string> headers(4);
00407         headers[0] = "term structure";
00408         headers[1] = "net present value";
00409         headers[2] = "fair spread";
00410         headers[3] = "fair fixed rate";
00411         std::string separator = " | ";
00412         Size width = headers[0].size() + separator.size()
00413                    + headers[1].size() + separator.size()
00414                    + headers[2].size() + separator.size()
00415                    + headers[3].size() + separator.size() - 1;
00416         std::string rule(width, '-'), dblrule(width, '=');
00417         std::string tab(8, ' ');
00418 
00419         // calculations
00420 
00421         std::cout << dblrule << std::endl;
00422         std::cout <<  "5-year market swap-rate = "
00423                   << std::setprecision(2) << io::rate(s5yRate->value())
00424                   << std::endl;
00425         std::cout << dblrule << std::endl;
00426 
00427         std::cout << tab << "5-years swap paying "
00428                   << io::rate(fixedRate) << std::endl;
00429         std::cout << headers[0] << separator
00430                   << headers[1] << separator
00431                   << headers[2] << separator
00432                   << headers[3] << separator << std::endl;
00433         std::cout << rule << std::endl;
00434 
00435         Real NPV;
00436         Rate fairRate;
00437         Spread fairSpread;
00438 
00439         // Of course, you're not forced to really use different curves
00440         forecastingTermStructure.linkTo(depoSwapTermStructure);
00441         discountingTermStructure.linkTo(depoSwapTermStructure);
00442 
00443         NPV = spot5YearSwap.NPV();
00444         fairSpread = spot5YearSwap.fairSpread();
00445         fairRate = spot5YearSwap.fairRate();
00446 
00447         std::cout << std::setw(headers[0].size())
00448                   << "depo-swap" << separator;
00449         std::cout << std::setw(headers[1].size())
00450                   << std::fixed << std::setprecision(2) << NPV << separator;
00451         std::cout << std::setw(headers[2].size())
00452                   << io::rate(fairSpread) << separator;
00453         std::cout << std::setw(headers[3].size())
00454                   << io::rate(fairRate) << separator;
00455         std::cout << std::endl;
00456 
00457 
00458         // let's check that the 5 years swap has been correctly re-priced
00459         QL_REQUIRE(std::fabs(fairRate-s5yQuote)<1e-8,
00460                    "5-years swap mispriced by "
00461                    << io::rate(std::fabs(fairRate-s5yQuote)));
00462 
00463 
00464         forecastingTermStructure.linkTo(depoFutSwapTermStructure);
00465         discountingTermStructure.linkTo(depoFutSwapTermStructure);
00466 
00467         NPV = spot5YearSwap.NPV();
00468         fairSpread = spot5YearSwap.fairSpread();
00469         fairRate = spot5YearSwap.fairRate();
00470 
00471         std::cout << std::setw(headers[0].size())
00472                   << "depo-fut-swap" << separator;
00473         std::cout << std::setw(headers[1].size())
00474                   << std::fixed << std::setprecision(2) << NPV << separator;
00475         std::cout << std::setw(headers[2].size())
00476                   << io::rate(fairSpread) << separator;
00477         std::cout << std::setw(headers[3].size())
00478                   << io::rate(fairRate) << separator;
00479         std::cout << std::endl;
00480 
00481         QL_REQUIRE(std::fabs(fairRate-s5yQuote)<1e-8,
00482                    "5-years swap mispriced!");
00483 
00484 
00485         forecastingTermStructure.linkTo(depoFRASwapTermStructure);
00486         discountingTermStructure.linkTo(depoFRASwapTermStructure);
00487 
00488         NPV = spot5YearSwap.NPV();
00489         fairSpread = spot5YearSwap.fairSpread();
00490         fairRate = spot5YearSwap.fairRate();
00491 
00492         std::cout << std::setw(headers[0].size())
00493                   << "depo-FRA-swap" << separator;
00494         std::cout << std::setw(headers[1].size())
00495                   << std::fixed << std::setprecision(2) << NPV << separator;
00496         std::cout << std::setw(headers[2].size())
00497                   << io::rate(fairSpread) << separator;
00498         std::cout << std::setw(headers[3].size())
00499                   << io::rate(fairRate) << separator;
00500         std::cout << std::endl;
00501 
00502         QL_REQUIRE(std::fabs(fairRate-s5yQuote)<1e-8,
00503                    "5-years swap mispriced!");
00504 
00505 
00506         std::cout << rule << std::endl;
00507 
00508         // now let's price the 1Y forward 5Y swap
00509 
00510         std::cout << tab << "5-years, 1-year forward swap paying "
00511                   << io::rate(fixedRate) << std::endl;
00512         std::cout << headers[0] << separator
00513                   << headers[1] << separator
00514                   << headers[2] << separator
00515                   << headers[3] << separator << std::endl;
00516         std::cout << rule << std::endl;
00517 
00518 
00519         forecastingTermStructure.linkTo(depoSwapTermStructure);
00520         discountingTermStructure.linkTo(depoSwapTermStructure);
00521 
00522         NPV = oneYearForward5YearSwap.NPV();
00523         fairSpread = oneYearForward5YearSwap.fairSpread();
00524         fairRate = oneYearForward5YearSwap.fairRate();
00525 
00526         std::cout << std::setw(headers[0].size())
00527                   << "depo-swap" << separator;
00528         std::cout << std::setw(headers[1].size())
00529                   << std::fixed << std::setprecision(2) << NPV << separator;
00530         std::cout << std::setw(headers[2].size())
00531                   << io::rate(fairSpread) << separator;
00532         std::cout << std::setw(headers[3].size())
00533                   << io::rate(fairRate) << separator;
00534         std::cout << std::endl;
00535 
00536 
00537         forecastingTermStructure.linkTo(depoFutSwapTermStructure);
00538         discountingTermStructure.linkTo(depoFutSwapTermStructure);
00539 
00540         NPV = oneYearForward5YearSwap.NPV();
00541         fairSpread = oneYearForward5YearSwap.fairSpread();
00542         fairRate = oneYearForward5YearSwap.fairRate();
00543 
00544         std::cout << std::setw(headers[0].size())
00545                   << "depo-fut-swap" << separator;
00546         std::cout << std::setw(headers[1].size())
00547                   << std::fixed << std::setprecision(2) << NPV << separator;
00548         std::cout << std::setw(headers[2].size())
00549                   << io::rate(fairSpread) << separator;
00550         std::cout << std::setw(headers[3].size())
00551                   << io::rate(fairRate) << separator;
00552         std::cout << std::endl;
00553 
00554 
00555         forecastingTermStructure.linkTo(depoFRASwapTermStructure);
00556         discountingTermStructure.linkTo(depoFRASwapTermStructure);
00557 
00558         NPV = oneYearForward5YearSwap.NPV();
00559         fairSpread = oneYearForward5YearSwap.fairSpread();
00560         fairRate = oneYearForward5YearSwap.fairRate();
00561 
00562         std::cout << std::setw(headers[0].size())
00563                   << "depo-FRA-swap" << separator;
00564         std::cout << std::setw(headers[1].size())
00565                   << std::fixed << std::setprecision(2) << NPV << separator;
00566         std::cout << std::setw(headers[2].size())
00567                   << io::rate(fairSpread) << separator;
00568         std::cout << std::setw(headers[3].size())
00569                   << io::rate(fairRate) << separator;
00570         std::cout << std::endl;
00571 
00572 
00573         // now let's say that the 5-years swap rate goes up to 4.60%.
00574         // A smarter market element--say, connected to a data source-- would
00575         // notice the change itself. Since we're using SimpleQuotes,
00576         // we'll have to change the value manually--which forces us to
00577         // downcast the handle and use the SimpleQuote
00578         // interface. In any case, the point here is that a change in the
00579         // value contained in the Quote triggers a new bootstrapping
00580         // of the curve and a repricing of the swap.
00581 
00582         boost::shared_ptr<SimpleQuote> fiveYearsRate =
00583             boost::dynamic_pointer_cast<SimpleQuote>(s5yRate);
00584         fiveYearsRate->setValue(0.0460);
00585 
00586         std::cout << dblrule << std::endl;
00587         std::cout <<  "5-year market swap-rate = "
00588                   << io::rate(s5yRate->value()) << std::endl;
00589         std::cout << dblrule << std::endl;
00590 
00591         std::cout << tab << "5-years swap paying "
00592                   << io::rate(fixedRate) << std::endl;
00593         std::cout << headers[0] << separator
00594                   << headers[1] << separator
00595                   << headers[2] << separator
00596                   << headers[3] << separator << std::endl;
00597         std::cout << rule << std::endl;
00598 
00599         // now get the updated results
00600         forecastingTermStructure.linkTo(depoSwapTermStructure);
00601         discountingTermStructure.linkTo(depoSwapTermStructure);
00602 
00603         NPV = spot5YearSwap.NPV();
00604         fairSpread = spot5YearSwap.fairSpread();
00605         fairRate = spot5YearSwap.fairRate();
00606 
00607         std::cout << std::setw(headers[0].size())
00608                   << "depo-swap" << separator;
00609         std::cout << std::setw(headers[1].size())
00610                   << std::fixed << std::setprecision(2) << NPV << separator;
00611         std::cout << std::setw(headers[2].size())
00612                   << io::rate(fairSpread) << separator;
00613         std::cout << std::setw(headers[3].size())
00614                   << io::rate(fairRate) << separator;
00615         std::cout << std::endl;
00616 
00617         QL_REQUIRE(std::fabs(fairRate-s5yRate->value())<1e-8,
00618                    "5-years swap mispriced!");
00619 
00620 
00621         forecastingTermStructure.linkTo(depoFutSwapTermStructure);
00622         discountingTermStructure.linkTo(depoFutSwapTermStructure);
00623 
00624         NPV = spot5YearSwap.NPV();
00625         fairSpread = spot5YearSwap.fairSpread();
00626         fairRate = spot5YearSwap.fairRate();
00627 
00628         std::cout << std::setw(headers[0].size())
00629                   << "depo-fut-swap" << separator;
00630         std::cout << std::setw(headers[1].size())
00631                   << std::fixed << std::setprecision(2) << NPV << separator;
00632         std::cout << std::setw(headers[2].size())
00633                   << io::rate(fairSpread) << separator;
00634         std::cout << std::setw(headers[3].size())
00635                   << io::rate(fairRate) << separator;
00636         std::cout << std::endl;
00637 
00638         QL_REQUIRE(std::fabs(fairRate-s5yRate->value())<1e-8,
00639                    "5-years swap mispriced!");
00640 
00641 
00642         forecastingTermStructure.linkTo(depoFRASwapTermStructure);
00643         discountingTermStructure.linkTo(depoFRASwapTermStructure);
00644 
00645         NPV = spot5YearSwap.NPV();
00646         fairSpread = spot5YearSwap.fairSpread();
00647         fairRate = spot5YearSwap.fairRate();
00648 
00649         std::cout << std::setw(headers[0].size())
00650                   << "depo-FRA-swap" << separator;
00651         std::cout << std::setw(headers[1].size())
00652                   << std::fixed << std::setprecision(2) << NPV << separator;
00653         std::cout << std::setw(headers[2].size())
00654                   << io::rate(fairSpread) << separator;
00655         std::cout << std::setw(headers[3].size())
00656                   << io::rate(fairRate) << separator;
00657         std::cout << std::endl;
00658 
00659         QL_REQUIRE(std::fabs(fairRate-s5yRate->value())<1e-8,
00660                    "5-years swap mispriced!");
00661 
00662         std::cout << rule << std::endl;
00663 
00664         // the 1Y forward 5Y swap changes as well
00665 
00666         std::cout << tab << "5-years, 1-year forward swap paying "
00667                   << io::rate(fixedRate) << std::endl;
00668         std::cout << headers[0] << separator
00669                   << headers[1] << separator
00670                   << headers[2] << separator
00671                   << headers[3] << separator << std::endl;
00672         std::cout << rule << std::endl;
00673 
00674 
00675         forecastingTermStructure.linkTo(depoSwapTermStructure);
00676         discountingTermStructure.linkTo(depoSwapTermStructure);
00677 
00678         NPV = oneYearForward5YearSwap.NPV();
00679         fairSpread = oneYearForward5YearSwap.fairSpread();
00680         fairRate = oneYearForward5YearSwap.fairRate();
00681 
00682         std::cout << std::setw(headers[0].size())
00683                   << "depo-swap" << separator;
00684         std::cout << std::setw(headers[1].size())
00685                   << std::fixed << std::setprecision(2) << NPV << separator;
00686         std::cout << std::setw(headers[2].size())
00687                   << io::rate(fairSpread) << separator;
00688         std::cout << std::setw(headers[3].size())
00689                   << io::rate(fairRate) << separator;
00690         std::cout << std::endl;
00691 
00692 
00693         forecastingTermStructure.linkTo(depoFutSwapTermStructure);
00694         discountingTermStructure.linkTo(depoFutSwapTermStructure);
00695 
00696         NPV = oneYearForward5YearSwap.NPV();
00697         fairSpread = oneYearForward5YearSwap.fairSpread();
00698         fairRate = oneYearForward5YearSwap.fairRate();
00699 
00700         std::cout << std::setw(headers[0].size())
00701                   << "depo-fut-swap" << separator;
00702         std::cout << std::setw(headers[1].size())
00703                   << std::fixed << std::setprecision(2) << NPV << separator;
00704         std::cout << std::setw(headers[2].size())
00705                   << io::rate(fairSpread) << separator;
00706         std::cout << std::setw(headers[3].size())
00707                   << io::rate(fairRate) << separator;
00708         std::cout << std::endl;
00709 
00710 
00711         forecastingTermStructure.linkTo(depoFRASwapTermStructure);
00712         discountingTermStructure.linkTo(depoFRASwapTermStructure);
00713 
00714         NPV = oneYearForward5YearSwap.NPV();
00715         fairSpread = oneYearForward5YearSwap.fairSpread();
00716         fairRate = oneYearForward5YearSwap.fairRate();
00717 
00718         std::cout << std::setw(headers[0].size())
00719                   << "depo-FRA-swap" << separator;
00720         std::cout << std::setw(headers[1].size())
00721                   << std::fixed << std::setprecision(2) << NPV << separator;
00722         std::cout << std::setw(headers[2].size())
00723                   << io::rate(fairSpread) << separator;
00724         std::cout << std::setw(headers[3].size())
00725                   << io::rate(fairRate) << separator;
00726         std::cout << std::endl;
00727 
00728         return 0;
00729 
00730     } catch (std::exception& e) {
00731         std::cout << e.what() << std::endl;
00732         return 1;
00733     } catch (...) {
00734         std::cout << "unknown error" << std::endl;
00735         return 1;
00736     }
00737 }
00738 

QuantLib.org
QuantLib
Hosted by
SourceForge.net Logo
Documentation generated by
doxygen