#include <ql/quantlib.hpp>
using namespace QuantLib;
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::FraRateHelper;
using TermStructures::FuturesRateHelper;
using TermStructures::SwapRateHelper;
int main(int argc, char* argv[])
{
try {
QL_IO_INIT
Calendar calendar = TARGET();
Date todaysDate(6, November, 2001);
Date settlementDate(8, November, 2001);
double d1wQuote=0.0382;
double d1mQuote=0.0372;
double d3mQuote=0.0363;
double d6mQuote=0.0353;
double d9mQuote=0.0348;
double d1yQuote=0.0345;
double fra3x6Quote=0.037125;
double fra6x9Quote=0.037125;
double fra6x12Quote=0.037125;
double fut1Quote=96.2875;
double fut2Quote=96.7875;
double fut3Quote=96.9875;
double fut4Quote=96.6875;
double fut5Quote=96.4875;
double fut6Quote=96.3875;
double fut7Quote=96.2875;
double fut8Quote=96.0875;
double s2yQuote=0.037125;
double s3yQuote=0.0398;
double s5yQuote=0.0443;
double s10yQuote=0.05165;
double s15yQuote=0.055175;
DayCounter depositDayCounter = Actual360();
int settlementDays = 2;
Handle<RateHelper> d1w(new DepositRateHelper(
d1wQuote, 1, Weeks, settlementDays,
calendar, ModifiedFollowing, depositDayCounter));
Handle<RateHelper> d1m(new DepositRateHelper(
d1mQuote, 1, Months, settlementDays,
calendar, ModifiedFollowing, depositDayCounter));
Handle<RateHelper> d3m(new DepositRateHelper(
d3mQuote, 3, Months, settlementDays,
calendar, ModifiedFollowing, depositDayCounter));
Handle<RateHelper> d6m(new DepositRateHelper(
d6mQuote, 6, Months, settlementDays,
calendar, ModifiedFollowing, depositDayCounter));
Handle<RateHelper> d9m(new DepositRateHelper(
d9mQuote, 9, Months, settlementDays,
calendar, ModifiedFollowing, depositDayCounter));
Handle<RateHelper> d1y(new DepositRateHelper(
d1yQuote, 1, Years, settlementDays,
calendar, ModifiedFollowing, depositDayCounter));
int swFixedLegFrequency = 1;
bool swFixedLegIsAdjusted = false;
DayCounter swFixedLegDayCounter = Thirty360(Thirty360::European);
int swFloatingLegFrequency = 2;
Handle<RateHelper> s2y(new SwapRateHelper(
s2yQuote, 2, Years, settlementDays,
calendar, ModifiedFollowing, swFixedLegFrequency,
swFixedLegIsAdjusted, swFixedLegDayCounter,
swFloatingLegFrequency));
Handle<RateHelper> s3y(new SwapRateHelper(
s3yQuote, 3, Years, settlementDays,
calendar, ModifiedFollowing, swFixedLegFrequency,
swFixedLegIsAdjusted, swFixedLegDayCounter,
swFloatingLegFrequency));
Handle<RateHelper> s5y(new SwapRateHelper(
s5yQuote, 5, Years, settlementDays,
calendar, ModifiedFollowing, swFixedLegFrequency,
swFixedLegIsAdjusted, swFixedLegDayCounter,
swFloatingLegFrequency));
Handle<RateHelper> s10y(new SwapRateHelper(
s10yQuote, 10, Years, settlementDays,
calendar, ModifiedFollowing, swFixedLegFrequency,
swFixedLegIsAdjusted, swFixedLegDayCounter,
swFloatingLegFrequency));
Handle<RateHelper> s15y(new SwapRateHelper(
s15yQuote, 15, Years, settlementDays,
calendar, ModifiedFollowing, swFixedLegFrequency,
swFixedLegIsAdjusted, swFixedLegDayCounter,
swFloatingLegFrequency));
DayCounter termStructureDayCounter =
ActualActual(ActualActual::ISDA);
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(todaysDate, settlementDate,
depoSwapInstruments, termStructureDayCounter));
RelinkableHandle<TermStructure> discountingTermStructure;
RelinkableHandle<TermStructure> forecastingTermStructure;
double nominal = 1000000.0;
int fixedLegFrequency = 1;
bool fixedLegIsAdjusted = false;
RollingConvention roll = ModifiedFollowing;
DayCounter fixedLegDayCounter = Thirty360(Thirty360::European);
int fixingDays = 2;
Rate fixedRate = 0.04;
int floatingLegFrequency = 2;
Handle<Xibor> euriborIndex(new Euribor(6, Months,
forecastingTermStructure));
Spread spread = 0.0;
int lenghtInYears = 5;
bool payFixedRate = true;
SimpleSwap spot5YearSwap(payFixedRate, settlementDate, lenghtInYears,
Years, calendar, roll, nominal, fixedLegFrequency, fixedRate,
fixedLegIsAdjusted, fixedLegDayCounter, floatingLegFrequency,
euriborIndex, fixingDays, spread,
discountingTermStructure);
SimpleSwap oneYearForward5YearSwap(payFixedRate,
calendar.advance(settlementDate, 1, Years, ModifiedFollowing),
lenghtInYears, Years,
calendar, roll, nominal, fixedLegFrequency, fixedRate,
fixedLegIsAdjusted, fixedLegDayCounter, floatingLegFrequency,
euriborIndex, fixingDays, spread,
discountingTermStructure);
std::cout << "*** 5Y swap at 4.43%" << std::endl;
double NPV;
Rate fairFixedRate;
Spread fairFloatingSpread;
forecastingTermStructure.linkTo(depoSwapTermStructure);
discountingTermStructure.linkTo(depoSwapTermStructure);
std::cout << "*** using Depo-Swap term structure:" << std::endl;
NPV = spot5YearSwap.NPV();
std::cout << "5Y "
<< RateFormatter::toString(fixedRate,2)
<< " NPV: "
<< DoubleFormatter::toString(NPV,2)
<< std::endl;
fairFloatingSpread = spot5YearSwap.fairSpread();
std::cout << "5Y "
<< RateFormatter::toString(fixedRate,2)
<< " fair spread: "
<< RateFormatter::toString(fairFloatingSpread,4)
<< std::endl;
fairFixedRate = spot5YearSwap.fairRate();
std::cout << "5Y fair fixed rate: "
<< RateFormatter::toString(fairFixedRate,4)
<< std::endl;
QL_REQUIRE(QL_FABS(fairFixedRate-s5yQuote)<1e-8,
"5 years swap mispriced!");
NPV = oneYearForward5YearSwap.NPV();
std::cout << "1Yx5Y "
<< RateFormatter::toString(fixedRate,2)
<< " NPV: "
<< DoubleFormatter::toString(NPV,2)
<< std::endl;
fairFloatingSpread = oneYearForward5YearSwap.fairSpread();
std::cout << "1Yx5Y "
<< RateFormatter::toString(fixedRate,2)
<< " fair spread: "
<< RateFormatter::toString(fairFloatingSpread,4)
<< std::endl;
fairFixedRate = oneYearForward5YearSwap.fairRate();
std::cout << "1Yx5Y fair fixed rate: "
<< RateFormatter::toString(fairFixedRate,4)
<< std::endl;
Handle<MarketElement> d1wRate(new SimpleMarketElement(d1wQuote));
Handle<MarketElement> d1mRate(new SimpleMarketElement(d1mQuote));
Handle<MarketElement> d3mRate(new SimpleMarketElement(d3mQuote));
Handle<MarketElement> d6mRate(new SimpleMarketElement(d6mQuote));
Handle<MarketElement> d9mRate(new SimpleMarketElement(d9mQuote));
Handle<MarketElement> d1yRate(new SimpleMarketElement(d1yQuote));
Handle<MarketElement> fra3x6Rate(new SimpleMarketElement(fra3x6Quote));
Handle<MarketElement> fra6x9Rate(new SimpleMarketElement(fra6x9Quote));
Handle<MarketElement> fra6x12Rate(new SimpleMarketElement(fra6x12Quote));
Handle<MarketElement> fut1Price(new SimpleMarketElement(fut1Quote));
Handle<MarketElement> fut2Price(new SimpleMarketElement(fut2Quote));
Handle<MarketElement> fut3Price(new SimpleMarketElement(fut3Quote));
Handle<MarketElement> fut4Price(new SimpleMarketElement(fut4Quote));
Handle<MarketElement> fut5Price(new SimpleMarketElement(fut5Quote));
Handle<MarketElement> fut6Price(new SimpleMarketElement(fut6Quote));
Handle<MarketElement> fut7Price(new SimpleMarketElement(fut7Quote));
Handle<MarketElement> fut8Price(new SimpleMarketElement(fut8Quote));
Handle<MarketElement> s2yRate(new SimpleMarketElement(s2yQuote));
Handle<MarketElement> s3yRate(new SimpleMarketElement(s3yQuote));
Handle<MarketElement> s5yRate(new SimpleMarketElement(s5yQuote));
Handle<MarketElement> s10yRate(new SimpleMarketElement(s10yQuote));
Handle<MarketElement> s15yRate(new SimpleMarketElement(s15yQuote));
d1w =Handle<RateHelper>(new DepositRateHelper(
RelinkableHandle<MarketElement>(d1wRate),
1, Weeks, settlementDays,
calendar, ModifiedFollowing, depositDayCounter));
d1m=Handle<RateHelper>(new DepositRateHelper(
RelinkableHandle<MarketElement>(d1mRate),
1, Months, settlementDays,
calendar, ModifiedFollowing, depositDayCounter));
d3m=Handle<RateHelper>(new DepositRateHelper(
RelinkableHandle<MarketElement>(d3mRate),
3, Months, settlementDays,
calendar, ModifiedFollowing, depositDayCounter));
d6m=Handle<RateHelper>(new DepositRateHelper(
RelinkableHandle<MarketElement>(d6mRate),
6, Months, settlementDays,
calendar, ModifiedFollowing, depositDayCounter));
d9m=Handle<RateHelper>(new DepositRateHelper(
RelinkableHandle<MarketElement>(d9mRate),
9, Months, settlementDays,
calendar, ModifiedFollowing, depositDayCounter));
d1y=Handle<RateHelper>(new DepositRateHelper(
RelinkableHandle<MarketElement>(d1yRate),
1, Years, settlementDays,
calendar, ModifiedFollowing, depositDayCounter));
s2y=Handle<RateHelper>(new SwapRateHelper(
RelinkableHandle<MarketElement>(s2yRate),
2, Years, settlementDays,
calendar, ModifiedFollowing, swFixedLegFrequency,
swFixedLegIsAdjusted, swFixedLegDayCounter,
swFloatingLegFrequency));
s3y=Handle<RateHelper>(new SwapRateHelper(
RelinkableHandle<MarketElement>(s3yRate),
3, Years, settlementDays,
calendar, ModifiedFollowing, swFixedLegFrequency,
swFixedLegIsAdjusted, swFixedLegDayCounter,
swFloatingLegFrequency));
s5y=Handle<RateHelper>(new SwapRateHelper(
RelinkableHandle<MarketElement>(s5yRate),
5, Years, settlementDays,
calendar, ModifiedFollowing, swFixedLegFrequency,
swFixedLegIsAdjusted, swFixedLegDayCounter,
swFloatingLegFrequency));
s10y=Handle<RateHelper>(new SwapRateHelper(
RelinkableHandle<MarketElement>(s10yRate),
10, Years, settlementDays,
calendar, ModifiedFollowing, swFixedLegFrequency,
swFixedLegIsAdjusted, swFixedLegDayCounter,
swFloatingLegFrequency));
s15y=Handle<RateHelper>(new SwapRateHelper(
RelinkableHandle<MarketElement>(s15yRate),
15, Years, settlementDays,
calendar, ModifiedFollowing, swFixedLegFrequency,
swFixedLegIsAdjusted, swFixedLegDayCounter,
swFloatingLegFrequency));
Handle<RateHelper> fra3x6(new FraRateHelper(
RelinkableHandle<MarketElement>(fra3x6Rate),
3, 6, settlementDays, calendar, ModifiedFollowing,
depositDayCounter));
Handle<RateHelper> fra6x9(new FraRateHelper(
RelinkableHandle<MarketElement>(fra6x9Rate),
6, 9, settlementDays, calendar, ModifiedFollowing,
depositDayCounter));
Handle<RateHelper> fra6x12(new FraRateHelper(
RelinkableHandle<MarketElement>(fra6x12Rate),
6, 12, settlementDays, calendar, ModifiedFollowing,
depositDayCounter));
int futMonths = 3;
Handle<RateHelper> fut1(new FuturesRateHelper(
RelinkableHandle<MarketElement>(fut1Price),
Date(19, December, 2001),
futMonths, calendar, ModifiedFollowing,
depositDayCounter));
Handle<RateHelper> fut2(new FuturesRateHelper(
RelinkableHandle<MarketElement>(fut1Price),
Date(20, March, 2002),
futMonths, calendar, ModifiedFollowing,
depositDayCounter));
Handle<RateHelper> fut3(new FuturesRateHelper(
RelinkableHandle<MarketElement>(fut1Price),
Date(19, June, 2002),
futMonths, calendar, ModifiedFollowing,
depositDayCounter));
Handle<RateHelper> fut4(new FuturesRateHelper(
RelinkableHandle<MarketElement>(fut1Price),
Date(18, September, 2002),
futMonths, calendar, ModifiedFollowing,
depositDayCounter));
Handle<RateHelper> fut5(new FuturesRateHelper(
RelinkableHandle<MarketElement>(fut1Price),
Date(18, December, 2002),
futMonths, calendar, ModifiedFollowing,
depositDayCounter));
Handle<RateHelper> fut6(new FuturesRateHelper(
RelinkableHandle<MarketElement>(fut1Price),
Date(19, March, 2003),
futMonths, calendar, ModifiedFollowing,
depositDayCounter));
Handle<RateHelper> fut7(new FuturesRateHelper(
RelinkableHandle<MarketElement>(fut1Price),
Date(18, June, 2003),
futMonths, calendar, ModifiedFollowing,
depositDayCounter));
Handle<RateHelper> fut8(new FuturesRateHelper(
RelinkableHandle<MarketElement>(fut1Price),
Date(17, September, 2003),
futMonths, calendar, ModifiedFollowing,
depositDayCounter));
std::vector<Handle<RateHelper> > depoFutSwapInstruments;
depoFutSwapInstruments.push_back(d1w);
depoFutSwapInstruments.push_back(d1m);
depoFutSwapInstruments.push_back(fut1);
depoFutSwapInstruments.push_back(fut2);
depoFutSwapInstruments.push_back(fut3);
depoFutSwapInstruments.push_back(fut4);
depoFutSwapInstruments.push_back(fut5);
depoFutSwapInstruments.push_back(fut6);
depoFutSwapInstruments.push_back(fut7);
depoFutSwapInstruments.push_back(fut8);
depoFutSwapInstruments.push_back(s3y);
depoFutSwapInstruments.push_back(s5y);
depoFutSwapInstruments.push_back(s10y);
depoFutSwapInstruments.push_back(s15y);
Handle<TermStructure> depoFutSwapTermStructure(new
PiecewiseFlatForward(todaysDate, settlementDate,
depoFutSwapInstruments, termStructureDayCounter));
std::vector<Handle<RateHelper> > depoFRASwapInstruments;
depoFRASwapInstruments.push_back(d1w);
depoFRASwapInstruments.push_back(d1m);
depoFRASwapInstruments.push_back(d3m);
depoFRASwapInstruments.push_back(fra3x6);
depoFRASwapInstruments.push_back(fra6x9);
depoFRASwapInstruments.push_back(fra6x12);
depoFRASwapInstruments.push_back(s2y);
depoFRASwapInstruments.push_back(s3y);
depoFRASwapInstruments.push_back(s5y);
depoFRASwapInstruments.push_back(s10y);
depoFRASwapInstruments.push_back(s15y);
Handle<TermStructure> depoFRASwapTermStructure(new
PiecewiseFlatForward(todaysDate, settlementDate,
depoFRASwapInstruments, termStructureDayCounter));
forecastingTermStructure.linkTo(depoFutSwapTermStructure);
discountingTermStructure.linkTo(depoFutSwapTermStructure);
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 = spot5YearSwap.fairSpread();
std::cout << "5Y "
<< RateFormatter::toString(fixedRate,2)
<< " fair spread: "
<< RateFormatter::toString(fairFloatingSpread,4)
<< std::endl;
fairFixedRate = spot5YearSwap.fairRate();
std::cout << "5Y fair fixed rate: "
<< RateFormatter::toString(fairFixedRate,4)
<< std::endl;
QL_REQUIRE(QL_FABS(fairFixedRate-s5yQuote)<1e-8,
"5 years swap mispriced!");
NPV = oneYearForward5YearSwap.NPV();
std::cout << "1Yx5Y "
<< RateFormatter::toString(fixedRate,2)
<< " NPV: "
<< DoubleFormatter::toString(NPV,2)
<< std::endl;
fairFloatingSpread = oneYearForward5YearSwap.fairSpread();
std::cout << "1Yx5Y "
<< RateFormatter::toString(fixedRate,2)
<< " fair spread: "
<< RateFormatter::toString(fairFloatingSpread,4)
<< std::endl;
fairFixedRate = oneYearForward5YearSwap.fairRate();
std::cout << "1Yx5Y fair fixed rate: "
<< RateFormatter::toString(fairFixedRate,4)
<< std::endl;
forecastingTermStructure.linkTo(depoFRASwapTermStructure);
discountingTermStructure.linkTo(depoFRASwapTermStructure);
std::cout << "*** using Depo-FRA-Swap term structure:" << std::endl;
NPV = spot5YearSwap.NPV();
std::cout << "5Y "
<< RateFormatter::toString(fixedRate,2)
<< " NPV: "
<< DoubleFormatter::toString(NPV,2)
<< std::endl;
fairFloatingSpread = spot5YearSwap.fairSpread();
std::cout << "5Y "
<< RateFormatter::toString(fixedRate,2)
<< " fair spread: "
<< RateFormatter::toString(fairFloatingSpread,4)
<< std::endl;
fairFixedRate = spot5YearSwap.fairRate();
std::cout << "5Y fair fixed rate: "
<< RateFormatter::toString(fairFixedRate,4)
<< std::endl;
QL_REQUIRE(QL_FABS(fairFixedRate-s5yRate->value())<1e-8,
"5 years swap mispriced!");
NPV = oneYearForward5YearSwap.NPV();
std::cout << "1Yx5Y "
<< RateFormatter::toString(fixedRate,2)
<< " NPV: "
<< DoubleFormatter::toString(NPV,2)
<< std::endl;
fairFloatingSpread = oneYearForward5YearSwap.fairSpread();
std::cout << "1Yx5Y "
<< RateFormatter::toString(fixedRate,2)
<< " fair spread: "
<< RateFormatter::toString(fairFloatingSpread,4)
<< std::endl;
fairFixedRate = oneYearForward5YearSwap.fairRate();
std::cout << "1Yx5Y fair fixed rate: "
<< RateFormatter::toString(fairFixedRate,4)
<< std::endl;
Handle<SimpleMarketElement> fiveYearsRate = s5yRate;
fiveYearsRate->setValue(0.0460);
std::cout << std::endl << "*** 5Y swap goes up to 4.60%" << std::endl;
forecastingTermStructure.linkTo(depoFutSwapTermStructure);
discountingTermStructure.linkTo(depoFutSwapTermStructure);
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 = spot5YearSwap.fairSpread();
std::cout << "5Y "
<< RateFormatter::toString(fixedRate,2)
<< " fair spread: "
<< RateFormatter::toString(fairFloatingSpread,4)
<< std::endl;
fairFixedRate = spot5YearSwap.fairRate();
std::cout << "5Y fair fixed rate: "
<< RateFormatter::toString(fairFixedRate,4)
<< std::endl;
QL_REQUIRE(QL_FABS(fairFixedRate-s5yRate->value())<1e-8,
"5 years swap mispriced!");
NPV = oneYearForward5YearSwap.NPV();
std::cout << "1Yx5Y "
<< RateFormatter::toString(fixedRate,2)
<< " NPV: "
<< DoubleFormatter::toString(NPV,2)
<< std::endl;
fairFloatingSpread = oneYearForward5YearSwap.fairSpread();
std::cout << "1Yx5Y "
<< RateFormatter::toString(fixedRate,2)
<< " fair spread: "
<< RateFormatter::toString(fairFloatingSpread,4)
<< std::endl;
fairFixedRate = oneYearForward5YearSwap.fairRate();
std::cout << "1Yx5Y fair fixed rate: "
<< RateFormatter::toString(fairFixedRate,4)
<< std::endl;
forecastingTermStructure.linkTo(depoFRASwapTermStructure);
discountingTermStructure.linkTo(depoFRASwapTermStructure);
std::cout << "*** using Depo-FRA-Swap term structure:" << std::endl;
NPV = spot5YearSwap.NPV();
std::cout << "5Y "
<< RateFormatter::toString(fixedRate,2)
<< " NPV: "
<< DoubleFormatter::toString(NPV,2)
<< std::endl;
fairFloatingSpread = spot5YearSwap.fairSpread();
std::cout << "5Y "
<< RateFormatter::toString(fixedRate,2)
<< " fair spread: "
<< RateFormatter::toString(fairFloatingSpread,4)
<< std::endl;
fairFixedRate = spot5YearSwap.fairRate();
std::cout << "5Y fair fixed rate: "
<< RateFormatter::toString(fairFixedRate,4)
<< std::endl;
QL_REQUIRE(QL_FABS(fairFixedRate-s5yRate->value())<1e-8,
"5 years swap mispriced!");
NPV = oneYearForward5YearSwap.NPV();
std::cout << "1Yx5Y "
<< RateFormatter::toString(fixedRate,2)
<< " NPV: "
<< DoubleFormatter::toString(NPV,2)
<< std::endl;
fairFloatingSpread = oneYearForward5YearSwap.fairSpread();
std::cout << "1Yx5Y "
<< RateFormatter::toString(fixedRate,2)
<< " fair spread: "
<< RateFormatter::toString(fairFloatingSpread,4)
<< std::endl;
fairFixedRate = oneYearForward5YearSwap.fairRate();
std::cout << "1Yx5Y fair fixed rate: "
<< RateFormatter::toString(fairFixedRate,4)
<< 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;
}
}