Posted by
Nathan Abbott on
URL: http://quantlib.414.s1.nabble.com/Problem-with-a-curve-near-Labor-Day-tp6562.html
I have having a problem with evaluating a swap near Labor Day which is a holiday in the USA. I can reproduce it with the swap example by changing the settlement date, the calendar and the index. Here is the modified swap example.
#include <ql/quantlib.hpp>
#include <boost/timer.hpp>
#include <iostream>
#include <iomanip>
using namespace QuantLib;
#if defined(QL_ENABLE_SESSIONS)
namespace QuantLib {
Integer sessionId() { return 0; }
}
#endif
int main(int, char* []) {
try {
boost::timer timer;
std::cout << std::endl;
/*********************
*** MARKET DATA ***
*********************/
//Calendar calendar = TARGET();
Calendar calendar = JointCalendar(UnitedStates(UnitedStates::NYSE), UnitedKingdom(UnitedKingdom::Exchange));
// uncommenting the following line generates an error
// calendar = Tokyo();
//Date settlementDate(22, September, 2004);
Date settlementDate(2, September, 2008);
// must be a business day
settlementDate = calendar.adjust(settlementDate);
Integer fixingDays = 2;
Date todaysDate = calendar.advance(settlementDate, -fixingDays, Days);
// nothing to do with Date::todaysDate
Settings::instance().evaluationDate() = todaysDate;
todaysDate = Settings::instance().evaluationDate();
std::cout << "Today: " << todaysDate.weekday()
<< ", " << todaysDate << std::endl;
std::cout << "Settlement date: " << settlementDate.weekday()
<< ", " << settlementDate << std::endl;
// deposits
Rate d1wQuote=0.0382;
Rate d1mQuote=0.0372;
Rate d3mQuote=0.0363;
Rate d6mQuote=0.0353;
Rate d9mQuote=0.0348;
Rate d1yQuote=0.0345;
// swaps
Rate s2yQuote=0.037125;
Rate s3yQuote=0.0398;
Rate s5yQuote=0.0443;
Rate s10yQuote=0.05165;
Rate s15yQuote=0.055175;
/********************
*** QUOTES ***
********************/
// SimpleQuote stores a value which can be manually changed;
// other Quote subclasses could read the value from a database
// or some kind of data feed.
// deposits
boost::shared_ptr<Quote> d1wRate(new SimpleQuote(d1wQuote));
boost::shared_ptr<Quote> d1mRate(new SimpleQuote(d1mQuote));
boost::shared_ptr<Quote> d3mRate(new SimpleQuote(d3mQuote));
boost::shared_ptr<Quote> d6mRate(new SimpleQuote(d6mQuote));
boost::shared_ptr<Quote> d9mRate(new SimpleQuote(d9mQuote));
boost::shared_ptr<Quote> d1yRate(new SimpleQuote(d1yQuote));
// swaps
boost::shared_ptr<Quote> s2yRate(new SimpleQuote(s2yQuote));
boost::shared_ptr<Quote> s3yRate(new SimpleQuote(s3yQuote));
boost::shared_ptr<Quote> s5yRate(new SimpleQuote(s5yQuote));
boost::shared_ptr<Quote> s10yRate(new SimpleQuote(s10yQuote));
boost::shared_ptr<Quote> s15yRate(new SimpleQuote(s15yQuote));
/*********************
*** RATE HELPERS ***
*********************/
// RateHelpers are built from the above quotes together with
// other instrument dependant infos. Quotes are passed in
// relinkable handles which could be relinked to some other
// data source later.
// deposits
DayCounter depositDayCounter = Actual360();
boost::shared_ptr<RateHelper> d1w(new DepositRateHelper(
Handle<Quote>(d1wRate),
1*Weeks, fixingDays,
calendar, ModifiedFollowing,
true, depositDayCounter));
boost::shared_ptr<RateHelper> d1m(new DepositRateHelper(
Handle<Quote>(d1mRate),
1*Months, fixingDays,
calendar, ModifiedFollowing,
true, depositDayCounter));
boost::shared_ptr<RateHelper> d3m(new DepositRateHelper(
Handle<Quote>(d3mRate),
3*Months, fixingDays,
calendar, ModifiedFollowing,
true, depositDayCounter));
boost::shared_ptr<RateHelper> d6m(new DepositRateHelper(
Handle<Quote>(d6mRate),
6*Months, fixingDays,
calendar, ModifiedFollowing,
true, depositDayCounter));
boost::shared_ptr<RateHelper> d9m(new DepositRateHelper(
Handle<Quote>(d9mRate),
9*Months, fixingDays,
calendar, ModifiedFollowing,
true, depositDayCounter));
boost::shared_ptr<RateHelper> d1y(new DepositRateHelper(
Handle<Quote>(d1yRate),
1*Years, fixingDays,
calendar, ModifiedFollowing,
true, depositDayCounter));
// setup swaps
Frequency swFixedLegFrequency = Annual;
BusinessDayConvention swFixedLegConvention = Unadjusted;
DayCounter swFixedLegDayCounter = Thirty360(Thirty360::European);
// boost::shared_ptr<IborIndex> swFloatingLegIndex(new Euribor6M);
boost::shared_ptr<IborIndex> swFloatingLegIndex(new USDLibor(6*Months));
boost::shared_ptr<RateHelper> s2y(new SwapRateHelper(
Handle<Quote>(s2yRate), 2*Years,
calendar, swFixedLegFrequency,
swFixedLegConvention, swFixedLegDayCounter,
swFloatingLegIndex));
boost::shared_ptr<RateHelper> s3y(new SwapRateHelper(
Handle<Quote>(s3yRate), 3*Years,
calendar, swFixedLegFrequency,
swFixedLegConvention, swFixedLegDayCounter,
swFloatingLegIndex));
boost::shared_ptr<RateHelper> s5y(new SwapRateHelper(
Handle<Quote>(s5yRate), 5*Years,
calendar, swFixedLegFrequency,
swFixedLegConvention, swFixedLegDayCounter,
swFloatingLegIndex));
boost::shared_ptr<RateHelper> s10y(new SwapRateHelper(
Handle<Quote>(s10yRate), 10*Years,
calendar, swFixedLegFrequency,
swFixedLegConvention, swFixedLegDayCounter,
swFloatingLegIndex));
boost::shared_ptr<RateHelper> s15y(new SwapRateHelper(
Handle<Quote>(s15yRate), 15*Years,
calendar, swFixedLegFrequency,
swFixedLegConvention, swFixedLegDayCounter,
swFloatingLegIndex));
/*********************
** CURVE BUILDING **
*********************/
// Any DayCounter would be fine.
// ActualActual::ISDA ensures that 30 years is 30.0
DayCounter termStructureDayCounter =
ActualActual(ActualActual::ISDA);
double tolerance = 1.0e-15;
// A depo-swap curve
std::vector<boost::shared_ptr<RateHelper> > depoSwapInstruments;
depoSwapInstruments.push_back(d1w);
depoSwapInstruments.push_back(d1m);
depoSwapInstruments.push_back(d3m);
depoSwapInstruments.push_back(d6m);
depoSwapInstruments.push_back(d9m);
depoSwapInstruments.push_back(d1y);
depoSwapInstruments.push_back(s2y);
depoSwapInstruments.push_back(s3y);
depoSwapInstruments.push_back(s5y);
depoSwapInstruments.push_back(s10y);
depoSwapInstruments.push_back(s15y);
boost::shared_ptr<YieldTermStructure> depoSwapTermStructure(
new PiecewiseYieldCurve<Discount,LogLinear>(
settlementDate, depoSwapInstruments,
termStructureDayCounter,
std::vector<Handle<Quote> >(),
std::vector<Date>(),
tolerance));
// Term structures that will be used for pricing:
// the one used for discounting cash flows
RelinkableHandle<YieldTermStructure> discountingTermStructure;
// the one used for forward rate forecasting
RelinkableHandle<YieldTermStructure> forecastingTermStructure;
/*********************
* SWAPS TO BE PRICED *
**********************/
// constant nominal 1,000,000 Euro
Real nominal = 1000000.0;
// fixed leg
Frequency fixedLegFrequency = Annual;
BusinessDayConvention fixedLegConvention = Unadjusted;
BusinessDayConvention floatingLegConvention = ModifiedFollowing;
DayCounter fixedLegDayCounter = Thirty360(Thirty360::European);
Rate fixedRate = 0.04;
DayCounter floatingLegDayCounter = Actual360();
// floating leg
Frequency floatingLegFrequency = Semiannual;
//boost::shared_ptr<IborIndex> euriborIndex(
// new Euribor6M(forecastingTermStructure));
boost::shared_ptr<IborIndex> euriborIndex(
new USDLibor(6*Months, forecastingTermStructure));
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, euriborIndex, spread,
floatingLegDayCounter);
// 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, ' ');
// calculations
std::cout << dblrule << std::endl;
std::cout << "5-year market swap-rate = "
<< std::setprecision(2) << io::rate(s5yRate->value())
<< std::endl;
std::cout << dblrule << std::endl;
std::cout << tab << "5-years swap paying "
<< io::rate(fixedRate) << std::endl;
std::cout << headers[0] << separator
<< headers[1] << separator
<< headers[2] << separator
<< headers[3] << separator << std::endl;
std::cout << rule << std::endl;
Real NPV;
Rate fairRate;
Spread fairSpread;
boost::shared_ptr<PricingEngine> swapEngine(
new DiscountingSwapEngine(discountingTermStructure));
spot5YearSwap.setPricingEngine(swapEngine);
// Of course, you're not forced to really use different curves
forecastingTermStructure.linkTo(depoSwapTermStructure);
discountingTermStructure.linkTo(depoSwapTermStructure);
NPV = spot5YearSwap.NPV();
fairSpread = spot5YearSwap.fairSpread();
fairRate = spot5YearSwap.fairRate();
std::cout << std::setw(headers[0].size())
<< "depo-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;
QL_REQUIRE(std::fabs(fairRate-s5yQuote)<1e-8,
"5-years swap mispriced by "
<< io::rate(std::fabs(fairRate-s5yQuote)));
Real seconds = timer.elapsed();
Integer hours = int(seconds/3600);
seconds -= hours * 3600;
Integer minutes = int(seconds/60);
seconds -= minutes * 60;
std::cout << " \nRun completed in ";
if (hours > 0)
std::cout << hours << " h ";
if (hours > 0 || minutes > 0)
std::cout << minutes << " m ";
std::cout << std::fixed << std::setprecision(0)
<< seconds << " s\n" << std::endl;
return 0;
} catch (std::exception& e) {
std::cout << e.what() << std::endl;
return 1;
} catch (...) {
std::cout << "unknown error" << std::endl;
return 1;
}
}
The example was compiled with Boost 1.35.0 and Visual Studio 2005.
The output of the program is this:
Today: Thursday, August 28th, 2008
Settlement date: Tuesday, September 2nd, 2008
====================================================================
5-year market swap-rate = 4.43 %
====================================================================
5-years swap paying 4.00 %
term structure | net present value | fair spread | fair fixed rate |
--------------------------------------------------------------------
1st iteration: could not bootstrap the 7th instrument, maturity September 1st, 2010: negative time (-0.00273224) given
All I did is change to settlement date to September 2nd, 2008, change the calendar to JointCalendar(UnitedStates(UnitedStates::NYSE), UnitedKingdom(UnitedKingdom::Exchange)), and change the the index from Euribor6M to USDLibor(6*Months).
The program seems to be trying to calculate the discount factor on 09/01/08 which is Labor Day in the USA. Am I doing something wrong in setting up the curve? Any help would be appreciated. The program works correctly if the settlement date is set to September 22 2004.
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users