QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
energyfuture.cpp
Go to the documentation of this file.
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
22#include <ql/settings.hpp>
23#include <utility>
24
25namespace QuantLib {
26
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(Settings::instance().evaluationDate());
37 }
38
40 return false;
41 }
42
44
45 NPV_ = 0.0;
46 additionalResults_.clear();
47
48 Date evaluationDate = Settings::instance().evaluationDate();
49 const Currency& baseCurrency =
51 const UnitOfMeasure baseUnitOfMeasure =
53
54 Real quantityUomConversionFactor =
57 baseUnitOfMeasure,
58 quantity_.unitOfMeasure()) * index_->lotQuantity();
59 Real indexUomConversionFactor =
60 calculateUomConversionFactor(index_->commodityType(),
61 index_->unitOfMeasure(),
62 baseUnitOfMeasure);
63 Real tradePriceUomConversionFactor =
66 baseUnitOfMeasure);
67
68 Real tradePriceFxConversionFactor =
70 baseCurrency, evaluationDate);
71 Real indexPriceFxConversionFactor =
72 calculateFxConversionFactor(index_->currency(), baseCurrency,
73 evaluationDate);
74
75 Real quoteValue = 0;
76
77 Date lastQuoteDate = index_->lastQuoteDate();
78 if (lastQuoteDate >= evaluationDate - 1) {
79 quoteValue = index_->price(evaluationDate);
80 } else {
81 quoteValue = index_->forwardPrice(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() << "]";
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
108 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
void addPricingError(PricingError::Level errorLevel, const std::string &error, const std::string &detail="") const
Definition: commodity.cpp:37
SecondaryCostAmounts secondaryCostAmounts_
Definition: commodity.hpp:75
const Money & amount() const
const UnitOfMeasure & unitOfMeasure() const
Currency specification
Definition: currency.hpp:36
Concrete date class.
Definition: date.hpp:125
Energy commodity class.
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)
void performCalculations() const override
bool isExpired() const override
returns whether the instrument might have value greater than zero.
CommodityUnitCost tradePrice_
EnergyFuture(Integer buySell, Quantity quantity, CommodityUnitCost tradePrice, ext::shared_ptr< CommodityIndex > index, const CommodityType &commodityType, const ext::shared_ptr< SecondaryCosts > &secondaryCosts)
ext::shared_ptr< CommodityIndex > index_
std::map< std::string, ext::any > additionalResults_
Definition: instrument.hpp:108
const Currency & currency() const
Definition: money.hpp:173
Decimal value() const
Definition: money.hpp:177
template class providing a null value for a given type.
Definition: null.hpp:76
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:228
Amount of a commodity.
Definition: quantity.hpp:34
const CommodityType & commodityType() const
Definition: quantity.hpp:128
Real amount() const
Definition: quantity.hpp:136
const UnitOfMeasure & unitOfMeasure() const
Definition: quantity.hpp:132
DateProxy & evaluationDate()
the date at which pricing is to be performed.
Definition: settings.hpp:147
static Settings & instance()
access to the unique instance
Definition: singleton.hpp:104
Unit of measure specification
commodity settings
Energy future.
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
detail::iso_date_holder iso_date(const Date &d)
output dates in ISO format (yyyy-mm-dd)
Definition: date.cpp:991
QL_REAL Real
real number
Definition: types.hpp:50
QL_INTEGER Integer
integer number
Definition: types.hpp:35
Definition: any.hpp:35
STL namespace.
global repository for run-time library settings