27 ext::shared_ptr<CommodityIndex> spreadIndex,
28 ext::shared_ptr<CommodityIndex> payIndex,
29 ext::shared_ptr<CommodityIndex> receiveIndex,
36 const ext::shared_ptr<SecondaryCosts>& secondaryCosts,
41 calendar, payCurrency, receiveCurrency, pricingPeriods, commodityType, secondaryCosts),
42 spreadIndex_(
std::move(spreadIndex)), payIndex_(
std::move(payIndex)),
43 receiveIndex_(
std::move(receiveIndex)), spreadToPayLeg_(spreadToPayLeg),
44 basis_(
std::move(basis)), payLegTermStructure_(
std::move(payLegTermStructure)),
45 receiveLegTermStructure_(
std::move(receiveLegTermStructure)),
46 discountTermStructure_(
std::move(discountTermStructure)) {
60 "] does not have any quotes or forward prices");
64 "] does not have any quotes; "
65 "using forward prices from [" +
72 "] does not have any quotes or forward prices");
76 "] does not have any quotes; "
77 "using forward prices from [" +
95 Real quantityUomConversionFactor =
100 Real payIndexUomConversionFactor =
104 Real receiveIndexUomConversionFactor =
109 Real payIndexFxConversionFactor =
111 baseCurrency, evaluationDate);
112 Real receiveIndexFxConversionFactor =
114 baseCurrency, evaluationDate);
115 Real payLegFxConversionFactor =
118 Real receiveLegFxConversionFactor =
122 Real basisUomConversionFactor =
126 Real basisFxConversionFactor =
132 basisUomConversionFactor * basisFxConversionFactor;
137 if (lastPayIndexQuoteDate < evaluationDate - 1) {
138 std::ostringstream message;
139 message <<
"index [" <<
payIndex_->name()
140 <<
"] has last quote date of "
144 if (lastReceiveIndexQuoteDate < evaluationDate - 1) {
145 std::ostringstream message;
147 <<
"] has last quote date of "
152 Date lastQuoteDate = std::min(lastPayIndexQuoteDate,
153 lastReceiveIndexQuoteDate);
155 Real totalQuantityAmount = 0;
162 Date periodStartDate =
164 for (
Date stepDate = periodStartDate;
165 stepDate <= pricingPeriod->endDate();
168 bool unrealized = stepDate > evaluationDate;
169 Real payQuoteValue = 0;
170 Real receiveQuoteValue = 0;
172 if (stepDate <= lastQuoteDate) {
173 payQuoteValue =
payIndex_->price(stepDate);
176 payQuoteValue =
payIndex_->forwardPrice(stepDate);
181 if (payQuoteValue == 0) {
182 std::ostringstream message;
183 message <<
"pay quote value for curve ["
184 <<
payIndex_->name() <<
"] is 0 for date "
188 if (receiveQuoteValue == 0) {
189 std::ostringstream message;
190 message <<
"receive quote value for curve ["
198 "] missing value for pricing date: "
202 "] missing value for pricing date: "
205 Real payLegPriceValue =
206 payQuoteValue * payIndexUomConversionFactor *
207 payIndexFxConversionFactor;
208 Real receiveLegPriceValue =
209 receiveQuoteValue * receiveIndexUomConversionFactor *
210 receiveIndexFxConversionFactor;
213 payLegPriceValue += basisValue;
215 receiveLegPriceValue += basisValue;
219 receiveLegPriceValue, unrealized);
223 Real periodQuantityAmount =
224 pricingPeriod->quantity().amount() *
225 quantityUomConversionFactor;
226 totalQuantityAmount += periodQuantityAmount;
228 Real avgDailyQuantityAmount =
229 periodDayCount == 0 ?
Real(0) :
230 periodQuantityAmount / periodDayCount;
232 Real payLegValue = 0;
233 Real receiveLegValue = 0;
235 dpi !=
dailyPositions_.end() && dpi->first <= pricingPeriod->endDate();
241 payLegValue += -dailyPosition.
payLegPrice * avgDailyQuantityAmount;
242 receiveLegValue += dailyPosition.
receiveLegPrice * avgDailyQuantityAmount;
245 Real discountFactor = 1;
246 Real payLegDiscountFactor = 1;
247 Real receiveLegDiscountFactor = 1;
248 if (pricingPeriod->paymentDate() >= evaluationDate + 2 ) {
251 pricingPeriod->paymentDate());
252 payLegDiscountFactor =
254 pricingPeriod->paymentDate());
255 receiveLegDiscountFactor =
257 pricingPeriod->paymentDate());
260 Real uDelta = receiveLegValue + payLegValue;
261 Real dDelta = (receiveLegValue * receiveLegDiscountFactor) +
262 (payLegValue * payLegDiscountFactor);
263 Real pmtFxConversionFactor =
264 (dDelta > 0) ? payLegFxConversionFactor : receiveLegFxConversionFactor;
267 Real pmtDiscountFactor =
268 (dDelta > 0) ? receiveLegDiscountFactor : payLegDiscountFactor;
271 ext::make_shared<CommodityCashFlow> (
272 pricingPeriod->paymentDate(),
274 uDelta * discountFactor),
275 Money(baseCurrency, uDelta),
277 dDelta * pmtFxConversionFactor),
279 uDelta * pmtFxConversionFactor),
282 pricingPeriod->paymentDate() <= evaluationDate);
286 totalQuantityAmount, evaluationDate);
293 for (SecondaryCostAmounts::const_iterator i =
296 Real amount = i->second.value();
305 }
catch (
const std::exception& e) {
Date adjust(const Date &, BusinessDayConvention convention=Following) const
Date advance(const Date &, Integer n, TimeUnit unit, BusinessDayConvention convention=Following, bool endOfMonth=false) const
void addPricingError(PricingError::Level errorLevel, const std::string &error, const std::string &detail="") const
SecondaryCostAmounts secondaryCostAmounts_
UnitOfMeasure & unitOfMeasure()
const Money & amount() const
const UnitOfMeasure & unitOfMeasure() const
void performCalculations() const override
ext::shared_ptr< CommodityIndex > payIndex_
ext::shared_ptr< CommodityIndex > spreadIndex_
Handle< YieldTermStructure > payLegTermStructure_
EnergyBasisSwap(const Calendar &calendar, ext::shared_ptr< CommodityIndex > spreadIndex, ext::shared_ptr< CommodityIndex > payIndex, ext::shared_ptr< CommodityIndex > receiveIndex, bool spreadToPayLeg, const Currency &payCurrency, const Currency &receiveCurrency, const PricingPeriods &pricingPeriods, CommodityUnitCost basis, const CommodityType &commodityType, const ext::shared_ptr< SecondaryCosts > &secondaryCosts, Handle< YieldTermStructure > payLegTermStructure, Handle< YieldTermStructure > receiveLegTermStructure, Handle< YieldTermStructure > discountTermStructure)
ext::shared_ptr< CommodityIndex > receiveIndex_
Handle< YieldTermStructure > receiveLegTermStructure_
Handle< YieldTermStructure > discountTermStructure_
static Real calculateUomConversionFactor(const CommodityType &commodityType, const UnitOfMeasure &fromUnitOfMeasure, const UnitOfMeasure &toUnitOfMeasure)
void calculateSecondaryCostAmounts(const CommodityType &commodityType, Real totalQuantityValue, const Date &evaluationDate) const
static Real calculateFxConversionFactor(const Currency &fromCurrency, const Currency &toCurrency, const Date &evaluationDate)
EnergyDailyPositions dailyPositions_
PricingPeriods pricingPeriods_
Quantity quantity() const override
Currency receiveCurrency_
const CommodityType & commodityType() const
CommodityCashFlows paymentCashFlows_
const char * what() const noexcept override
returns the error message.
Shared handle to an observable.
std::map< std::string, ext::any > additionalResults_
const Currency & currency() const
template class providing a null value for a given type.
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
DateProxy & evaluationDate()
the date at which pricing is to be performed.
static Settings & instance()
access to the unique instance
Unit of measure specification
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
#define QL_FAIL(message)
throw an error (possibly with file and line information)
detail::iso_date_holder iso_date(const Date &d)
output dates in ISO format (yyyy-mm-dd)
QL_INTEGER Integer
integer number
std::vector< ext::shared_ptr< PricingPeriod > > PricingPeriods