1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2008 J. Erik Radmall
5
6 This file is part of QuantLib, a free-software/open-source library
7 for financial quantitative analysts and developers - http://quantlib.org/
8
9 QuantLib is free software: you can redistribute it and/or modify it
10 under the terms of the QuantLib license. You should have received a
11 copy of the license along with this program; if not, please email
12 <quantlib-dev@lists.sf.net>. The license is also available online at
13 <http://quantlib.org/license.shtml>.
14
15 This program is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the license for more details.
18*/
19
20#include <ql/experimental/commodities/commoditysettings.hpp>
21#include <ql/experimental/commodities/energyfuture.hpp>
22#include <ql/settings.hpp>
23#include <utility>
24
25namespace QuantLib {
26
27 EnergyFuture::EnergyFuture(Integer buySell,
28 Quantity quantity,
29 CommodityUnitCost tradePrice,
30 ext::shared_ptr<CommodityIndex> index,
31 const CommodityType& commodityType,
32 const ext::shared_ptr<SecondaryCosts>& secondaryCosts)
33 : EnergyCommodity(commodityType, secondaryCosts), buySell_(buySell),
34 quantity_(std::move(quantity)), tradePrice_(std::move(tradePrice)), index_(std::move(index)) {
35 registerWith(h: Settings::instance().evaluationDate());
36 registerWith(h: index_);
37 }
38
39 bool EnergyFuture::isExpired() const {
40 return false;
41 }
42
43 void EnergyFuture::performCalculations() const {
44
45 NPV_ = 0.0;
46 additionalResults_.clear();
47
48 Date evaluationDate = Settings::instance().evaluationDate();
49 const Currency& baseCurrency =
50 CommoditySettings::instance().currency();
51 const UnitOfMeasure baseUnitOfMeasure =
52 CommoditySettings::instance().unitOfMeasure();
53
54 Real quantityUomConversionFactor =
55 calculateUomConversionFactor(
56 commodityType: quantity_.commodityType(),
57 fromUnitOfMeasure: baseUnitOfMeasure,
58 toUnitOfMeasure: quantity_.unitOfMeasure()) * index_->lotQuantity();
59 Real indexUomConversionFactor =
60 calculateUomConversionFactor(commodityType: index_->commodityType(),
61 fromUnitOfMeasure: index_->unitOfMeasure(),
62 toUnitOfMeasure: baseUnitOfMeasure);
63 Real tradePriceUomConversionFactor =
64 calculateUomConversionFactor(commodityType: quantity_.commodityType(),
65 fromUnitOfMeasure: tradePrice_.unitOfMeasure(),
66 toUnitOfMeasure: baseUnitOfMeasure);
67
68 Real tradePriceFxConversionFactor =
69 calculateFxConversionFactor(fromCurrency: tradePrice_.amount().currency(),
70 toCurrency: baseCurrency, evaluationDate);
71 Real indexPriceFxConversionFactor =
72 calculateFxConversionFactor(fromCurrency: index_->currency(), toCurrency: baseCurrency,
73 evaluationDate);
74
75 Real quoteValue = 0;
76
77 Date lastQuoteDate = index_->lastQuoteDate();
78 if (lastQuoteDate >= evaluationDate - 1) {
79 quoteValue = index_->price(date: evaluationDate);
80 } else {
81 quoteValue = index_->forwardPrice(date: evaluationDate);
82 std::ostringstream message;
83 message << "curve [" << index_->name()
84 << "] has last quote date of "
85 << io::iso_date(lastQuoteDate)
86 << " using forward price from ["
87 << index_->forwardCurve()->name() << "]";
88 addPricingError(errorLevel: PricingError::Warning, error: message.str());
89 }
90
91 QL_REQUIRE(quoteValue != Null<Real>(),
92 "missing quote for [" << index_->name() << "]");
93
94 Real tradePriceValue =
95 tradePrice_.amount().value() * tradePriceUomConversionFactor
96 * tradePriceFxConversionFactor;
97 Real quotePriceValue = quoteValue * indexUomConversionFactor
98 * indexPriceFxConversionFactor;
99
100 Real quantityAmount = quantity_.amount() * quantityUomConversionFactor;
101
102 Real delta = (((quotePriceValue - tradePriceValue) * quantityAmount)
103 * index_->lotQuantity()) * buySell_;
104
105 NPV_ = delta;
106
107 calculateSecondaryCostAmounts(commodityType: quantity_.commodityType(),
108 totalQuantityValue: quantity_.amount(), evaluationDate);
109 for (SecondaryCostAmounts::const_iterator i = secondaryCostAmounts_.begin(); i != secondaryCostAmounts_.end(); ++i) {
110 Real amount = i->second.value();
111 NPV_ -= amount;
112 }
113
114 // additionalResults_["brokerCommission"] =
115 // -(brokerCommissionValue * quantityAmount);
116 }
117
118}
119
120

source code of quantlib/ql/experimental/commodities/energyfuture.cpp