23#ifndef quantext_price_curve_hpp
24#define quantext_price_curve_hpp
26#include <boost/algorithm/cxx11/is_sorted.hpp>
30#include <ql/currency.hpp>
31#include <ql/math/comparison.hpp>
32#include <ql/patterns/lazyobject.hpp>
33#include <ql/quote.hpp>
34#include <ql/termstructures/interpolatedcurve.hpp>
35#include <ql/time/calendars/nullcalendar.hpp>
47template <
class Interpolator>
49 public QuantLib::LazyObject,
50 protected QuantLib::InterpolatedCurve<Interpolator> {
56 const QuantLib::DayCounter& dc,
const QuantLib::Currency&
currency,
57 const Interpolator& interpolator = Interpolator());
61 const std::vector<QuantLib::Handle<QuantLib::Quote> >& quotes,
62 const QuantLib::DayCounter& dc,
const QuantLib::Currency&
currency,
63 const Interpolator& interpolator = Interpolator());
67 const std::vector<QuantLib::Real>&
prices,
const QuantLib::DayCounter& dc,
68 const QuantLib::Currency&
currency,
const Interpolator& interpolator = Interpolator());
72 const std::vector<QuantLib::Handle<QuantLib::Quote> >& quotes,
73 const QuantLib::DayCounter& dc,
const QuantLib::Currency&
currency,
74 const Interpolator& interpolator = Interpolator());
84 QuantLib::Date
maxDate()
const override;
85 QuantLib::Time
maxTime()
const override;
90 QuantLib::Time
minTime()
const override;
91 std::vector<QuantLib::Date>
pillarDates()
const override;
97 const std::vector<QuantLib::Time>&
times()
const {
return this->times_; }
98 const std::vector<QuantLib::Real>&
prices()
const {
return this->data_; }
104 const QuantLib::DayCounter& dc,
const QuantLib::Currency&
currency,
105 const Interpolator& interpolator = Interpolator());
114 QuantLib::Real
priceImpl(QuantLib::Time t)
const override;
117 mutable std::vector<QuantLib::Date>
dates_;
121 std::vector<QuantLib::Handle<QuantLib::Quote> >
quotes_;
130template <
class Interpolator>
132 const std::vector<QuantLib::Real>& prices,
133 const QuantLib::DayCounter& dc,
134 const QuantLib::Currency& currency,
135 const Interpolator& interpolator)
137 std::vector<
QuantLib::Time>(tenors.size()), prices,
139 dates_(tenors.size()), currency_(currency), tenors_(tenors) {
141 QL_REQUIRE(boost::algorithm::is_sorted(
tenors_.begin(),
tenors_.end()),
"Tenors must be sorted");
146template <
class Interpolator>
148 const std::vector<QuantLib::Period>& tenors,
const std::vector<QuantLib::Handle<QuantLib::Quote> >& quotes,
149 const QuantLib::DayCounter& dc,
const QuantLib::Currency& currency,
const Interpolator& interpolator)
151 std::vector<
QuantLib::Time>(tenors.size()),
152 std::vector<
QuantLib::Real>(quotes.size()),
154 dates_(tenors.size()), currency_(currency), quotes_(quotes), tenors_(tenors) {
156 QL_REQUIRE(boost::algorithm::is_sorted(
tenors_.begin(),
tenors_.end()),
"Tenors must be sorted");
161 for (QuantLib::Size i = 0; i <
quotes_.size(); ++i) {
162 registerWith(quotes[i]);
166template <
class Interpolator>
168 const std::vector<QuantLib::Date>& dates,
169 const std::vector<QuantLib::Real>& prices,
170 const QuantLib::DayCounter& dc,
171 const QuantLib::Currency& currency,
172 const Interpolator& interpolator)
174 std::vector<
QuantLib::Time>(dates.size()),
175 prices, interpolator),
176 dates_(dates), currency_(currency) {
182template <
class Interpolator>
184 const QuantLib::Date& referenceDate,
const std::vector<QuantLib::Date>& dates,
185 const std::vector<QuantLib::Handle<QuantLib::Quote> >& quotes,
const QuantLib::DayCounter& dc,
186 const QuantLib::Currency& currency,
const Interpolator& interpolator)
188 std::vector<
QuantLib::Time>(dates.size()),
189 std::vector<
QuantLib::Real>(quotes.size()),
191 dates_(dates), currency_(currency), quotes_(quotes) {
197 for (QuantLib::Size i = 0; i <
quotes_.size(); ++i) {
198 registerWith(quotes[i]);
202template <
class Interpolator>
204 const QuantLib::Date& referenceDate,
const QuantLib::DayCounter& dc,
205 const QuantLib::Currency& currency,
const Interpolator& interpolator)
207 QuantLib::InterpolatedCurve<Interpolator>(interpolator), currency_(currency) {}
211 QuantLib::LazyObject::update();
221 if (!tenors_.empty()) {
222 populateDatesFromTenors();
223 this->interpolation_.update();
227 if (!quotes_.empty()) {
228 getPricesFromQuotes();
229 this->interpolation_.update();
235 return dates_.back();
240 return this->times_.back();
245 return this->times_.front();
256 return this->interpolation_(t,
true);
261 QL_REQUIRE(this->data_.size() >= Interpolator::requiredPoints,
"not enough times for the interpolation method");
264 if (!quotes_.empty()) {
265 getPricesFromQuotes();
268 QL_REQUIRE(this->data_.size() == this->times_.size(),
"Number of times must equal number of prices");
270 QuantLib::InterpolatedCurve<Interpolator>::setupInterpolation();
271 this->interpolation_.update();
275 QuantLib::Date asof = QuantLib::Settings::instance().evaluationDate();
276 for (QuantLib::Size i = 0; i < dates_.size(); ++i) {
277 dates_[i] = asof + tenors_[i];
278 this->times_[i] = timeFromReference(dates_[i]);
284 QL_REQUIRE(!dates_.empty(),
"Dates cannot be empty for InterpolatedPriceCurve");
285 this->times_[0] = timeFromReference(dates_[0]);
286 for (QuantLib::Size i = 1; i < dates_.size(); ++i) {
287 QL_REQUIRE(dates_[i] > dates_[i - 1],
"invalid date (" << dates_[i] <<
", vs " << dates_[i - 1] <<
")");
288 this->times_[i] = timeFromReference(dates_[i]);
289 QL_REQUIRE(!QuantLib::close(this->times_[i], this->times_[i - 1]),
"two dates correspond to the same time "
290 "under this curve's day count convention");
296 for (QuantLib::Size i = 0; i < quotes_.size(); ++i) {
297 QL_REQUIRE(!this->quotes_[i].empty(),
"price quote at index " << i <<
" is empty");
298 this->data_[i] = quotes_[i]->value();
Interpolated price curve.
const QuantLib::Currency currency_
QuantLib::Time minTime() const override
The minimum time for which the curve can return values.
void performCalculations() const override
InterpolatedPriceCurve(const std::vector< QuantLib::Period > &tenors, const std::vector< QuantLib::Real > &prices, const QuantLib::DayCounter &dc, const QuantLib::Currency ¤cy, const Interpolator &interpolator=Interpolator())
Curve constructed from periods and prices. No conventions are applied in getting to a date from a per...
void convertDatesToTimes()
const QuantLib::Currency & currency() const override
The currency in which prices are expressed.
std::vector< QuantLib::Date > dates_
const std::vector< QuantLib::Time > & times() const
std::vector< QuantLib::Period > tenors_
std::vector< QuantLib::Handle< QuantLib::Quote > > quotes_
QuantLib::Date maxDate() const override
void getPricesFromQuotes() const
const std::vector< QuantLib::Real > & prices() const
std::vector< QuantLib::Date > pillarDates() const override
The pillar dates for the PriceTermStructure.
void populateDatesFromTenors() const
QuantLib::Real priceImpl(QuantLib::Time t) const override
Price calculation.
QuantLib::Time maxTime() const override
Term structure of prices.