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