Build QuantLib/QuantExt instrument, link pricing engine. If build() is called multiple times, reset() should be called between these calls.
48 {
49 DLOG(
"ForwardBond::build() called for trade " <<
id());
50
51
56
58
59 const QuantLib::ext::shared_ptr<Market> market = engineFactory->market();
60
61 QuantLib::ext::shared_ptr<EngineBuilder> builder_fwd = engineFactory->builder("ForwardBond");
62 QuantLib::ext::shared_ptr<EngineBuilder> builder_bd = engineFactory->builder("Bond");
63
66
69
72
76
79 bool isPhysicallySettled;
80 if (settlement_ ==
"Physical" ||
settlement_.empty())
81 isPhysicallySettled = true;
82 else if (settlement_ == "Cash")
83 isPhysicallySettled = false;
84 else {
85 QL_FAIL("ForwardBond: invalid settlement '" << settlement_ << "', expected Cash or Physical");
86 }
95
96 QL_REQUIRE((amount == Null<Real>() && lockRate != Null<Real>()) ||
97 (amount != Null<Real>() && lockRate == Null<Real>()),
98 "ForwardBond: exactly one of Amount of LockRate must be given");
99 QL_REQUIRE(dv01 >= 0.0, "negative DV01 given");
100 QL_REQUIRE(compensationPaymentDate <= fwdMaturityDate, "Premium cannot be paid after forward contract maturity");
101
102 if (lockRate != Null<Real>())
103 isPhysicallySettled = false;
104
105 QL_REQUIRE(!
bondData_.
coupons().empty(),
"ForwardBond: No LegData given. If you want to represent a zero bond, "
106 "set it up as a coupon bond with zero fixed rate");
107
109 QL_REQUIRE(firstLegIsPayer == false, "ForwardBond: The underlying bond must be entered with a receiver leg. Use "
110 "LongInBond to specify pay direction of forward payoff");
111 QL_REQUIRE(compensationPayment > 0.0 ||
close_enough(compensationPayment, 0.0),
112 "ForwardBond: Negative compensation payments ("
113 << compensationPayment
114 << ") are not allowed. Notice that we will ensure that a positive compensation amount will be paid "
115 "by the party being long in the forward contract.");
116
117 QuantLib::ext::shared_ptr<Payoff> payoff;
118 if (amount != Null<Real>()) {
119 payoff = longInForward ? QuantLib::ext::make_shared<QuantExt::ForwardBondTypePayoff>(Position::Long, amount)
121 }
122 compensationPayment = longInForward ? compensationPayment : -compensationPayment;
123
124 std::vector<Leg> separateLegs;
126 Leg leg;
127 auto configuration = builder_bd->configuration(MarketContext::pricing);
128 auto legBuilder = engineFactory->legBuilder(
bondData_.
coupons()[i].legType());
129 leg = legBuilder->buildLeg(
bondData_.
coupons()[i], engineFactory, requiredFixings_, configuration);
130 separateLegs.push_back(leg);
131 }
133 auto bond = QuantLib::ext::make_shared<QuantLib::Bond>(settlementDays,
calendar, issueDate, leg);
134
135
140 maturity_ = bond->cashflows().back()->date();
142
143
144 QuantLib::ext::shared_ptr<QuantLib::Instrument> fwdBond =
145 payoff ? QuantLib::ext::make_shared<QuantExt::ForwardBond>(bond, payoff, fwdMaturityDate, fwdSettlementDate,
146 isPhysicallySettled, settlementDirty, compensationPayment,
149 fwdMaturityDate, fwdSettlementDate, isPhysicallySettled,
150 settlementDirty, compensationPayment,
151 compensationPaymentDate,
bondData_.bondNotional(), dv01);
152
153 QuantLib::ext::shared_ptr<fwdBondEngineBuilder> fwdBondBuilder =
154 QuantLib::ext::dynamic_pointer_cast<fwdBondEngineBuilder>(builder_fwd);
155 QL_REQUIRE(fwdBondBuilder, "ForwardBond::build(): could not cast builder: " << id());
156
161 instrument_.reset(
new VanillaInstrument(fwdBond, 1.0));
162
165}
const string & securityId() const
bool hasCreditRisk() const
const std::vector< LegData > & coupons() const
void populateFromBondReferenceData(const QuantLib::ext::shared_ptr< BondReferenceDatum > &referenceDatum, const std::string &startDate="", const std::string &endDate="")
populate data from reference datum and check data for completeness
const string & referenceCurveId() const
const string & issueDate() const
const string & settlementDays() const
const string & calendar() const
const string & incomeCurveId() const
const string & creditCurveId() const
Real bondNotional() const
const string & settlementDirty() const
const string & fwdMaturityDate() const
const string & compensationPayment() const
const string & compensationPaymentDate() const
const string & dv01() const
const string & lockRateDayCounter() const
const string & longInForward() const
const string & lockRate() const
const string & fwdSettlementDate() const
const string & amount() const
std::vector< bool > legPayers_
std::vector< string > legCurrencies_
std::vector< QuantLib::Leg > legs_
void setSensitivityTemplate(const EngineBuilder &builder)
QuantLib::ext::shared_ptr< InstrumentWrapper > instrument_
std::map< std::string, boost::any > additionalData_
Calendar parseCalendar(const string &s)
Convert text to QuantLib::Calendar.
Date parseDate(const string &s)
Convert std::string to QuantLib::Date.
Currency parseCurrency(const string &s)
Convert text to QuantLib::Currency.
bool parseBool(const string &s)
Convert text to bool.
Real parseReal(const string &s)
Convert text to Real.
DayCounter parseDayCounter(const string &s)
Convert text to QuantLib::DayCounter.
#define DLOG(text)
Logging Macro (Level = Debug)
Filter close_enough(const RandomVariable &x, const RandomVariable &y)
Real currentNotional(const Leg &leg)
Size size(const ValueType &v)
Real originalNotional(const Leg &leg)
Leg joinLegs(const std::vector< Leg > &legs)