/src/quantlib/ql/experimental/commodities/commoditycashflow.cpp
Line | Count | Source (jump to first uncovered line) |
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/commoditycashflow.hpp> |
21 | | #include <ql/patterns/visitor.hpp> |
22 | | #include <iomanip> |
23 | | |
24 | | namespace QuantLib { |
25 | | |
26 | 0 | void CommodityCashFlow::accept(AcyclicVisitor& v) { |
27 | 0 | auto* v1 = dynamic_cast<Visitor<CommodityCashFlow>*>(&v); |
28 | 0 | if (v1 != nullptr) |
29 | 0 | v1->visit(*this); |
30 | 0 | else |
31 | 0 | CashFlow::accept(v); |
32 | 0 | } |
33 | | |
34 | | std::ostream& operator<<(std::ostream& out, |
35 | 0 | const CommodityCashFlows& cashFlows) { |
36 | 0 | if (cashFlows.empty()) |
37 | 0 | return out << "no cashflows" << std::endl; |
38 | 0 | out << "cashflows" << std::endl; |
39 | 0 | std::string currencyCode; //= cashFlows[0]->discountedAmount().currency().code(); |
40 | 0 | Real totalDiscounted = 0; |
41 | 0 | Real totalUndiscounted = 0; |
42 | 0 | for (const auto& i : cashFlows) { |
43 | | //const ext::shared_ptr<CommodityCashFlow> cashFlow = *i; |
44 | 0 | const ext::shared_ptr<CommodityCashFlow> cashFlow = i.second; |
45 | 0 | totalDiscounted += cashFlow->discountedAmount().value(); |
46 | 0 | totalUndiscounted += cashFlow->undiscountedAmount().value(); |
47 | | //out << io::iso_date(cashFlow->date()) << " " << |
48 | 0 | out << io::iso_date(i.first) << " " << std::setw(16) << std::right << std::fixed |
49 | 0 | << std::setprecision(2) << cashFlow->discountedAmount().value() << " " |
50 | 0 | << currencyCode << std::setw(16) << std::right << std::fixed << std::setprecision(2) |
51 | 0 | << cashFlow->undiscountedAmount().value() << " " << currencyCode << std::endl; |
52 | 0 | } |
53 | 0 | out << "total " |
54 | 0 | << std::setw(16) << std::right << std::fixed |
55 | 0 | << std::setprecision(2) << totalDiscounted << " " << currencyCode |
56 | 0 | << std::setw(16) << std::right << std::fixed |
57 | 0 | << std::setprecision(2) << totalUndiscounted << " " |
58 | 0 | << currencyCode << std::endl; |
59 | 0 | return out; |
60 | 0 | } |
61 | | |
62 | | } |
63 | | |