Posted by
Luigi Ballabio-2 on
URL: http://quantlib.414.s1.nabble.com/Swap-start-date-and-length-tp2655p2656.html
At 03:49 PM 9/8/03 -0700, navin mehta wrote:
>Given a swap object, how can I get the start date and length ? (I tried to
>find it in the heirarchy,and even the Swaphelper, but could not find any
>public method)
Navin,
I'm afraid this has been overlooked. You'll need either to change
the source files, or rely on some arcane incantation as the one I'm writing
below.
Would you mind filing this into the Feature Request page on Sourceforge
(
http://sourceforge.net/tracker/?group_id=12740&atid=362740) so that we
don't forget to include a solution in next release?
Thanks,
Luigi
--------------------------------
#include <ql/Instruments/swap.hpp>
#include <ql/CashFlows/coupon.hpp>
namespace QuantLib {
namespace Instruments {
/* This class below inherits from Swap in order to gain access
to its protected members.
Don't try this kind of hacks at home.
Oh, and did I mention this isn't tested?
*/
class SwapInspector : public Swap {
private:
// we don't really want to instantiate this
SwapInspector()
: Swap(std::vector<Handle<CashFlow> >(),
std::vector<Handle<CashFlow> >(),
RelinkableHandle<TermStructure>()) {}
public:
// return the start date of the first coupon
static Date startDate(const Swap& swap) {
// get the first cash flow
Handle<CashFlow> firstCF = swap.firstLeg_.front();
try {
// is this a coupon?
Handle<CashFlows::Coupon> coupon = firstCF;
// ok, return its start date
return coupon.accrualStartDate();
} catch (Error&) {
// it was not a coupon. We'll doctor the
// error message to make it more readable.
throw Error("The swap coupons did not provide "
"enough information");
}
}
// return the end date of the last coupon
static Date endDate(const Swap& swap) {
Handle<CashFlow> lastCF = swap.firstLeg_.back();
try {
Handle<CashFlows::Coupon> coupon = lastCF;
return coupon.accrualEndDate();
} catch (Error&) {
throw Error("The swap coupons did not provide "
"enough information");
}
}
};
}
}