Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
Public Member Functions | List of all members
CommodityFixedLegBuilder Class Reference

#include <ored/portfolio/commoditylegbuilder.hpp>

+ Inheritance diagram for CommodityFixedLegBuilder:
+ Collaboration diagram for CommodityFixedLegBuilder:

Public Member Functions

 CommodityFixedLegBuilder ()
 
QuantLib::Leg buildLeg (const ore::data::LegData &data, const QuantLib::ext::shared_ptr< ore::data::EngineFactory > &engineFactory, RequiredFixings &requiredFixings, const std::string &configuration, const QuantLib::Date &openEndDateReplacement=Null< Date >(), const bool useXbsCurves=false) const override
 
- Public Member Functions inherited from LegBuilder
 LegBuilder (const string &legType)
 
virtual ~LegBuilder ()
 
virtual Leg buildLeg (const LegData &data, const QuantLib::ext::shared_ptr< EngineFactory > &, RequiredFixings &requiredFixings, const string &configuration, const QuantLib::Date &openEndDateReplacement=Null< Date >(), const bool useXbsCurves=false) const =0
 
const string & legType () const
 

Detailed Description

Definition at line 33 of file commoditylegbuilder.hpp.

Constructor & Destructor Documentation

◆ CommodityFixedLegBuilder()

Definition at line 35 of file commoditylegbuilder.hpp.

35: LegBuilder("CommodityFixed") {}
LegBuilder(const string &legType)

Member Function Documentation

◆ buildLeg()

Leg buildLeg ( const ore::data::LegData data,
const QuantLib::ext::shared_ptr< ore::data::EngineFactory > &  engineFactory,
RequiredFixings requiredFixings,
const std::string &  configuration,
const QuantLib::Date &  openEndDateReplacement = Null<Date>(),
const bool  useXbsCurves = false 
) const
override

Definition at line 311 of file commoditylegbuilder.cpp.

