builing a 1-month US libor forward curve in quantlib using PiecewideYieldCurve- need help

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

builing a 1-month US libor forward curve in quantlib using PiecewideYieldCurve- need help

Kumar Aiyer
I have deposit rates and swap rates from Bloomberg (S23 swap curve for those who are familiar with bloomberg)
I want to generate a forward 1-month US Libor curve with a 1 month frequency (for use with monthly cashflows).
 
I am not sure how I should use the PiecewiseYieldCurve class for this purpose. Can an expert in this mailing list give me some guidance.
many thanks
 
Kumar Aiyer
 
=====================code starts here=======================================

rdb->buildForwards<Discount,LogLinear,IterativeBootstrap>( dt1 , rdb->getSwapCurve());

 

template <class T, class I, template<class C> class B>

void buildForwards(QuantLib::Date& settlementDate, const YieldCurve& yc, const I& interpolator = I()) {

/*********************

*** MARKET DATA ***

*********************/

Calendar calendar = QuantLib::TARGET();

// must be a business day

settlementDate = calendar.adjust(settlementDate);

// assume fixings for same date

Integer fixingDays = 0;

Natural curveSettlementDays = 0;

Date currentDate = calendar.advance(settlementDate, -fixingDays, Days);

// nothing to do with Date::todaysDate

Settings::instance().evaluationDate() = currentDate;

Period OneMonth(1, TimeUnit::Months);

std::cout << "Today: " << currentDate.weekday()

<< ", " << currentDate << std::endl;

std::cout << "Settlement date: " << settlementDate.weekday()

<< ", " << settlementDate << std::endl;

// deposit rates

Rate d1dQuote = yc.day1.get()/100.0;

Rate d2dQuote = yc.day2.get()/100.0;

Rate d1wQuote = yc.week1.get()/100.0;

Rate d2wQuote = yc.week2.get()/100.0;

Rate d1mQuote = yc.month1.get()/100.0;

Rate d2mQuote = yc.month2.get()/100.0;

Rate d3mQuote = yc.month3.get()/100.0;

Rate d4mQuote = yc.month4.get()/100.0;

Rate d5mQuote = yc.month5.get()/100.0;

Rate d6mQuote = yc.month6.get()/100.0;

Rate d7mQuote = yc.month7.get()/100.0;

Rate d8mQuote = yc.month8.get()/100.0;

Rate d9mQuote = yc.month9.get()/100.0;

Rate d10mQuote = yc.month10.get()/100.0;

Rate d11mQuote = yc.month11.get()/100.0;

Rate d1yQuote = yc.year1.get()/100.0;

// swaps

Rate s2yQuote = yc.year2.get()/100.0;

Rate s3yQuote = yc.year3.get()/100.0;

Rate s4yQuote =yc.year4.get()/100.0;

Rate s5yQuote = yc.year5.get()/100.0;

Rate s6yQuote = yc.year6.get()/100.0;

Rate s7yQuote = yc.year7.get()/100.0;

Rate s8yQuote = yc.year8.get()/100.0;

Rate s9yQuote = yc.year9.get()/100.0;

Rate s10yQuote = yc.year10.get()/100.0;

Rate s11yQuote = yc.year11.get()/100.0;

Rate s12yQuote = yc.year12.get()/100.0;

Rate s15yQuote = yc.year15.get()/100.0;

Rate s20yQuote = yc.year20.get()/100.0;

Rate s25yQuote = yc.year25.get()/100.0;

Rate s30yQuote = yc.year30.get()/100.0;

Rate s40yQuote = yc.year40.get()/100.0;

/********************

*** 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.

// deposits

boost::shared_ptr<Quote> d1dRate(new SimpleQuote(d1dQuote));

boost::shared_ptr<Quote> d2dRate(new SimpleQuote(d2dQuote));

boost::shared_ptr<Quote> d1wRate(new SimpleQuote(d1wQuote));

boost::shared_ptr<Quote> d2wRate(new SimpleQuote(d2wQuote));

boost::shared_ptr<Quote> d1mRate(new SimpleQuote(d1mQuote));

boost::shared_ptr<Quote> d2mRate(new SimpleQuote(d2mQuote));

boost::shared_ptr<Quote> d3mRate(new SimpleQuote(d3mQuote));

boost::shared_ptr<Quote> d4mRate(new SimpleQuote(d4mQuote));

boost::shared_ptr<Quote> d5mRate(new SimpleQuote(d5mQuote));

boost::shared_ptr<Quote> d6mRate(new SimpleQuote(d6mQuote));

boost::shared_ptr<Quote> d7mRate(new SimpleQuote(d7mQuote));

boost::shared_ptr<Quote> d8mRate(new SimpleQuote(d8mQuote));

boost::shared_ptr<Quote> d9mRate(new SimpleQuote(d9mQuote));

boost::shared_ptr<Quote> d10mRate(new SimpleQuote(d10mQuote));

boost::shared_ptr<Quote> d11mRate(new SimpleQuote(d11mQuote));

boost::shared_ptr<Quote> d1yRate(new SimpleQuote(d1yQuote));

// swaps

boost::shared_ptr<Quote> s2yRate(new SimpleQuote(s2yQuote));

boost::shared_ptr<Quote> s3yRate(new SimpleQuote(s3yQuote));

boost::shared_ptr<Quote> s4yRate(new SimpleQuote(s4yQuote));

boost::shared_ptr<Quote> s5yRate(new SimpleQuote(s5yQuote));

boost::shared_ptr<Quote> s6yRate(new SimpleQuote(s6yQuote));

boost::shared_ptr<Quote> s7yRate(new SimpleQuote(s7yQuote));

boost::shared_ptr<Quote> s8yRate(new SimpleQuote(s8yQuote));

boost::shared_ptr<Quote> s9yRate(new SimpleQuote(s9yQuote));

boost::shared_ptr<Quote> s10yRate(new SimpleQuote(s10yQuote));

boost::shared_ptr<Quote> s11yRate(new SimpleQuote(s11yQuote));

boost::shared_ptr<Quote> s12yRate(new SimpleQuote(s12yQuote));

boost::shared_ptr<Quote> s15yRate(new SimpleQuote(s15yQuote));

boost::shared_ptr<Quote> s20yRate(new SimpleQuote(s20yQuote));

boost::shared_ptr<Quote> s25yRate(new SimpleQuote(s25yQuote));

boost::shared_ptr<Quote> s30yRate(new SimpleQuote(s30yQuote));

boost::shared_ptr<Quote> s40yRate(new SimpleQuote(s40yQuote));

/*********************

*** 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.

// deposits

// days

DayCounter depositDayCounter = QuantLib::Actual360();

boost::shared_ptr<RateHelper> d1d(new DepositRateHelper(

Handle<Quote>(d1dRate),

1*Days, fixingDays,

calendar, ModifiedFollowing,

true, depositDayCounter));

boost::shared_ptr<RateHelper> d2d(new DepositRateHelper(

Handle<Quote>(d2dRate),

2*Days, fixingDays,

calendar, ModifiedFollowing,

true, depositDayCounter));

// weeks

boost::shared_ptr<RateHelper> d1w(new DepositRateHelper(

Handle<Quote>(d1wRate),

1*Weeks, fixingDays,

calendar, ModifiedFollowing,

true, depositDayCounter));

boost::shared_ptr<RateHelper> d2w(new DepositRateHelper(

Handle<Quote>(d2wRate),

2*Weeks, fixingDays,

calendar, ModifiedFollowing,

true, depositDayCounter));

// months

boost::shared_ptr<RateHelper> d1m(new DepositRateHelper(

Handle<Quote>(d1mRate),

1*Months, fixingDays,

calendar, ModifiedFollowing,

true, depositDayCounter));

boost::shared_ptr<RateHelper> d2m(new DepositRateHelper(

Handle<Quote>(d2mRate),

2*Months, fixingDays,

calendar, ModifiedFollowing,

true, depositDayCounter));

boost::shared_ptr<RateHelper> d3m(new DepositRateHelper(

Handle<Quote>(d3mRate),

3*Months, fixingDays,

calendar, ModifiedFollowing,

true, depositDayCounter));

boost::shared_ptr<RateHelper> d4m(new DepositRateHelper(

Handle<Quote>(d4mRate),

4*Months, fixingDays,

calendar, ModifiedFollowing,

true, depositDayCounter));

boost::shared_ptr<RateHelper> d5m(new DepositRateHelper(

Handle<Quote>(d5mRate),

5*Months, fixingDays,

calendar, ModifiedFollowing,

true, depositDayCounter));

boost::shared_ptr<RateHelper> d6m(new DepositRateHelper(

Handle<Quote>(d6mRate),

6*Months, fixingDays,

calendar, ModifiedFollowing,

true, depositDayCounter));

boost::shared_ptr<RateHelper> d7m(new DepositRateHelper(

Handle<Quote>(d7mRate),

7*Months, fixingDays,

calendar, ModifiedFollowing,

true, depositDayCounter));

boost::shared_ptr<RateHelper> d8m(new DepositRateHelper(

Handle<Quote>(d8mRate),

8*Months, fixingDays,

calendar, ModifiedFollowing,

true, depositDayCounter));

boost::shared_ptr<RateHelper> d9m(new DepositRateHelper(

Handle<Quote>(d9mRate),

9*Months, fixingDays,

calendar, ModifiedFollowing,

true, depositDayCounter));

boost::shared_ptr<RateHelper> d10m(new DepositRateHelper(

Handle<Quote>(d10mRate),

10*Months, fixingDays,

calendar, ModifiedFollowing,

true, depositDayCounter));

boost::shared_ptr<RateHelper> d11m(new DepositRateHelper(

Handle<Quote>(d11mRate),

11*Months, fixingDays,

calendar, ModifiedFollowing,

true, depositDayCounter));

boost::shared_ptr<RateHelper> d1y(new DepositRateHelper(

Handle<Quote>(d1yRate),

1*Years, fixingDays,

calendar, ModifiedFollowing,

true, depositDayCounter));

// setup swaps

Frequency swFixedLegFrequency = Annual;

BusinessDayConvention swFixedLegConvention = Unadjusted;

DayCounter swFixedLegDayCounter = Thirty360(Thirty360::European);

boost::shared_ptr<IborIndex> swFloatingLegIndex(new USDLibor(OneMonth));

boost::shared_ptr<RateHelper> s2y(new SwapRateHelper(

Handle<Quote>(s2yRate), 2*Years,

calendar, swFixedLegFrequency,

swFixedLegConvention, swFixedLegDayCounter,

swFloatingLegIndex));

boost::shared_ptr<RateHelper> s3y(new SwapRateHelper(

Handle<Quote>(s3yRate), 3*Years,

calendar, swFixedLegFrequency,

swFixedLegConvention, swFixedLegDayCounter,

swFloatingLegIndex));

boost::shared_ptr<RateHelper> s4y(new SwapRateHelper(

Handle<Quote>(s4yRate), 4*Years,

calendar, swFixedLegFrequency,

swFixedLegConvention, swFixedLegDayCounter,

swFloatingLegIndex));

boost::shared_ptr<RateHelper> s5y(new SwapRateHelper(

Handle<Quote>(s5yRate), 5*Years,

calendar, swFixedLegFrequency,

swFixedLegConvention, swFixedLegDayCounter,

swFloatingLegIndex));

boost::shared_ptr<RateHelper> s6y(new SwapRateHelper(

Handle<Quote>(s6yRate), 6*Years,

calendar, swFixedLegFrequency,

swFixedLegConvention, swFixedLegDayCounter,

swFloatingLegIndex));

boost::shared_ptr<RateHelper> s7y(new SwapRateHelper(

Handle<Quote>(s7yRate), 7*Years,

calendar, swFixedLegFrequency,

swFixedLegConvention, swFixedLegDayCounter,

swFloatingLegIndex));

boost::shared_ptr<RateHelper> s8y(new SwapRateHelper(

Handle<Quote>(s8yRate), 8*Years,

calendar, swFixedLegFrequency,

swFixedLegConvention, swFixedLegDayCounter,

swFloatingLegIndex));

boost::shared_ptr<RateHelper> s9y(new SwapRateHelper(

Handle<Quote>(s9yRate), 9*Years,

calendar, swFixedLegFrequency,

swFixedLegConvention, swFixedLegDayCounter,

swFloatingLegIndex));

boost::shared_ptr<RateHelper> s10y(new SwapRateHelper(

Handle<Quote>(s10yRate), 10*Years,

calendar, swFixedLegFrequency,

swFixedLegConvention, swFixedLegDayCounter,

swFloatingLegIndex));

boost::shared_ptr<RateHelper> s11y(new SwapRateHelper(

Handle<Quote>(s11yRate), 11*Years,

calendar, swFixedLegFrequency,

swFixedLegConvention, swFixedLegDayCounter,

swFloatingLegIndex));

boost::shared_ptr<RateHelper> s12y(new SwapRateHelper(

Handle<Quote>(s12yRate), 12*Years,

calendar, swFixedLegFrequency,

swFixedLegConvention, swFixedLegDayCounter,

swFloatingLegIndex));

boost::shared_ptr<RateHelper> s15y(new SwapRateHelper(

Handle<Quote>(s15yRate), 15*Years,

calendar, swFixedLegFrequency,

swFixedLegConvention, swFixedLegDayCounter,

swFloatingLegIndex));

boost::shared_ptr<RateHelper> s20y(new SwapRateHelper(

Handle<Quote>(s20yRate), 20*Years,

calendar, swFixedLegFrequency,

swFixedLegConvention, swFixedLegDayCounter,

swFloatingLegIndex));

boost::shared_ptr<RateHelper> s25y(new SwapRateHelper(

Handle<Quote>(s25yRate), 25*Years,

calendar, swFixedLegFrequency,

swFixedLegConvention, swFixedLegDayCounter,

swFloatingLegIndex));

boost::shared_ptr<RateHelper> s30y(new SwapRateHelper(

Handle<Quote>(s30yRate), 30*Years,

calendar, swFixedLegFrequency,

swFixedLegConvention, swFixedLegDayCounter,

swFloatingLegIndex));

boost::shared_ptr<RateHelper> s40y(new SwapRateHelper(

Handle<Quote>(s40yRate), 40*Years,

calendar, swFixedLegFrequency,

swFixedLegConvention, swFixedLegDayCounter,

swFloatingLegIndex));

/*********************

** CURVE BUILDING **

*********************/

DayCounter termStructureDayCounter =

Actual360();

Real tolerance = 1.0e-7;

Size max = 5000;

// A depo-swap curve

std::vector<boost::shared_ptr<RateHelper> > depoSwapInstruments;

if (!yc.day1.IsNull())

depoSwapInstruments.push_back(d1d);

if (!yc.day2.IsNull())

depoSwapInstruments.push_back(d2d);

if (!yc.week1.IsNull())

depoSwapInstruments.push_back(d1w);

if (!yc.week2.IsNull())

depoSwapInstruments.push_back(d2w);

if (!yc.month1.IsNull())

depoSwapInstruments.push_back(d1m);

if (!yc.month2.IsNull())

depoSwapInstruments.push_back(d2m);

if (!yc.month3.IsNull())

depoSwapInstruments.push_back(d3m);

if (!yc.month4.IsNull())

depoSwapInstruments.push_back(d4m);

if (!yc.month5.IsNull())

depoSwapInstruments.push_back(d5m);

if (!yc.month6.IsNull())

depoSwapInstruments.push_back(d6m);

if (!yc.month7.IsNull())

depoSwapInstruments.push_back(d7m);

if (!yc.month8.IsNull())

depoSwapInstruments.push_back(d8m);

if (!yc.month9.IsNull())

depoSwapInstruments.push_back(d9m);

if (!yc.month10.IsNull())

depoSwapInstruments.push_back(d10m);

if (!yc.month11.IsNull())

depoSwapInstruments.push_back(d11m);

if (!yc.year1.IsNull())

depoSwapInstruments.push_back(d1y);

if (!yc.year2.IsNull())

depoSwapInstruments.push_back(s2y);

if (!yc.year3.IsNull())

depoSwapInstruments.push_back(s3y);

if (!yc.year4.IsNull())

depoSwapInstruments.push_back(s4y);

if (!yc.year5.IsNull())

depoSwapInstruments.push_back(s5y);

if (!yc.year6.IsNull())

depoSwapInstruments.push_back(s6y);

if (!yc.year7.IsNull())

depoSwapInstruments.push_back(s7y);

if (!yc.year8.IsNull())

depoSwapInstruments.push_back(s8y);

if (!yc.year9.IsNull())

depoSwapInstruments.push_back(s9y);

if (!yc.year10.IsNull())

depoSwapInstruments.push_back(s10y);

if (!yc.year11.IsNull())

depoSwapInstruments.push_back(s11y);

if (!yc.year12.IsNull())

depoSwapInstruments.push_back(s12y);

if (!yc.year15.IsNull())

depoSwapInstruments.push_back(s15y);

if (!yc.year20.IsNull())

depoSwapInstruments.push_back(s20y);

if (!yc.year25.IsNull())

depoSwapInstruments.push_back(s25y);

if (!yc.year30.IsNull())

depoSwapInstruments.push_back(s30y);

if (!yc.year40.IsNull())

depoSwapInstruments.push_back(s40y);

boost::shared_ptr<YieldTermStructure> depoSwapTermStructure (

new PiecewiseYieldCurve<T,I,B>(

settlementDate, depoSwapInstruments,

termStructureDayCounter,

std::vector<Handle<Quote> >(),

std::vector<Date>(),

tolerance,

interpolator));

RelinkableHandle<YieldTermStructure> forecastingTermStructure;

forecastingTermStructure.linkTo(depoSwapTermStructure);

// Loop over period of term structure.

// do it for 360 months

int i;

int mos = 360;

std::vector<Rate> fwdRates(mos);

USDLibor libor1M(OneMonth,forecastingTermStructure);

Period indexTenor = libor1M.tenor();

DayCounter indexDayCounter = libor1M.dayCounter();

ofstream fpout("c:/temp/yc.txt", ios::out);

for (i=0; i<mos; i++) {

currentDate = calendar.advance(currentDate, OneMonth, Following);

// move the evaluationDate to currentDate

// and update ratehelpers dates...

//Settings::instance().evaluationDate() = currentDate;

try {

fwdRates[i] = libor1M.fixing(currentDate);

//fwdRates[i] = forecastingTermStructure->forwardRate(currentDate,indexTenor,indexDayCounter,Simple);

}

catch (QuantLib::Error& e) {

cerr << "RatesDB:: " << e.what() << endl;

}

fpout << "date: " << currentDate << ", fwdRate[" << i << "]: " << fwdRates[i] << endl;

}

fpout.close();

return;

}

#endif


------------------------------------------------------------------------------
Enter the BlackBerry Developer Challenge  
This is your chance to win up to $100,000 in prizes! For a limited time,
vendors submitting new applications to BlackBerry App World(TM) will have
the opportunity to enter the BlackBerry Developer Challenge. See full prize  
details at: http://p.sf.net/sfu/Challenge
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users