QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
energycommodity.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
24#include <iomanip>
25#include <utility>
26
27namespace QuantLib {
28
30 Real payLegPrice,
31 Real receiveLegPrice,
32 bool unrealized)
33 : date(date), quantityAmount(0), payLegPrice(payLegPrice),
34 receiveLegPrice(receiveLegPrice), unrealized(unrealized) {}
35
36 std::ostream& operator<<(std::ostream& out,
37 const EnergyDailyPositions& dailyPositions) {
38 out << std::setw(12) << std::left << "positions"
39 << std::setw(12) << std::right << "pay"
40 << std::setw(12) << std::right << "receive"
41 << std::setw(10) << std::right << "qty"
42 << std::setw(14) << std::right << "delta"
43 << std::setw(10) << std::right << "open" << std::endl;
44
45 for (const auto& i : dailyPositions) {
46 const EnergyDailyPosition& dailyPosition = i.second;
47 out << std::setw(4) << io::iso_date(i.first) << " " << std::setw(12) << std::right
48 << std::fixed << std::setprecision(6) << dailyPosition.payLegPrice << std::setw(12)
49 << std::right << std::fixed << std::setprecision(6) << dailyPosition.receiveLegPrice
50 << std::setw(10) << std::right << std::fixed << std::setprecision(2)
51 << dailyPosition.quantityAmount << std::setw(14) << std::right << std::fixed
52 << std::setprecision(2) << dailyPosition.riskDelta << std::setw(10) << std::right
53 << std::fixed << std::setprecision(2)
54 << (dailyPosition.unrealized ? dailyPosition.quantityAmount : 0) << std::endl;
55 }
56
57 return out;
58 }
59
60
61
62
63
65 auto* arguments = dynamic_cast<EnergyCommodity::arguments*>(args);
66 QL_REQUIRE(arguments != nullptr, "wrong argument type");
67 //arguments->legs = legs_;
68 //arguments->payer = payer_;
69 }
70
73 const auto* results = dynamic_cast<const EnergyCommodity::results*>(r);
74 QL_REQUIRE(results != nullptr, "wrong result type");
75 }
76
78 const ext::shared_ptr<SecondaryCosts>& secondaryCosts)
79 : Commodity(secondaryCosts), commodityType_(std::move(commodityType)) {}
80
82 return commodityType_;
83 }
84
85
87 const CommodityType& commodityType,
88 const UnitOfMeasure& fromUnitOfMeasure,
89 const UnitOfMeasure& toUnitOfMeasure) {
90 if (toUnitOfMeasure != fromUnitOfMeasure) {
93 commodityType, fromUnitOfMeasure, toUnitOfMeasure);
94 return uomConv.conversionFactor();
95 }
96
97 return 1;
98 }
99
101 const Currency& fromCurrency,
102 const Currency& toCurrency,
103 const Date& evaluationDate) {
104 if (fromCurrency != toCurrency) {
106 fromCurrency, toCurrency,
107 evaluationDate /*, ExchangeRate::Direct*/);
108 if (fromCurrency == exchRate.target())
109 return 1.0 / exchRate.rate();
110 return exchRate.rate();
111 }
112 return 1;
113 }
114
116 const CommodityUnitCost& unitCost,
117 const Date& evaluationDate) const {
118 if (unitCost.amount().value() != 0) {
119 const Currency& baseCurrency =
121 const UnitOfMeasure baseUnitOfMeasure =
123 Real unitCostUomConversionFactor =
125 unitCost.unitOfMeasure(),
126 baseUnitOfMeasure);
127 Real unitCostFxConversionFactor =
129 baseCurrency, evaluationDate);
130 return unitCost.amount().value() * unitCostUomConversionFactor
131 * unitCostFxConversionFactor;
132 }
133 return 0;
134 }
135
137 const CommodityType& commodityType,
138 Real totalQuantityValue,
139 const Date& evaluationDate) const {
140 secondaryCostAmounts_.clear();
141 if (secondaryCosts_ != nullptr) {
142 const Currency& baseCurrency =
144 try {
145 for (SecondaryCosts::const_iterator i = secondaryCosts_->begin();
146 i != secondaryCosts_->end(); ++i) {
147 if (ext::any_cast<CommodityUnitCost>(&i->second) != nullptr) {
148 Real value =
151 ext::any_cast<CommodityUnitCost>(i->second),
152 evaluationDate) * totalQuantityValue;
153 secondaryCostAmounts_[i->first] =
154 Money(baseCurrency, value);
155 } else if (ext::any_cast<Money>(&i->second) != nullptr) {
156 const Money& amount = ext::any_cast<Money>(i->second);
157 Real fxConversionFactor =
159 baseCurrency,
160 evaluationDate);
161 secondaryCostAmounts_[i->first] =
162 Money(baseCurrency,
163 amount.value() * fxConversionFactor);
164 }
165 }
166 } catch (const std::exception& e) {
167 QL_FAIL("error calculating secondary costs: " << e.what());
168 }
169 }
170 }
171
172}
173
Commodity base class.
Definition: commodity.hpp:63
ext::shared_ptr< SecondaryCosts > secondaryCosts_
Definition: commodity.hpp:73
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
static Real calculateUomConversionFactor(const CommodityType &commodityType, const UnitOfMeasure &fromUnitOfMeasure, const UnitOfMeasure &toUnitOfMeasure)
void setupArguments(PricingEngine::arguments *) const override
Real calculateUnitCost(const CommodityType &commodityType, const CommodityUnitCost &unitCost, const Date &evaluationDate) const
EnergyCommodity(CommodityType commodityType, const ext::shared_ptr< SecondaryCosts > &secondaryCosts)
void calculateSecondaryCostAmounts(const CommodityType &commodityType, Real totalQuantityValue, const Date &evaluationDate) const
void fetchResults(const PricingEngine::results *) const override
static Real calculateFxConversionFactor(const Currency &fromCurrency, const Currency &toCurrency, const Date &evaluationDate)
const CommodityType & commodityType() const
exchange rate between two currencies
const Currency & target() const
the target currency.
Decimal rate() const
the exchange rate (when available)
ExchangeRate lookup(const Currency &source, const Currency &target, Date date=Date(), ExchangeRate::Type type=ExchangeRate::Derived) const
virtual void fetchResults(const PricingEngine::results *) const
Definition: instrument.hpp:155
amount of cash
Definition: money.hpp:38
const Currency & currency() const
Definition: money.hpp:173
Decimal value() const
Definition: money.hpp:177
static UnitOfMeasureConversionManager & instance()
access to the unique instance
Definition: singleton.hpp:104
Real conversionFactor() const
the conversion factor
UnitOfMeasureConversion lookup(const CommodityType &commodityType, const UnitOfMeasure &, const UnitOfMeasure &, UnitOfMeasureConversion::Type type=UnitOfMeasureConversion::Derived) const
Unit of measure specification
commodity settings
Energy commodity.
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
#define QL_FAIL(message)
throw an error (possibly with file and line information)
Definition: errors.hpp:92
exchange-rate repository
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
Definition: any.hpp:35
std::ostream & operator<<(std::ostream &out, GFunctionFactory::YieldCurveModel type)
std::map< Date, EnergyDailyPosition > EnergyDailyPositions
STL namespace.
ext::shared_ptr< YieldTermStructure > r
Unit-of-measure conversion manager.