Re: Fair Rate of Overnight Indexed Swap
Posted by Fabio on
URL: http://quantlib.414.s1.nabble.com/Fair-Rate-of-Overnight-Indexed-Swap-tp15053p15056.html
Ok, I managed to work it out. I post here a possible solution.
#pragma warning(disable:4996)
#include <iostream>
#include <vector>
#include <ql/quantlib.hpp>
using namespace std;
using namespace boost;
using namespace QuantLib;
int main(){
// GENERAL SETTINGS
Calendar calendar = TARGET();
Date settlementDate(13, December, 2012);
Integer settlementDays = -2;
Date today = calendar.advance(settlementDate, settlementDays, Days);
Settings::instance().evaluationDate() = today;
// DATES AND DISCOUNT FACTORS
vector<Date> dates(0);
vector<DiscountFactor> discountFactors(0);
for (int i=0; i<13; i++){
dates.push_back(settlementDate + i*Years);
}
for (int i=0; i<13; i++){
discountFactors.push_back(1.0 - i*0.01);
}
// DISCOUNT CURVE CONSTRUCTION
boost::shared_ptr<YieldTermStructure> cirOisCurve;
cirOisCurve = boost::shared_ptr<YieldTermStructure>(new InterpolatedDiscountCurve <LogLinear>(dates, discountFactors, Actual360()));
Handle<YieldTermStructure> discountingTermStructure(cirOisCurve);
boost::shared_ptr<OvernightIndex> eonia(new Eonia(discountingTermStructure));
// SWAP SET-UP
Date previousResetDate(today);
Date maturity(20, December, 2012);
Real nominal = 100000.0;
Spread spread = 0.0;
Rate fixedRate = 0.00070;
OvernightIndexedSwap::Type type = OvernightIndexedSwap::Payer;
BusinessDayConvention legConvention = ModifiedFollowing;
Frequency legFrequency = Annual;
eonia->addFixing(eonia->fixingDate(previousResetDate),0.01,true);
Schedule schedule(settlementDate, maturity, Period(legFrequency), TARGET(), legConvention, legConvention, DateGeneration::Forward, false);
OvernightIndexedSwap ois_swap(type, nominal, schedule, fixedRate, Thirty360(), eonia, spread);
// SWAP PRICING
boost::shared_ptr<PricingEngine> swapEngine(new DiscountingSwapEngine(discountingTermStructure));
ois_swap.setPricingEngine(swapEngine);
double npv = ois_swap.NPV();
double fairRate = ois_swap.fairRate();
cout << "Fair Rate: " << fairRate << endl;
cout << "Net Present Value: " << npv << endl;
getchar();
return 0;
}