/
commodity.cpp
98 lines (80 loc) · 3.34 KB
/
commodity.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2008 J. Erik Radmall
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it
under the terms of the QuantLib license. You should have received a
copy of the license along with this program; if not, please email
<quantlib-dev@lists.sf.net>. The license is also available online at
<http://quantlib.org/license.shtml>.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/
#include <ql/experimental/commodities/commodity.hpp>
#include <iomanip>
#include <utility>
namespace QuantLib {
Commodity::Commodity(ext::shared_ptr<SecondaryCosts> secondaryCosts)
: secondaryCosts_(std::move(secondaryCosts)) {}
const SecondaryCostAmounts& Commodity::secondaryCostAmounts() const {
return secondaryCostAmounts_;
}
const PricingErrors& Commodity::pricingErrors() const {
return pricingErrors_;
}
void Commodity::addPricingError(PricingError::Level errorLevel,
const std::string& error,
const std::string& detail) const {
pricingErrors_.emplace_back(errorLevel, error, detail);
}
std::ostream& operator<<(std::ostream& out,
const SecondaryCostAmounts& secondaryCostAmounts) {
std::string currencyCode;
Real totalAmount = 0;
out << "secondary costs" << std::endl;
for (const auto& secondaryCostAmount : secondaryCostAmounts) {
Real amount = secondaryCostAmount.second.value();
if (currencyCode.empty())
currencyCode = secondaryCostAmount.second.currency().code();
totalAmount += amount;
out << std::setw(28) << std::left << secondaryCostAmount.first << std::setw(12)
<< std::right << std::fixed << std::setprecision(2) << amount << " " << currencyCode
<< std::endl;
}
out << std::setw(28) << std::left << "total"
<< std::setw(12) << std::right << std::fixed
<< std::setprecision(2) << totalAmount << " " << currencyCode
<< std::endl;
return out;
}
std::ostream& operator<<(std::ostream& out, const PricingError& error) {
switch (error.errorLevel) {
case PricingError::Info:
out << "info: ";
break;
case PricingError::Warning:
out << "warning: ";
break;
case PricingError::Error:
out << "*** error: ";
break;
case PricingError::Fatal:
out << "*** fatal: ";
break;
}
out << error.error;
if (!error.detail.empty())
out << ": " << error.detail;
return out;
}
std::ostream& operator<<(std::ostream& out, const PricingErrors& errors) {
if (!errors.empty()) {
out << "*** pricing errors" << std::endl;
for (const auto& error : errors)
out << error << std::endl;
}
return out;
}
}