30 ext::shared_ptr<CommodityIndex> index,
35 const ext::shared_ptr<SecondaryCosts>& secondaryCosts,
40 calendar, payCurrency, receiveCurrency, pricingPeriods, commodityType, secondaryCosts),
41 payReceive_(payer ? 1 : 0), fixedPrice_(
std::move(fixedPrice)),
42 fixedPriceUnitOfMeasure_(
std::move(fixedPriceUnitOfMeasure)), index_(
std::move(index)),
43 payLegTermStructure_(
std::move(payLegTermStructure)),
44 receiveLegTermStructure_(
std::move(receiveLegTermStructure)),
45 discountTermStructure_(
std::move(discountTermStructure)) {
60 if (
index_->forwardCurveEmpty()) {
62 <<
"] does not have any quotes");
65 "index [" +
index_->name() +
66 "] does not have any quotes; "
67 "using forward prices from ["
68 +
index_->forwardCurve()->name() +
"]");
84 Real quantityUomConversionFactor =
89 Real fixedPriceUomConversionFactor =
93 Real indexUomConversionFactor =
98 Real fixedPriceFxConversionFactor =
100 baseCurrency, evaluationDate);
101 Real indexPriceFxConversionFactor =
103 baseCurrency, evaluationDate);
104 Real payLegFxConversionFactor =
109 Real receiveLegFxConversionFactor =
116 if (lastQuoteDate < evaluationDate - 1) {
117 std::ostringstream message;
118 message <<
"index [" <<
index_->name()
119 <<
"] has last quote date of "
124 Real totalQuantityAmount = 0;
128 QL_REQUIRE(pricingPeriod->quantity().amount() != 0,
"quantity is zero");
133 Date periodStartDate =
135 for (
Date stepDate = periodStartDate;
136 stepDate <= pricingPeriod->endDate();
139 bool unrealized = stepDate > evaluationDate;
142 if (stepDate <= lastQuoteDate) {
143 quoteValue =
index_->price(stepDate);
145 quoteValue =
index_->forwardPrice(stepDate);
148 if (quoteValue == 0) {
149 std::ostringstream message;
150 message <<
"pay quote value for curve ["
151 <<
index_->name() <<
"] is 0 for date "
157 "curve [" <<
index_->name() <<
158 "] missing value for pricing date: "
161 Real fixedLegPriceValue =
163 fixedPriceFxConversionFactor;
164 Real floatingLegPriceValue =
165 quoteValue * indexUomConversionFactor *
166 indexPriceFxConversionFactor;
167 Real payLegPriceValue =
169 floatingLegPriceValue;
170 Real receiveLegPriceValue =
176 receiveLegPriceValue, unrealized);
180 Real periodQuantityAmount =
181 pricingPeriod->quantity().amount() *
182 quantityUomConversionFactor;
183 totalQuantityAmount += periodQuantityAmount;
185 Real avgDailyQuantityAmount =
186 periodDayCount == 0 ?
Real(0) :
187 periodQuantityAmount / periodDayCount;
189 Real payLegValue = 0;
190 Real receiveLegValue = 0;
192 dpi !=
dailyPositions_.end() && dpi->first <= pricingPeriod->endDate();
198 payLegValue += -dailyPosition.
payLegPrice * avgDailyQuantityAmount;
199 receiveLegValue += dailyPosition.
receiveLegPrice * avgDailyQuantityAmount;
202 Real discountFactor = 1;
203 Real payLegDiscountFactor = 1;
204 Real receiveLegDiscountFactor = 1;
205 if (pricingPeriod->paymentDate() >= evaluationDate + 2) {
208 pricingPeriod->paymentDate());
209 payLegDiscountFactor =
211 pricingPeriod->paymentDate());
212 receiveLegDiscountFactor =
214 pricingPeriod->paymentDate());
217 Real uDelta = receiveLegValue + payLegValue;
218 Real dDelta = (receiveLegValue * receiveLegDiscountFactor) +
219 (payLegValue * payLegDiscountFactor);
220 Real pmtFxConversionFactor =
221 ((dDelta *
payReceive_) > 0) ? payLegFxConversionFactor :
222 receiveLegFxConversionFactor;
226 Real pmtDiscountFactor =
227 (dDelta > 0) ? receiveLegDiscountFactor :
228 payLegDiscountFactor;
231 ext::make_shared<CommodityCashFlow>(
232 pricingPeriod->paymentDate(),
234 uDelta * discountFactor),
235 Money(baseCurrency, uDelta),
237 dDelta * pmtFxConversionFactor),
239 uDelta * pmtFxConversionFactor),
242 pricingPeriod->paymentDate() <= evaluationDate);
246 totalQuantityAmount, evaluationDate);
253 for (SecondaryCostAmounts::const_iterator i =
256 Real amount = i->second.value();
265 }
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()
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_
void performCalculations() const override
bool isExpired() const override
returns whether the instrument might have value greater than zero.
EnergyVanillaSwap(bool payer, const Calendar &calendar, Money fixedPrice, UnitOfMeasure fixedPriceUnitOfMeasure, ext::shared_ptr< CommodityIndex > index, const Currency &payCurrency, const Currency &receiveCurrency, const PricingPeriods &pricingPeriods, const CommodityType &commodityType, const ext::shared_ptr< SecondaryCosts > &secondaryCosts, Handle< YieldTermStructure > payLegTermStructure, Handle< YieldTermStructure > receiveLegTermStructure, Handle< YieldTermStructure > discountTermStructure)
UnitOfMeasure fixedPriceUnitOfMeasure_
Handle< YieldTermStructure > payLegTermStructure_
ext::shared_ptr< CommodityIndex > index_
Handle< YieldTermStructure > receiveLegTermStructure_
Handle< YieldTermStructure > discountTermStructure_
const char * what() const noexcept override
returns the error message.
virtual bool hasOccurred(const Date &refDate=Date(), ext::optional< bool > includeRefDate=ext::nullopt) const
returns true if an event has already occurred before a date
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