discount' : is not a member of 'Handle<class QuantLib::TermStruct ure>'
Posted by Luis Pereira-3 on Mar 13, 2002; 8:20am
URL: http://quantlib.414.s1.nabble.com/discount-is-not-a-member-of-Handle-class-QuantLib-TermStruct-ure-tp1901.html
Hi,
I am trying to use the QuantLib::TermStructure in order to calculate
Discount Factors.
As a starting point I used the Quantlib Example for valuing SWAPS. Then I
deleted some part of the
code in order to keep it more simple and yet working. After that in the zone
////////////////TEST////////////////////////////
I created a variable which i tryed to get the value of the Discount Factor
of one year. In tryed like this:
////////////////TEST////////////////////////////
DiscountFactor bla;
bla=depoSwapTermStructure.discount(1, false);
////////////////TEST////////////////////////////
But I get the message "discount' : is not a member of 'Handle<class
QuantLib::TermStructure>'".
What am I doing wrong?
Thanks for your help.
Regards,
Luís Pereira
PS: Below you can find all the code:
// teste.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <ql/quantlib.hpp>
using namespace QuantLib;
using QuantLib::Handle;
using Calendars::TARGET;
using DayCounters::ActualActual;
using DayCounters::Actual360;
using DayCounters::Thirty360;
using Indexes::Xibor;
using Indexes::Euribor;
using Instruments::SimpleSwap;
using TermStructures::PiecewiseFlatForward;
using TermStructures::FlatForward;
using TermStructures::RateHelper;
using TermStructures::DepositRateHelper;
using TermStructures::SwapRateHelper;
int main(int argc, char* argv[])
{
try{
Calendar calendar = TARGET();
Currency currency = EUR;
int settlementDays = 2;
int fixingDays = 2;
/*********************
*** MARKET DATA ***
*********************/
Date todaysDate(6, November, 2001);
// deposits
double d1wQuote=0.0382;
double d1mQuote=0.0372;
double d3mQuote=0.0363;
double d6mQuote=0.0353;
double d9mQuote=0.0348;
double d1yQuote=0.0345;
// swaps
double s2yQuote=0.037125;
double s3yQuote=0.0398;
double s5yQuote=0.0443;
double s10yQuote=0.05165;
double s15yQuote=0.055175;
/*********************
*** RATE HELPERS ***
*********************/
// RateHelpers are built from the above quotes together with other
// instrument dependant infos.
// setup deposits
DayCounter depositDayCounter = Actual360();
Handle<RateHelper> d1w(new DepositRateHelper(
d1wQuote, settlementDays,
1, Weeks, calendar, ModifiedFollowing, depositDayCounter));
Handle<RateHelper> d1m(new DepositRateHelper(
d1mQuote, settlementDays,
1, Months, calendar, ModifiedFollowing, depositDayCounter));
Handle<RateHelper> d3m(new DepositRateHelper(
d3mQuote, settlementDays,
3, Months, calendar, ModifiedFollowing, depositDayCounter));
Handle<RateHelper> d6m(new DepositRateHelper(
d6mQuote, settlementDays,
6, Months, calendar, ModifiedFollowing, depositDayCounter));
Handle<RateHelper> d9m(new DepositRateHelper(
d9mQuote, settlementDays,
9, Months, calendar, ModifiedFollowing, depositDayCounter));
Handle<RateHelper> d1y(new DepositRateHelper(
d1yQuote, settlementDays,
1, Years, calendar, ModifiedFollowing, depositDayCounter));
// setup swaps
int swFixedLegFrequency = 1;
bool swFixedLegIsAdjusted = false;
DayCounter swFixedLegDayCounter = Thirty360(Thirty360::European);
int swFloatingLegFrequency = 2;
Handle<RateHelper> s2y(new SwapRateHelper(
s2yQuote, settlementDays,
2, calendar, ModifiedFollowing, swFixedLegFrequency,
swFixedLegIsAdjusted, swFixedLegDayCounter,
swFloatingLegFrequency));
Handle<RateHelper> s3y(new SwapRateHelper(
s3yQuote, settlementDays,
3, calendar, ModifiedFollowing, swFixedLegFrequency,
swFixedLegIsAdjusted, swFixedLegDayCounter,
swFloatingLegFrequency));
Handle<RateHelper> s5y(new SwapRateHelper(
s5yQuote, settlementDays,
5, calendar, ModifiedFollowing, swFixedLegFrequency,
swFixedLegIsAdjusted, swFixedLegDayCounter,
swFloatingLegFrequency));
Handle<RateHelper> s10y(new SwapRateHelper(
s10yQuote, settlementDays,
10, calendar, ModifiedFollowing, swFixedLegFrequency,
swFixedLegIsAdjusted, swFixedLegDayCounter,
swFloatingLegFrequency));
Handle<RateHelper> s15y(new SwapRateHelper(
s15yQuote, settlementDays,
15, calendar, ModifiedFollowing, swFixedLegFrequency,
swFixedLegIsAdjusted, swFixedLegDayCounter,
swFloatingLegFrequency));
/*********************
** CURVE BUILDING **
*********************/
// Any DayCounter would be fine.
// ActualActual::ISDA ensures that 30 years is 30.0
DayCounter termStructureDayCounter =
ActualActual(ActualActual::ISDA);
// A depo-swap curve
std::vector<Handle<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);
Handle<TermStructure> depoSwapTermStructure(new
PiecewiseFlatForward(currency, termStructureDayCounter,
todaysDate, calendar, settlementDays, depoSwapInstruments));
/*********************
* SWAPS TO BE PRICED *
**********************/
// Term structures that will be used for pricing:
// the one used for discounting cash flows
RelinkableHandle<TermStructure> discountingTermStructure;
// the one used for forward rate forecasting
RelinkableHandle<TermStructure> forecastingTermStructure;
// spot start
Date spotDate = calendar.advance(todaysDate, settlementDays, Days,
Following);
// constant nominal 1,000,000 Euro
std::vector<double> nominals;
nominals.push_back(1000000);
// fixed leg
int fixedLegFrequency = 1; // annual
bool fixedLegIsAdjusted = false;
RollingConvention roll = ModifiedFollowing;
DayCounter fixedLegDayCounter = Thirty360(Thirty360::European);
Rate fixedRate = 0.04;
// constant coupon
std::vector<double> couponRates;
couponRates.push_back(fixedRate);
// floating leg
int floatingLegFrequency = 2;
Handle<Xibor> euriborIndex(new Euribor(6, Months,
forecastingTermStructure)); // using the forecasting curve
// constant null spread
std::vector<double> spreads;
spreads.push_back(0.0);
////////////////TEST////////////////////////////
DiscountFactor bla;
bla=depoSwapTermStructure.discount(1, false);
////////////////TEST////////////////////////////
int lenghtInYears = 5;
bool payFixedRate = true;
SimpleSwap spot5YearSwap(payFixedRate, spotDate, lenghtInYears,
Years, calendar, roll, nominals, fixedLegFrequency, couponRates,
fixedLegIsAdjusted, fixedLegDayCounter, floatingLegFrequency,
euriborIndex, fixingDays, spreads,
discountingTermStructure); // using the discounting curve
SimpleSwap oneYearForward5YearSwap(payFixedRate,
calendar.advance(spotDate, 1, Years, ModifiedFollowing),
lenghtInYears, Years,
calendar, roll, nominals, fixedLegFrequency, couponRates,
fixedLegIsAdjusted, fixedLegDayCounter, floatingLegFrequency,
euriborIndex, fixingDays, spreads,
discountingTermStructure); // using the discounting curve
/***************
* SWAP PRICING *
****************/
// let's price in term of NPV, fixed rate, and spread
double NPV;
Rate fairFixedRate;
Spread fairFloatingSpread;
// Of course, you're not forced to really use different curves
forecastingTermStructure.linkTo(depoSwapTermStructure);
discountingTermStructure.linkTo(depoSwapTermStructure);
std::cout << "*** using Depo-Fut-Swap term structure:" << std::endl;
NPV = spot5YearSwap.NPV();
std::cout << "5Y "
<< RateFormatter::toString(fixedRate,2)
<< " NPV: "
<< DoubleFormatter::toString(NPV,2)
<< std::endl;
fairFloatingSpread = - NPV / spot5YearSwap.floatingLegBPS();
std::cout << "5Y "
<< RateFormatter::toString(fixedRate,2)
<< " spread: "
<< RateFormatter::toString(fairFloatingSpread,8)
<< std::endl;
fairFixedRate = fixedRate - NPV / spot5YearSwap.fixedLegBPS();
std::cout << "5Y fixed rate: "
<< RateFormatter::toString(fairFixedRate,8)
<< std::endl;
// let's check that the 5 years swap has been correctly re-priced
QL_REQUIRE(abs(fairFixedRate-s5yQuote)<1e-8,
"5 years swap mispriced!");
// now let's price the 1Y forward 5Y swap
NPV = oneYearForward5YearSwap.NPV();
std::cout << "1Yx5Y "
<< RateFormatter::toString(fixedRate,2)
<< " NPV: "
<< DoubleFormatter::toString(NPV,2)
<< std::endl;
fairFloatingSpread = -NPV / spot5YearSwap.floatingLegBPS();
std::cout << "1Yx5Y "
<< RateFormatter::toString(fixedRate,2)
<< " spread: "
<< RateFormatter::toString(fairFloatingSpread,8)
<< std::endl;
fairFixedRate = fixedRate -
NPV/oneYearForward5YearSwap.fixedLegBPS();
std::cout << "1Yx5Y fixed rate: "
<< RateFormatter::toString(fairFixedRate,8)
<< 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;
}
}