Update of /cvsroot/quantlibaddin/QuantLibAddin/qlo
In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv17035/qlo
Modified Files:
.cvsignore termstructures.cpp termstructures.hpp
Added Files:
ratehelpers.cpp ratehelpers.hpp
Log Message:
ratehelpes in their own files
Index: termstructures.hpp
===================================================================
RCS file: /cvsroot/quantlibaddin/QuantLibAddin/qlo/termstructures.hpp,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -d -r1.4 -r1.5
*** termstructures.hpp 15 Jun 2006 18:07:56 -0000 1.4
--- termstructures.hpp 15 Jun 2006 20:23:55 -0000 1.5
***************
*** 23,28 ****
#include <ql/termstructure.hpp>
- #include <ql/Indexes/xibor.hpp>
- #include <ql/calendar.hpp>
#include <ql/TermStructures/piecewiseflatforward.hpp>
#include <ql/TermStructures/forwardspreadedtermstructure.hpp>
--- 23,26 ----
***************
*** 33,86 ****
namespace QuantLibAddin {
- class RateHelper : public ObjHandler::LibraryObject<QuantLib::RateHelper> {
- public:
- double setQuote(double quote);
- QuantLib::Handle<QuantLib::Quote> quoteHandle() const {
- return quoteHandle_;
- }
- protected:
- boost::shared_ptr<QuantLib::SimpleQuote> quote_;
- QuantLib::Handle<QuantLib::Quote> quoteHandle_;
- };
-
- class DepositRateHelper : public RateHelper {
- public:
- DepositRateHelper(
- const double "e,
- const QuantLib::Period& p,
- const long &fixingDays,
- const QuantLib::Calendar& calendar,
- const QuantLib::BusinessDayConvention &convention,
- const QuantLib::DayCounter &dayCounter);
- };
-
- class FuturesRateHelper : public RateHelper {
- public:
- FuturesRateHelper(
- const double &price,
- const std::string &immDateID,
- const QuantLib::Integer &months,
- const QuantLib::Calendar& calendar,
- const QuantLib::BusinessDayConvention &bDayConvention,
- const QuantLib::DayCounter &dayCounter);
- };
-
- class SwapRateHelper : public RateHelper {
- public:
- SwapRateHelper(
- const double "e,
- const QuantLib::Period& p,
- const long &fixingDays,
- const QuantLib::Calendar& calendar,
- const QuantLib::Frequency &fixedFrequency,
- const QuantLib::BusinessDayConvention &fixedConvention,
- const QuantLib::DayCounter &fixedDayCounter,
- const boost::shared_ptr<QuantLib::Xibor>& index);
- };
-
class YieldTermStructure : public ObjHandler::LibraryObject<QuantLib::YieldTermStructure> {
};
-
class PiecewiseYieldCurve : public YieldTermStructure {
public:
--- 31,37 ----
***************
*** 123,132 ****
};
- std::vector<std::string> qlRateHelperSelection(
- const std::vector<std::string>& instrumentHandles,
- const std::vector<bool>& includeFlag,
- const std::vector<long>& priority,
- const long& nFutures);
-
}
--- 74,77 ----
Index: .cvsignore
===================================================================
RCS file: /cvsroot/quantlibaddin/QuantLibAddin/qlo/.cvsignore,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -d -r1.4 -r1.5
*** .cvsignore 14 Jun 2006 18:34:31 -0000 1.4
--- .cvsignore 15 Jun 2006 20:23:55 -0000 1.5
***************
*** 20,23 ****
--- 20,24 ----
vo_processes.*pp
vo_randomsequencegenerator.*pp
+ vo_ratehelpers.*pp
vo_schedule.*pp
vo_shortratemodels.*pp
Index: termstructures.cpp
===================================================================
RCS file: /cvsroot/quantlibaddin/QuantLibAddin/qlo/termstructures.cpp,v
retrieving revision 1.7
retrieving revision 1.8
diff -C2 -d -r1.7 -r1.8
*** termstructures.cpp 15 Jun 2006 18:07:56 -0000 1.7
--- termstructures.cpp 15 Jun 2006 20:23:55 -0000 1.8
***************
*** 24,27 ****
--- 24,28 ----
#include <oh/objhandlerdefines.hpp>
#include <qlo/termstructures.hpp>
+ #include <qlo/ratehelpers.hpp>
#include <qlo/typefactory.hpp>
#include <qlo/generalutils.hpp>
***************
*** 34,112 ****
namespace QuantLibAddin {
- double RateHelper::setQuote(double quote) {
- double diff = quote - quote_->value();
- quote_->setValue(quote);
- return diff;
- }
-
- DepositRateHelper::DepositRateHelper(
- const double "e,
- const QuantLib::Period& p,
- const long &fixingDays,
- const QuantLib::Calendar& calendar,
- const QuantLib::BusinessDayConvention &convention,
- const QuantLib::DayCounter &dayCounter) {
-
- quote_ = boost::shared_ptr<QuantLib::SimpleQuote>(
- new QuantLib::SimpleQuote(quote));
- quoteHandle_.linkTo(quote_);
-
- libraryObject_ = boost::shared_ptr<QuantLib::RateHelper>(
- new QuantLib::DepositRateHelper(quoteHandle_,
- p,
- fixingDays,
- calendar,
- convention,
- dayCounter));
- }
-
- FuturesRateHelper::FuturesRateHelper(
- const double &price,
- const std::string &immDateID,
- const QuantLib::Integer &months,
- const QuantLib::Calendar& calendar,
- const QuantLib::BusinessDayConvention &bDayConvention,
- const QuantLib::DayCounter &dayCounter) {
-
- quote_ = boost::shared_ptr<QuantLib::SimpleQuote>(new QuantLib::SimpleQuote(price));
- quoteHandle_.linkTo(quote_);
-
- QuantLib::Date expiry = QuantLib::Date::IMMdate(immDateID);
-
- libraryObject_ = boost::shared_ptr<QuantLib::RateHelper>(
- new QuantLib::FuturesRateHelper(
- quoteHandle_,
- expiry,
- months,
- calendar,
- bDayConvention,
- dayCounter));
- }
-
- SwapRateHelper::SwapRateHelper(
- const double "e,
- const QuantLib::Period& p,
- const long &fixingDays,
- const QuantLib::Calendar& calendar,
- const QuantLib::Frequency &fixedFrequency,
- const QuantLib::BusinessDayConvention &fixedConvention,
- const QuantLib::DayCounter &fixedDayCounter,
- const boost::shared_ptr<QuantLib::Xibor>& index) {
-
- quote_ = boost::shared_ptr<QuantLib::SimpleQuote>(new QuantLib::SimpleQuote(quote));
- quoteHandle_.linkTo(quote_);
-
- libraryObject_ = boost::shared_ptr<QuantLib::RateHelper>(
- new QuantLib::SwapRateHelper(quoteHandle_,
- p,
- fixingDays,
- calendar,
- fixedFrequency,
- fixedConvention,
- fixedDayCounter,
- index));
- }
-
-
PiecewiseYieldCurve::PiecewiseYieldCurve(
const long &nDays,
--- 35,38 ----
***************
*** 205,310 ****
}
- // helper class
- namespace detail {
-
- struct RateHelperItem {
- QuantLib::Date latestDate;
- long priority;
- std::string instanceName;
- RateHelperItem(
- const QuantLib::Date& latestDate,
- const long& priority,
- const std::string& instanceName)
- : latestDate(latestDate), priority(priority), instanceName(instanceName) {}
- };
-
- class RateHelperPrioritySorter {
- public:
- // does h1 come before h2?
- bool operator()(const RateHelperItem& h1,
- const RateHelperItem& h2) const {
-
- if (h1.latestDate > h2.latestDate)
- return false;
-
- if (h1.latestDate == h2.latestDate) {
- if (h1.priority > h2.priority) {
- return false;
- } else if (h1.priority == h2.priority) {
- return h1.instanceName > h2.instanceName;
- }
- }
-
- return true;
-
- }
- };
- }
-
- std::vector<std::string> qlRateHelperSelection(
- const std::vector<std::string>& instrumentHandles,
- const std::vector<bool>& includeFlag,
- const std::vector<long>& priority,
- const long& nFutures) {
-
- QL_REQUIRE(!instrumentHandles.empty(), "no instrument given");
-
- QuantLib::Size nInstruments = instrumentHandles.size();
- QL_REQUIRE(includeFlag.size()==nInstruments,
- "includeFlag / instruments mismatch");
- QL_REQUIRE(priority.size()==nInstruments,
- "priority / instruments mismatch");
-
- std::vector<boost::shared_ptr<RateHelper> > instruments;
- for (std::vector<std::string>::const_iterator it = instrumentHandles.begin();
- it != instrumentHandles.end(); it++) {
- OH_GET_OBJECT(objectPointer, *it, RateHelper)
- instruments.push_back(objectPointer);
- }
-
- // purge input rate helpers according to their includeFlag,
- // their expiration, and maximum number of allowed futures
- std::vector<detail::RateHelperItem> rhs;
- QuantLib::Size i;
- long futuresCounter = 0;
- QuantLib::Date earliestDate, evalDate = QuantLib::Settings::instance().evaluationDate();
- for (i=0; i<nInstruments; i++) {
- const boost::shared_ptr < QuantLib::RateHelper > temp =
- instruments[i]->getLibraryObject < QuantLib::RateHelper >();
- earliestDate = temp->earliestDate();
- if (includeFlag[i]) {
- if (!boost::dynamic_pointer_cast<FuturesRateHelper>(instruments[i]) && (earliestDate >= evalDate)) {
- rhs.push_back(detail::RateHelperItem(temp->latestDate(),
- priority[i], instrumentHandles[i]));
- } else if (futuresCounter<nFutures && (earliestDate-2 >= evalDate)) {
- futuresCounter++;
- rhs.push_back(detail::RateHelperItem(temp->latestDate(),
- priority[i], instrumentHandles[i]));
- }
- }
- }
-
- std::vector<std::string> instanceNames;
-
- // zero or one rate helper left
- if (rhs.size()<2) {
- std::vector<detail::RateHelperItem>::const_iterator i;
- for (i = rhs.begin(); i != rhs.end(); i++)
- instanceNames.push_back(i->instanceName);
- return instanceNames;
- }
-
- // sort rate helpers according to their latest date and priority
- std::sort(rhs.begin(), rhs.end(), detail::RateHelperPrioritySorter());
-
- for (i=0; i<rhs.size()-1; i++) {
- if (rhs[i].latestDate < rhs[i+1].latestDate)
- instanceNames.push_back(rhs[i].instanceName);
- }
- // add the last one in any case
- instanceNames.push_back(rhs[i].instanceName);
-
- return instanceNames;
- }
}
--- 131,134 ----
--- NEW FILE: ratehelpers.hpp ---
/*
Copyright (C) 2005, 2006 Eric Ehlers
Copyright (C) 2006 Ferdinando Ametrano
Copyright (C) 2005 Plamen Neykov
Copyright (C) 2005 Aurelien Chanudet
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 qua...@li...
The license is also available online at http://quantlib.org/html/license.html
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.
*/
#ifndef qla_ratehelpers_hpp
#define qla_ratehelpers_hpp
#include <ql/Indexes/xibor.hpp>
#include <ql/TermStructures/ratehelpers.hpp>
#include <oh/objhandler.hpp>
namespace QuantLibAddin {
class RateHelper : public ObjHandler::LibraryObject<QuantLib::RateHelper> {
public:
double setQuote(double quote);
QuantLib::Handle<QuantLib::Quote> quoteHandle() const {
return quoteHandle_;
}
protected:
boost::shared_ptr<QuantLib::SimpleQuote> quote_;
QuantLib::Handle<QuantLib::Quote> quoteHandle_;
};
class DepositRateHelper : public RateHelper {
public:
DepositRateHelper(
const double "e,
const QuantLib::Period& p,
const long &fixingDays,
const QuantLib::Calendar& calendar,
const QuantLib::BusinessDayConvention &convention,
const QuantLib::DayCounter &dayCounter);
};
class FuturesRateHelper : public RateHelper {
public:
FuturesRateHelper(
const double &price,
const std::string &immDateID,
const QuantLib::Integer &months,
const QuantLib::Calendar& calendar,
const QuantLib::BusinessDayConvention &bDayConvention,
const QuantLib::DayCounter &dayCounter);
};
class SwapRateHelper : public RateHelper {
public:
SwapRateHelper(
const double "e,
const QuantLib::Period& p,
const long &fixingDays,
const QuantLib::Calendar& calendar,
const QuantLib::Frequency &fixedFrequency,
const QuantLib::BusinessDayConvention &fixedConvention,
const QuantLib::DayCounter &fixedDayCounter,
const boost::shared_ptr<QuantLib::Xibor>& index);
};
std::vector<std::string> qlRateHelperSelection(
const std::vector<std::string>& instrumentHandles,
const std::vector<bool>& includeFlag,
const std::vector<long>& priority,
const long& nFutures);
}
#endif
--- NEW FILE: ratehelpers.cpp ---
/*
Copyright (C) 2005, 2006 Eric Ehlers
Copyright (C) 2006 Ferdinando Ametrano
Copyright (C) 2005 Plamen Neykov
Copyright (C) 2005 Aurelien Chanudet
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 qua...@li...
The license is also available online at http://quantlib.org/html/license.html
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.
*/
#ifdef HAVE_CONFIG_H
#include <qlo/config.hpp>
#endif
#include <oh/objhandlerdefines.hpp>
#include <qlo/ratehelpers.hpp>
#include <qlo/typefactory.hpp>
#include <qlo/generalutils.hpp>
#include <ql/date.hpp>
#include <ql/Math/cubicspline.hpp>
#include <ql/TermStructures/discountcurve.hpp>
#include <ql/TermStructures/forwardcurve.hpp>
namespace QuantLibAddin {
double RateHelper::setQuote(double quote) {
double diff = quote - quote_->value();
quote_->setValue(quote);
return diff;
}
DepositRateHelper::DepositRateHelper(
const double "e,
const QuantLib::Period& p,
const long &fixingDays,
const QuantLib::Calendar& calendar,
const QuantLib::BusinessDayConvention &convention,
const QuantLib::DayCounter &dayCounter) {
quote_ = boost::shared_ptr<QuantLib::SimpleQuote>(
new QuantLib::SimpleQuote(quote));
quoteHandle_.linkTo(quote_);
libraryObject_ = boost::shared_ptr<QuantLib::RateHelper>(
new QuantLib::DepositRateHelper(quoteHandle_,
p,
fixingDays,
calendar,
convention,
dayCounter));
}
FuturesRateHelper::FuturesRateHelper(
const double &price,
const std::string &immDateID,
const QuantLib::Integer &months,
const QuantLib::Calendar& calendar,
const QuantLib::BusinessDayConvention &bDayConvention,
const QuantLib::DayCounter &dayCounter) {
quote_ = boost::shared_ptr<QuantLib::SimpleQuote>(new QuantLib::SimpleQuote(price));
quoteHandle_.linkTo(quote_);
QuantLib::Date expiry = QuantLib::Date::IMMdate(immDateID);
libraryObject_ = boost::shared_ptr<QuantLib::RateHelper>(
new QuantLib::FuturesRateHelper(
quoteHandle_,
expiry,
months,
calendar,
bDayConvention,
dayCounter));
}
SwapRateHelper::SwapRateHelper(
const double "e,
const QuantLib::Period& p,
const long &fixingDays,
const QuantLib::Calendar& calendar,
const QuantLib::Frequency &fixedFrequency,
const QuantLib::BusinessDayConvention &fixedConvention,
const QuantLib::DayCounter &fixedDayCounter,
const boost::shared_ptr<QuantLib::Xibor>& index) {
quote_ = boost::shared_ptr<QuantLib::SimpleQuote>(new QuantLib::SimpleQuote(quote));
quoteHandle_.linkTo(quote_);
libraryObject_ = boost::shared_ptr<QuantLib::RateHelper>(
new QuantLib::SwapRateHelper(quoteHandle_,
p,
fixingDays,
calendar,
fixedFrequency,
fixedConvention,
fixedDayCounter,
index));
}
// helper class
namespace detail {
struct RateHelperItem {
QuantLib::Date latestDate;
long priority;
std::string instanceName;
RateHelperItem(
const QuantLib::Date& latestDate,
const long& priority,
const std::string& instanceName)
: latestDate(latestDate), priority(priority), instanceName(instanceName) {}
};
class RateHelperPrioritySorter {
public:
// does h1 come before h2?
bool operator()(const RateHelperItem& h1,
const RateHelperItem& h2) const {
if (h1.latestDate > h2.latestDate)
return false;
if (h1.latestDate == h2.latestDate) {
if (h1.priority > h2.priority) {
return false;
} else if (h1.priority == h2.priority) {
return h1.instanceName > h2.instanceName;
}
}
return true;
}
};
}
std::vector<std::string> qlRateHelperSelection(
const std::vector<std::string>& instrumentHandles,
const std::vector<bool>& includeFlag,
const std::vector<long>& priority,
const long& nFutures) {
QL_REQUIRE(!instrumentHandles.empty(), "no instrument given");
QuantLib::Size nInstruments = instrumentHandles.size();
QL_REQUIRE(includeFlag.size()==nInstruments,
"includeFlag / instruments mismatch");
QL_REQUIRE(priority.size()==nInstruments,
"priority / instruments mismatch");
std::vector<boost::shared_ptr<RateHelper> > instruments;
for (std::vector<std::string>::const_iterator it = instrumentHandles.begin();
it != instrumentHandles.end(); it++) {
OH_GET_OBJECT(objectPointer, *it, RateHelper)
instruments.push_back(objectPointer);
}
// purge input rate helpers according to their includeFlag,
// their expiration, and maximum number of allowed futures
std::vector<detail::RateHelperItem> rhs;
QuantLib::Size i;
long futuresCounter = 0;
QuantLib::Date earliestDate, evalDate = QuantLib::Settings::instance().evaluationDate();
for (i=0; i<nInstruments; i++) {
const boost::shared_ptr < QuantLib::RateHelper > temp =
instruments[i]->getLibraryObject < QuantLib::RateHelper >();
earliestDate = temp->earliestDate();
if (includeFlag[i]) {
if (!boost::dynamic_pointer_cast<FuturesRateHelper>(instruments[i]) && (earliestDate >= evalDate)) {
rhs.push_back(detail::RateHelperItem(temp->latestDate(),
priority[i], instrumentHandles[i]));
} else if (futuresCounter<nFutures && (earliestDate-2 >= evalDate)) {
futuresCounter++;
rhs.push_back(detail::RateHelperItem(temp->latestDate(),
priority[i], instrumentHandles[i]));
}
}
}
std::vector<std::string> instanceNames;
// zero or one rate helper left
if (rhs.size()<2) {
std::vector<detail::RateHelperItem>::const_iterator i;
for (i = rhs.begin(); i != rhs.end(); i++)
instanceNames.push_back(i->instanceName);
return instanceNames;
}
// sort rate helpers according to their latest date and priority
std::sort(rhs.begin(), rhs.end(), detail::RateHelperPrioritySorter());
for (i=0; i<rhs.size()-1; i++) {
if (rhs[i].latestDate < rhs[i+1].latestDate)
instanceNames.push_back(rhs[i].instanceName);
}
// add the last one in any case
instanceNames.push_back(rhs[i].instanceName);
return instanceNames;
}
}
|