1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
|
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*!
Copyright (C) 2006 Allen Kuo
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it
under the terms of the QuantLib license. You should have received a
copy of the license along with this program; if not, please email
<quantlib-dev@lists.sf.net>. The license is also available online at
<http://quantlib.org/license.shtml>.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/
/* This example shows how to set up a term structure and price a simple
forward-rate agreement.
*/
#include <ql/qldefines.hpp>
#if !defined(BOOST_ALL_NO_LIB) && defined(BOOST_MSVC)
# include <ql/auto_link.hpp>
#endif
#include <ql/instruments/forwardrateagreement.hpp>
#include <ql/termstructures/yield/piecewiseyieldcurve.hpp>
#include <ql/termstructures/yield/ratehelpers.hpp>
#include <ql/indexes/ibor/euribor.hpp>
#include <ql/time/daycounters/actualactual.hpp>
#include <iostream>
#define LENGTH(a) (sizeof(a)/sizeof(a[0]))
using namespace std;
using namespace QuantLib;
int main(int, char* []) {
try {
std::cout << std::endl;
/*********************
*** MARKET DATA ***
*********************/
RelinkableHandle<YieldTermStructure> euriborTermStructure;
ext::shared_ptr<IborIndex> euribor3m(
new Euribor3M(euriborTermStructure));
Date todaysDate = Date(23, May, 2006);
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;
// 3 month term FRA quotes (index refers to monthsToStart)
Rate threeMonthFraQuote[10];
threeMonthFraQuote[1]=0.030;
threeMonthFraQuote[2]=0.031;
threeMonthFraQuote[3]=0.032;
threeMonthFraQuote[6]=0.033;
threeMonthFraQuote[9]=0.034;
/********************
*** 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.
// FRAs
ext::shared_ptr<SimpleQuote> fra1x4Rate(
new SimpleQuote(threeMonthFraQuote[1]));
ext::shared_ptr<SimpleQuote> fra2x5Rate(
new SimpleQuote(threeMonthFraQuote[2]));
ext::shared_ptr<SimpleQuote> fra3x6Rate(
new SimpleQuote(threeMonthFraQuote[3]));
ext::shared_ptr<SimpleQuote> fra6x9Rate(
new SimpleQuote(threeMonthFraQuote[6]));
ext::shared_ptr<SimpleQuote> fra9x12Rate(
new SimpleQuote(threeMonthFraQuote[9]));
RelinkableHandle<Quote> h1x4; h1x4.linkTo(fra1x4Rate);
RelinkableHandle<Quote> h2x5; h2x5.linkTo(fra2x5Rate);
RelinkableHandle<Quote> h3x6; h3x6.linkTo(fra3x6Rate);
RelinkableHandle<Quote> h6x9; h6x9.linkTo(fra6x9Rate);
RelinkableHandle<Quote> h9x12; h9x12.linkTo(fra9x12Rate);
/*********************
*** 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.
DayCounter fraDayCounter = euribor3m->dayCounter();
BusinessDayConvention convention = euribor3m->businessDayConvention();
bool endOfMonth = euribor3m->endOfMonth();
ext::shared_ptr<RateHelper> fra1x4(
new FraRateHelper(h1x4, 1, 4,
fixingDays, calendar, convention,
endOfMonth, fraDayCounter));
ext::shared_ptr<RateHelper> fra2x5(
new FraRateHelper(h2x5, 2, 5,
fixingDays, calendar, convention,
endOfMonth, fraDayCounter));
ext::shared_ptr<RateHelper> fra3x6(
new FraRateHelper(h3x6, 3, 6,
fixingDays, calendar, convention,
endOfMonth, fraDayCounter));
ext::shared_ptr<RateHelper> fra6x9(
new FraRateHelper(h6x9, 6, 9,
fixingDays, calendar, convention,
endOfMonth, fraDayCounter));
ext::shared_ptr<RateHelper> fra9x12(
new FraRateHelper(h9x12, 9, 12,
fixingDays, calendar, convention,
endOfMonth, fraDayCounter));
/*********************
** CURVE BUILDING **
*********************/
// Any DayCounter would be fine.
// ActualActual::ISDA ensures that 30 years is 30.0
DayCounter termStructureDayCounter =
ActualActual(ActualActual::ISDA);
// A FRA curve
std::vector<ext::shared_ptr<RateHelper> > fraInstruments;
fraInstruments.push_back(fra1x4);
fraInstruments.push_back(fra2x5);
fraInstruments.push_back(fra3x6);
fraInstruments.push_back(fra6x9);
fraInstruments.push_back(fra9x12);
ext::shared_ptr<YieldTermStructure> fraTermStructure(
new PiecewiseYieldCurve<Discount,LogLinear>(
settlementDate, fraInstruments,
termStructureDayCounter));
/***********************
*** construct FRA's ***
***********************/
Calendar fraCalendar = euribor3m->fixingCalendar();
BusinessDayConvention fraBusinessDayConvention =
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;
Size i;
for (i=0; i<LENGTH(monthsToStart); i++) {
Date fraValueDate = fraCalendar.advance(
settlementDate,monthsToStart[i],Months,
fraBusinessDayConvention);
Rate fraStrikeRate = threeMonthFraQuote[monthsToStart[i]];
ForwardRateAgreement myFRA(fraValueDate,
fraFwdType,fraStrikeRate,
fraNotional, euribor3m);
cout << "3m Term FRA, Months to Start: "
<< monthsToStart[i]
<< endl;
cout << "strike FRA rate: "
<< io::rate(fraStrikeRate)
<< endl;
cout << "FRA 3m forward rate: "
<< myFRA.forwardRate()
<< endl;
cout << "FRA market quote: "
<< io::rate(threeMonthFraQuote[monthsToStart[i]])
<< endl;
cout << "FRA amount [should be zero]: "
<< myFRA.amount()
<< 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++) {
Date fraValueDate = fraCalendar.advance(
settlementDate,monthsToStart[i],Months,
fraBusinessDayConvention);
Rate fraStrikeRate =
threeMonthFraQuote[monthsToStart[i]] - BpsShift;
ForwardRateAgreement myFRA(fraValueDate,
fraFwdType, fraStrikeRate,
fraNotional, euribor3m);
cout << "3m Term FRA, 100 notional, Months to Start = "
<< monthsToStart[i]
<< endl;
cout << "strike FRA rate: "
<< io::rate(fraStrikeRate)
<< endl;
cout << "FRA 3m forward rate: "
<< myFRA.forwardRate()
<< endl;
cout << "FRA market quote: "
<< io::rate(threeMonthFraQuote[monthsToStart[i]])
<< endl;
cout << "FRA amount [should be positive]: "
<< myFRA.amount()
<< endl;
cout << "FRA NPV [should be positive]: "
<< myFRA.NPV()
<< endl
<< endl;
}
return 0;
} catch (exception& e) {
cerr << e.what() << endl;
return 1;
} catch (...) {
cerr << "unknown error" << endl;
return 1;
}
}
|