313 {
314
315 // Check that our leg data has commodity fixed leg data
316 auto fixedLegData = QuantLib::ext::dynamic_pointer_cast<CommodityFixedLegData>(data.concreteLegData());
317 QL_REQUIRE(fixedLegData, "Wrong LegType, expected CommodityFixed, got " << data.legType());
318
319 // Build our schedule and get the quantities and prices
320 Schedule schedule = makeSchedule(data.schedule(),openEndDateReplacement);
321 vector<Real> prices = buildScheduledVector(fixedLegData->prices(), fixedLegData->priceDates(), schedule);
322 vector<Real> quantities = buildScheduledVector(fixedLegData->quantities(), fixedLegData->quantityDates(), schedule);
323
324 // Build fixed rate leg with 1/1 day counter, prices as rates and quantities as notionals so that we have price x
325 // notional in each period as the amount. We don't make any payment date adjustments yet as they come later.
326 OneDayCounter dc;
327 Leg fixedRateLeg = FixedRateLeg(schedule)
328 .withNotionals(quantities)
329 .withCouponRates(prices, dc)
330 .withPaymentAdjustment(Unadjusted)
331 .withPaymentLag(0)
332 .withPaymentCalendar(NullCalendar());
333
334 // Get explicit payment dates which in most cases should be empty
335 vector<Date> paymentDates;
336 if (!data.paymentDates().empty()) {
337 paymentDates = parseVectorOfValues<Date>(data.paymentDates(), &parseDate);
338 if (fixedLegData->commodityPayRelativeTo() == CommodityPayRelativeTo::FutureExpiryDate) {
339 QL_REQUIRE(paymentDates.size() == fixedRateLeg.size(),
340 "Expected the number of payment dates derived from float leg with tag '"
341 << fixedLegData->tag() << "' (" << paymentDates.size()
342 << ") to equal the number of fixed price periods (" << fixedRateLeg.size()
343 << "). Are the leg schedules consistent? Should CommodityPayRelativeTo = FutureExpiryDate "
344 "be used?");
345 } else {
346 QL_REQUIRE(paymentDates.size() == fixedRateLeg.size(),
347 "Expected the number of explicit payment dates ("
348 << paymentDates.size() << ") to equal the number of fixed price periods ("
349 << fixedRateLeg.size() << ")");
350 }
351 }
352
353 // Create the commodity fixed leg.
354 Leg commodityFixedLeg;
355 for (Size i = 0; i < fixedRateLeg.size(); i++) {
356
357 auto cp = QuantLib::ext::dynamic_pointer_cast<FixedRateCoupon>(fixedRateLeg[i]);
358
359 // Get payment date
360 Date pmtDate;
361 if (!paymentDates.empty()) {
362 // Explicit payment dates were provided
363 Calendar paymentCalendar =
364 data.paymentCalendar().empty() ? NullCalendar() : parseCalendar(data.paymentCalendar());
365 PaymentLag paymentLag = parsePaymentLag(data.paymentLag());
366 Period paymentLagPeriod = boost::apply_visitor(PaymentLagPeriod(), paymentLag);
367 BusinessDayConvention paymentConvention =
368 data.paymentConvention().empty() ? Unadjusted : parseBusinessDayConvention(data.paymentConvention());
369 pmtDate = paymentCalendar.advance(paymentDates[i], paymentLagPeriod, paymentConvention);
370 } else {
371 // Gather the payment conventions.
372 BusinessDayConvention bdc =
373 data.paymentConvention().empty() ? Following : parseBusinessDayConvention(data.paymentConvention());
374
375 Calendar paymentCalendar =
376 data.paymentCalendar().empty() ? schedule.calendar() : parseCalendar(data.paymentCalendar());
377
378 PaymentLag payLag = parsePaymentLag(data.paymentLag());
379 Period paymentLag = boost::apply_visitor(PaymentLagPeriod(), payLag);
380
381 // Get unadjusted payment date based on pay relative to value.
382 if (fixedLegData->commodityPayRelativeTo() == CommodityPayRelativeTo::CalculationPeriodEndDate) {
383 pmtDate = cp->accrualEndDate();
384 } else if (fixedLegData->commodityPayRelativeTo() == CommodityPayRelativeTo::CalculationPeriodStartDate) {
385 pmtDate = cp->accrualStartDate();
386 } else if (fixedLegData->commodityPayRelativeTo() == CommodityPayRelativeTo::TerminationDate) {
387 pmtDate = fixedRateLeg.back()->date();
388 } else if (fixedLegData->commodityPayRelativeTo() == CommodityPayRelativeTo::FutureExpiryDate) {
389 QL_FAIL("Internal error: commodity fixed leg builder can not determine payment date relative to future "
390 "expiry date, this has to be handled in the instrument builder.");
391 } else {
392 QL_FAIL("Unexpected value " << fixedLegData->commodityPayRelativeTo() << " for CommodityPayRelativeTo");
393 }
394
395 // Adjust the payment date using the payment conventions
396 pmtDate = paymentCalendar.advance(pmtDate, paymentLag, bdc);
397 }
398
399 // Create the fixed cashflow for this period
400 commodityFixedLeg.push_back(QuantLib::ext::make_shared<SimpleCashFlow>(cp->amount(), pmtDate));
401 }
402
403 applyIndexing(commodityFixedLeg, data, engineFactory, requiredFixings, openEndDateReplacement, useXbsCurves);
404 addToRequiredFixings(commodityFixedLeg, QuantLib::ext::make_shared<FixingDateGetter>(requiredFixings));
405
406 addToRequiredFixings(commodityFixedLeg, QuantLib::ext::make_shared<ore::data::FixingDateGetter>(requiredFixings));
407
408 return commodityFixedLeg;
409}
Calendar parseCalendar(const string &s)
Convert text to QuantLib::Calendar.
Definition: parsers.cpp:157
BusinessDayConvention parseBusinessDayConvention(const string &s)
Convert text to QuantLib::BusinessDayConvention.
Definition: parsers.cpp:173
PaymentLag parsePaymentLag(const string &s)
Convert text to PaymentLag.
Definition: parsers.cpp:628
@ data
Definition: log.hpp:77
void applyIndexing(Leg &leg, const LegData &data, const QuantLib::ext::shared_ptr< EngineFactory > &engineFactory, RequiredFixings &requiredFixings, const QuantLib::Date &openEndDateReplacement, const bool useXbsCurves)
Definition: legdata.cpp:2633
void addToRequiredFixings(const QuantLib::Leg &leg, const QuantLib::ext::shared_ptr< FixingDateGetter > &fixingDateGetter)
vector< T > buildScheduledVector(const vector< T > &values, const vector< string > &dates, const Schedule &schedule, const bool checkAllValuesAppearInResult=false)
Definition: legdata.hpp:1061
boost::variant< QuantLib::Period, QuantLib::Natural > PaymentLag
Definition: types.hpp:32
Schedule makeSchedule(const ScheduleDates &data)
Definition: schedule.cpp:263
+ Here is the call graph for this function: