21#include <ql/cashflows/couponpricer.hpp>
22#include <ql/cashflows/fixedratecoupon.hpp>
23#include <ql/cashflows/iborcoupon.hpp>
25#include <boost/make_shared.hpp>
32 const VanillaSwap::Type type,
const std::vector<std::vector<Real>>& trancheNominals,
33 const Schedule& nominalSchedule,
const Size referencedTranche,
const Schedule& fixedSchedule,
34 const std::vector<Real>& fixedRate,
const DayCounter& fixedDayCount,
const Schedule& floatingSchedule,
35 const QuantLib::ext::shared_ptr<IborIndex>& iborIndex,
const std::vector<Real>& gearing,
const std::vector<Real>& spread,
36 const std::vector<Real>& cappedRate,
const std::vector<Real>& flooredRate,
const DayCounter& floatingDayCount,
37 boost::optional<BusinessDayConvention> paymentConvention)
38 : Swap(2), type_(type), trancheNominals_(trancheNominals), nominalSchedule_(nominalSchedule),
39 referencedTranche_(referencedTranche), fixedSchedule_(fixedSchedule), fixedRate_(fixedRate),
40 fixedDayCount_(fixedDayCount), floatingSchedule_(floatingSchedule), iborIndex_(iborIndex), gearing_(gearing),
41 spread_(spread), cappedRate_(cappedRate), flooredRate_(flooredRate), floatingDayCount_(floatingDayCount) {
50 QL_REQUIRE(
nominalSchedule.size() > 0,
"Nominal schedule size is zero");
51 QL_REQUIRE(
nominalSchedule.hasTenor(),
"Nominal schedule needs a tenor");
52 QL_REQUIRE(
nominalSchedule.tenor().frequency() != QuantLib::OtherFrequency,
53 "Nominal schedule tenor (" <<
nominalSchedule.tenor() <<
") not allowed, corresponds to OtherFrequency");
54 QL_REQUIRE(
fixedSchedule.size() > 0,
"Fixed schedule size is zero");
57 "Fixed schedule size (" <<
fixedSchedule_.size() <<
") does not match fixed rate size ("
61 "Floating schedule size (" <<
floatingSchedule_.size() <<
") does not match gearing size ("
64 "Floating schedule size (" <<
floatingSchedule_.size() <<
") does not match spread size ("
67 "Floating schedule size (" <<
floatingSchedule_.size() <<
") does not match spread size ("
70 "Floating schedule size (" <<
floatingSchedule_.size() <<
") does not match spread size ("
73 "fixed schedule size - 1 (" <<
fixedSchedule.size() - 1 <<
") must divide floating schedule size - 1 ("
76 QL_REQUIRE(trancheNominals.size() > 0,
"trancheNominals must be non-empty");
78 "referencedTranche (" <<
referencedTranche <<
") out of range 0..." << (trancheNominals.size() - 1));
79 for (Size i = 0; i < trancheNominals.size(); ++i) {
81 "Tranche nominals at " << i <<
" (" << trancheNominals.size() <<
") do not match nominal schedule ("
86 std::vector<Real> fixedNominal, floatingNominal;
92 floatingNominal.resize(fixedNominal.size() * ratio);
94 std::generate(floatingNominal.begin(), floatingNominal.end(),
95 [i, &fixedNominal, ratio]()
mutable { return fixedNominal[i++ / ratio]; });
103 std::vector<Real> gearingTmp(
gearing_);
104 for (Size i = 0; i < gearingTmp.size(); ++i) {
105 if (close(gearingTmp[i], 0.0))
106 gearingTmp[i] = QL_EPSILON;
110 .withNotionals(fixedNominal)
115 .withNotionals(floatingNominal)
119 .withGearings(gearingTmp)
123 for (Leg::const_iterator i = legs_[1].begin(); i < legs_[1].end(); ++i)
126 auto cpnPricer = QuantLib::ext::make_shared<BlackIborCouponPricer>();
130 case VanillaSwap::Payer:
134 case VanillaSwap::Receiver:
139 QL_FAIL(
"Unknown vanilla swap type");
144 Swap::setupArguments(args);
163 const Leg& fixedCoupons =
fixedLeg();
168 for (Size i = 0; i < fixedCoupons.size(); ++i) {
169 auto coupon = QuantLib::ext::dynamic_pointer_cast<FixedRateCoupon>(fixedCoupons[i]);
170 QL_REQUIRE(coupon !=
nullptr,
"BalanceGuaranteedSwap::setupArguments(): expected fixed rate coupon");
179 std::vector<Date>(floatingCoupons.size());
185 for (Size i = 0; i < floatingCoupons.size(); ++i) {
186 auto coupon = QuantLib::ext::dynamic_pointer_cast<FloatingRateCoupon>(floatingCoupons[i]);
187 QL_REQUIRE(coupon !=
nullptr,
"BalanceGuaranteedSwap::setupArguments(): expected fixed rate coupon");
196 }
catch (
const std::exception&) {
206 QL_REQUIRE(trancheIndex <
trancheNominals_.size(),
"BalanceGuaranteedSwap::trancheNominal(): tranceIndex ("
207 << trancheIndex <<
") out of range 0..."
219 Swap::fetchResults(r);
230 Swap::arguments::validate();
Balance Guaranteed Swap instrument.
Arguments for Balance Guaranteed Swap
std::vector< std::vector< Real > > trancheNominals
std::vector< Real > cappedRate
std::vector< Date > floatingResetDates
QuantLib::ext::shared_ptr< IborIndex > iborIndex
std::vector< Real > floatingGearings
std::vector< Real > floatingSpreads
std::vector< Date > floatingFixingDates
std::vector< Date > trancheNominalDates
std::vector< Real > flooredRate
std::vector< Date > fixedPayDates
QuantLib::Frequency trancheNominalFrequency
std::vector< Date > fixedResetDates
void validate() const override
std::vector< Real > floatingCoupons
std::vector< Real > fixedRate
std::vector< Time > floatingAccrualTimes
std::vector< Real > fixedCoupons
std::vector< Date > floatingPayDates
Results for Balance Guaranteed Swap
void fetchResults(const QuantLib::PricingEngine::results *) const override
const std::vector< Real > fixedRate_
BalanceGuaranteedSwap(const VanillaSwap::Type type, const std::vector< std::vector< Real > > &trancheNominals, const Schedule &nominalSchedule, const Size referencedTranche, const Schedule &fixedSchedule, const std::vector< Real > &fixedRate, const DayCounter &fixedDayCount, const Schedule &floatingSchedule, const QuantLib::ext::shared_ptr< IborIndex > &iborIndex, const std::vector< Real > &gearing, const std::vector< Real > &spread, const std::vector< Real > &cappedRate, const std::vector< Real > &flooredRate, const DayCounter &floatingDayCount, boost::optional< BusinessDayConvention > paymentConvention=boost::none)
BusinessDayConvention paymentConvention() const
const Schedule nominalSchedule_
const Size referencedTranche_
const Schedule & fixedSchedule() const
const std::vector< std::vector< Real > > trancheNominals_
const Schedule fixedSchedule_
const Leg & floatingLeg() const
const std::vector< Real > cappedRate_
const Schedule & floatingSchedule() const
const QuantLib::ext::shared_ptr< IborIndex > & iborIndex() const
const Size referencedTranche() const
const DayCounter fixedDayCount_
const std::vector< Real > flooredRate_
const Schedule floatingSchedule_
BusinessDayConvention paymentConvention_
const VanillaSwap::Type type_
const Leg & fixedLeg() const
void setupExpired() const override
const std::vector< Real > gearing_
void setupArguments(QuantLib::PricingEngine::arguments *) const override
const DayCounter floatingDayCount_
const Schedule & nominalSchedule() const
const QuantLib::ext::shared_ptr< IborIndex > iborIndex_
const std::vector< std::vector< Real > > & trancheNominal() const
const std::vector< Real > spread_
void setCouponPricer(const Leg &leg, const QuantLib::ext::shared_ptr< FloatingRateCouponPricer > &pricer)
Set Coupon Pricer.