QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
commoditycurve.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
22#include <utility>
23
24namespace QuantLib {
25
27 CommodityType commodityType,
28 Currency currency,
29 UnitOfMeasure unitOfMeasure,
30 const Calendar& calendar,
31 const std::vector<Date>& dates,
32 std::vector<Real> prices,
33 const DayCounter& dayCounter)
34 : TermStructure(dates[0], calendar, dayCounter), name_(std::move(name)),
35 commodityType_(std::move(commodityType)), unitOfMeasure_(std::move(unitOfMeasure)),
36 currency_(std::move(currency)), dates_(dates), data_(std::move(prices)),
37 interpolator_(ForwardFlat()), basisOfCurveUomConversionFactor_(1) {
38
39 QL_REQUIRE(dates_.size()>1, "too few dates");
40 QL_REQUIRE(data_.size()==dates_.size(), "dates/prices count mismatch");
41
42 times_.resize(dates_.size());
43 times_[0]=0.0;
44 for (Size i = 1; i < dates_.size(); i++) {
45 QL_REQUIRE(dates_[i] > dates_[i-1],
46 "invalid date (" << dates_[i] << ", vs "
47 << dates_[i-1] << ")");
49 }
50
53 data_.begin());
55 }
56
58 CommodityType commodityType,
59 Currency currency,
60 UnitOfMeasure unitOfMeasure,
61 const Calendar& calendar,
62 const DayCounter& dayCounter)
63 : TermStructure(0, calendar, dayCounter), name_(std::move(name)),
64 commodityType_(std::move(commodityType)), unitOfMeasure_(std::move(unitOfMeasure)),
65 currency_(std::move(currency)), interpolator_(ForwardFlat()),
66 basisOfCurveUomConversionFactor_(1) {}
67
68 void CommodityCurve::setPrices(std::map<Date, Real>& prices) {
69 QL_REQUIRE(prices.size()>1, "too few prices");
70
71 dates_.clear();
72 data_.clear();
73 for (std::map<Date, Real>::const_iterator i = prices.begin(); i != prices.end(); ++i) {
74 dates_.push_back(i->first);
75 data_.push_back(i->second);
76 }
77
78 times_.resize(dates_.size());
79 times_[0]=0.0;
80 for (Size i = 1; i < dates_.size(); i++)
82
85 data_.begin());
87 }
88
90 const ext::shared_ptr<CommodityCurve>& basisOfCurve) {
95 basisOfCurve_->unitOfMeasure_,
97 }
98
99 std::ostream& operator<<(std::ostream& out, const CommodityCurve& curve) {
100 out << "[" << curve.name_ << "] (" << curve.currency_.code()
101 << "/" << curve.unitOfMeasure_.code() << ")";
102 if (curve.basisOfCurve_ != nullptr)
103 out << "; basis to (" << (*curve.basisOfCurve_) << ")";
104 return out;
105 }
106
107}
calendar class
Definition: calendar.hpp:61
Commodity term structure.
const ext::shared_ptr< CommodityCurve > & basisOfCurve() const
void setBasisOfCurve(const ext::shared_ptr< CommodityCurve > &basisOfCurve)
const std::vector< Real > & prices() const
std::vector< Date > dates_
CommodityCurve(std::string name, CommodityType commodityType, Currency currency, UnitOfMeasure unitOfMeasure, const Calendar &calendar, const std::vector< Date > &dates, std::vector< Real > prices, const DayCounter &dayCounter=Actual365Fixed())
std::vector< Time > times_
void setPrices(std::map< Date, Real > &prices)
std::vector< Real > data_
ext::shared_ptr< CommodityCurve > basisOfCurve_
static Real calculateUomConversionFactor(const CommodityType &commodityType, const UnitOfMeasure &fromUnitOfMeasure, const UnitOfMeasure &toUnitOfMeasure)
Currency specification
Definition: currency.hpp:36
const std::string & code() const
ISO 4217 three-letter code, e.g, "USD".
Definition: currency.hpp:178
day counter class
Definition: daycounter.hpp:44
Time yearFraction(const Date &, const Date &, const Date &refPeriodStart=Date(), const Date &refPeriodEnd=Date()) const
Returns the period between two dates as a fraction of year.
Definition: daycounter.hpp:128
Forward-flat interpolation factory and traits.
Interpolation interpolate(const I1 &xBegin, const I1 &xEnd, const I2 &yBegin) const
Basic term-structure functionality.
virtual DayCounter dayCounter() const
the day counter used for date/time conversion
Unit of measure specification
const std::string & code() const
code, e.g, "BBL", "MT"
Commodity curve.
Commodity pricing helpers.
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
std::ostream & operator<<(std::ostream &out, GFunctionFactory::YieldCurveModel type)
STL namespace.