| 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/commodityindex.hpp> |
| 21 | #include <ql/experimental/commodities/commoditypricinghelpers.hpp> |
| 22 | #include <utility> |
| 23 | |
| 24 | namespace QuantLib { |
| 25 | |
| 26 | CommodityIndex::CommodityIndex(const std::string& indexName, |
| 27 | CommodityType commodityType, |
| 28 | Currency currency, |
| 29 | UnitOfMeasure unitOfMeasure, |
| 30 | Calendar calendar, |
| 31 | Real lotQuantity, |
| 32 | ext::shared_ptr<CommodityCurve> forwardCurve, |
| 33 | ext::shared_ptr<ExchangeContracts> exchangeContracts, |
| 34 | int nearbyOffset) |
| 35 | : name_(indexName), commodityType_(std::move(commodityType)), |
| 36 | unitOfMeasure_(std::move(unitOfMeasure)), currency_(std::move(currency)), |
| 37 | calendar_(std::move(calendar)), lotQuantity_(lotQuantity), |
| 38 | forwardCurve_(std::move(forwardCurve)), exchangeContracts_(std::move(exchangeContracts)), |
| 39 | nearbyOffset_(nearbyOffset) { |
| 40 | quotes_ = IndexManager::instance().getHistory(name: indexName); |
| 41 | IndexManager::instance().setHistory(name: indexName, history: quotes_); |
| 42 | registerWith(h: Settings::instance().evaluationDate()); |
| 43 | registerWith(h: IndexManager::instance().notifier(name: name())); |
| 44 | |
| 45 | if (forwardCurve_ != nullptr) |
| 46 | // registerWith(forwardCurve_); |
| 47 | forwardCurveUomConversionFactor_ = |
| 48 | CommodityPricingHelper::calculateUomConversionFactor( |
| 49 | commodityType: commodityType_, |
| 50 | fromUnitOfMeasure: forwardCurve_->unitOfMeasure_, |
| 51 | toUnitOfMeasure: unitOfMeasure_); |
| 52 | } |
| 53 | |
| 54 | std::ostream& operator<<(std::ostream& out, const CommodityIndex& index) { |
| 55 | out << "[" << index.name_ << "] (" |
| 56 | << index.currency_.code() << "/" |
| 57 | << index.unitOfMeasure_.code() << ")" ; |
| 58 | if (index.forwardCurve_ != nullptr) |
| 59 | out << "; forward (" << (*index.forwardCurve_) << ")" ; |
| 60 | return out; |
| 61 | } |
| 62 | |
| 63 | } |
| 64 | |