This example evaluates a forward-rate agreement.
#include <ql/quantlib.hpp>
#ifdef BOOST_MSVC
#endif
#include <boost/timer.hpp>
#include <iostream>
#define LENGTH(a) (sizeof(a)/sizeof(a[0]))
#ifdef BOOST_MSVC
# ifdef QL_ENABLE_THREAD_SAFE_OBSERVER_PATTERN
# include <ql/auto_link.hpp>
# define BOOST_LIB_NAME boost_system
# include <boost/config/auto_link.hpp>
# undef BOOST_LIB_NAME
# define BOOST_LIB_NAME boost_thread
# include <boost/config/auto_link.hpp>
# undef BOOST_LIB_NAME
# endif
#endif
#if defined(QL_ENABLE_SESSIONS)
}
#endif
int main(int, char* []) {
try {
boost::timer timer;
std::cout << std::endl;
boost::shared_ptr<IborIndex> euribor3m(
Settings::instance().evaluationDate() = todaysDate;
Calendar calendar = euribor3m->fixingCalendar();
Integer fixingDays = euribor3m->fixingDays();
Date settlementDate = calendar.
advance(todaysDate, fixingDays, Days);
std::cout <<
"Today: " << todaysDate.
weekday()
<< ", " << todaysDate << std::endl;
std::cout << "Settlement date: " << settlementDate.weekday()
<< ", " << settlementDate << std::endl;
Rate threeMonthFraQuote[10];
threeMonthFraQuote[1]=0.030;
threeMonthFraQuote[2]=0.031;
threeMonthFraQuote[3]=0.032;
threeMonthFraQuote[6]=0.033;
threeMonthFraQuote[9]=0.034;
boost::shared_ptr<SimpleQuote> fra1x4Rate(
boost::shared_ptr<SimpleQuote> fra2x5Rate(
boost::shared_ptr<SimpleQuote> fra3x6Rate(
boost::shared_ptr<SimpleQuote> fra6x9Rate(
boost::shared_ptr<SimpleQuote> fra9x12Rate(
DayCounter fraDayCounter = euribor3m->dayCounter();
bool endOfMonth = euribor3m->endOfMonth();
boost::shared_ptr<RateHelper> fra1x4(
fixingDays, calendar, convention,
endOfMonth, fraDayCounter));
boost::shared_ptr<RateHelper> fra2x5(
fixingDays, calendar, convention,
endOfMonth, fraDayCounter));
boost::shared_ptr<RateHelper> fra3x6(
fixingDays, calendar, convention,
endOfMonth, fraDayCounter));
boost::shared_ptr<RateHelper> fra6x9(
fixingDays, calendar, convention,
endOfMonth, fraDayCounter));
boost::shared_ptr<RateHelper> fra9x12(
fixingDays, calendar, convention,
endOfMonth, fraDayCounter));
double tolerance = 1.0e-15;
std::vector<boost::shared_ptr<RateHelper> > fraInstruments;
fraInstruments.push_back(fra1x4);
fraInstruments.push_back(fra2x5);
fraInstruments.push_back(fra3x6);
fraInstruments.push_back(fra6x9);
fraInstruments.push_back(fra9x12);
boost::shared_ptr<YieldTermStructure> fraTermStructure(
settlementDate, fraInstruments,
termStructureDayCounter,
tolerance));
discountingTermStructure.
linkTo(fraTermStructure);
Calendar fraCalendar = euribor3m->fixingCalendar();
euribor3m->businessDayConvention();
Position::Type fraFwdType = Position::Long;
Real fraNotional = 100.0;
Integer monthsToStart[] = { 1, 2, 3, 6, 9 };
euriborTermStructure.linkTo(fraTermStructure);
cout << endl;
cout << "Test FRA construction, NPV calculation, and FRA purchase"
<< endl
<< endl;
for (i=0; i<LENGTH(monthsToStart); i++) {
settlementDate,monthsToStart[i],Months,
fraBusinessDayConvention);
fraValueDate,FraTermMonths,Months,
fraBusinessDayConvention);
Rate fraStrikeRate = threeMonthFraQuote[monthsToStart[i]];
ForwardRateAgreement myFRA(fraValueDate, fraMaturityDate,
fraFwdType,fraStrikeRate,
fraNotional, euribor3m,
discountingTermStructure);
cout << "3m Term FRA, Months to Start: "
<< monthsToStart[i]
<< endl;
cout << "strike FRA rate: "
<< endl;
cout << "FRA 3m forward rate: "
<< myFRA.forwardRate()
<< endl;
cout << "FRA market quote: "
<<
io::rate(threeMonthFraQuote[monthsToStart[i]])
<< endl;
cout << "FRA spot value: "
<< myFRA.spotValue()
<< endl;
cout << "FRA forward value: "
<< myFRA.forwardValue()
<< endl;
cout << "FRA implied Yield: "
<< myFRA.impliedYield(myFRA.spotValue(),
myFRA.forwardValue(),
settlementDate,
fraDayCounter)
<< endl;
cout << "market Zero Rate: "
<< discountingTermStructure->zeroRate(fraMaturityDate,
fraDayCounter,
Simple)
<< endl;
cout << "FRA NPV [should be zero]: "
<< myFRA.NPV()
<< endl
<< endl;
}
cout << endl << endl;
cout << "Now take a 100 basis-point upward shift in FRA quotes "
<< "and examine NPV"
<< endl
<< endl;
const Real BpsShift = 0.01;
threeMonthFraQuote[1]=0.030+BpsShift;
threeMonthFraQuote[2]=0.031+BpsShift;
threeMonthFraQuote[3]=0.032+BpsShift;
threeMonthFraQuote[6]=0.033+BpsShift;
threeMonthFraQuote[9]=0.034+BpsShift;
fra1x4Rate->setValue(threeMonthFraQuote[1]);
fra2x5Rate->setValue(threeMonthFraQuote[2]);
fra3x6Rate->setValue(threeMonthFraQuote[3]);
fra6x9Rate->setValue(threeMonthFraQuote[6]);
fra9x12Rate->setValue(threeMonthFraQuote[9]);
for (i=0; i<LENGTH(monthsToStart); i++) {
settlementDate,monthsToStart[i],Months,
fraBusinessDayConvention);
fraValueDate,FraTermMonths,Months,
fraBusinessDayConvention);
threeMonthFraQuote[monthsToStart[i]] - BpsShift;
ForwardRateAgreement myFRA(fraValueDate, fraMaturityDate,
fraFwdType, fraStrikeRate,
fraNotional, euribor3m,
discountingTermStructure);
cout << "3m Term FRA, 100 notional, Months to Start = "
<< monthsToStart[i]
<< endl;
cout << "strike FRA rate: "
<< endl;
cout << "FRA 3m forward rate: "
<< myFRA.forwardRate()
<< endl;
cout << "FRA market quote: "
<<
io::rate(threeMonthFraQuote[monthsToStart[i]])
<< endl;
cout << "FRA spot value: "
<< myFRA.spotValue()
<< endl;
cout << "FRA forward value: "
<< myFRA.forwardValue()
<< endl;
cout << "FRA implied Yield: "
<< myFRA.impliedYield(myFRA.spotValue(),
myFRA.forwardValue(),
settlementDate,
Simple,
fraDayCounter)
<< endl;
cout << "market Zero Rate: "
<< discountingTermStructure->zeroRate(fraMaturityDate,
fraDayCounter,
Simple)
<< endl;
cout << "FRA NPV [should be positive]: "
<< myFRA.NPV()
<< endl
<< endl;
}
double seconds = timer.elapsed();
seconds -= hours * 3600;
seconds -= minutes * 60;
cout << " \nRun completed in ";
if (hours > 0)
cout << hours << " h ";
if (hours > 0 || minutes > 0)
cout << minutes << " m ";
cout << fixed << setprecision(0)
<< seconds << " s\n" << endl;
return 0;
} catch (exception& e) {
cerr << e.what() << endl;
return 1;
} catch (...) {
cerr << "unknown error" << endl;
return 1;
}
}