Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
pricetermstructureadapter.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2018 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
20
21using namespace std;
22using namespace QuantLib;
23
24namespace QuantExt {
25
26PriceTermStructureAdapter::PriceTermStructureAdapter(const QuantLib::ext::shared_ptr<PriceTermStructure>& priceCurve,
27 const QuantLib::ext::shared_ptr<YieldTermStructure>& discount,
28 Natural spotDays, const Calendar& spotCalendar)
29 : priceCurve_(priceCurve), discount_(discount), spotDays_(spotDays), spotCalendar_(spotCalendar) {
30
31 QL_REQUIRE(
32 priceCurve_->referenceDate() == discount_->referenceDate(),
33 "PriceTermStructureAdapter: The reference date of the discount curve and price curve should be the same");
34
35 registerWith(priceCurve_);
36 registerWith(discount_);
37}
38
39PriceTermStructureAdapter::PriceTermStructureAdapter(const QuantLib::ext::shared_ptr<PriceTermStructure>& priceCurve,
40 const QuantLib::ext::shared_ptr<YieldTermStructure>& discount,
41 const Handle<Quote>& spotQuote)
42 : priceCurve_(priceCurve), discount_(discount), spotDays_(0), spotQuote_(spotQuote) {
43
44 QL_REQUIRE(
45 priceCurve_->referenceDate() == discount_->referenceDate(),
46 "PriceTermStructureAdapter: The reference date of the discount curve and price curve should be the same");
47
48 registerWith(priceCurve_);
49 registerWith(discount_);
50 registerWith(spotQuote_);
51}
52
53Date PriceTermStructureAdapter::maxDate() const {
54 // Take the min of the two underlying curves' max date
55 // Extrapolation will be determined by each underlying curve individually
56 return min(priceCurve_->maxDate(), discount_->maxDate());
57}
58
59const Date& PriceTermStructureAdapter::referenceDate() const {
60 QL_REQUIRE(
61 priceCurve_->referenceDate() == discount_->referenceDate(),
62 "PriceTermStructureAdapter: The reference date of the discount curve and price curve should be the same");
63 return priceCurve_->referenceDate();
64}
65
66DayCounter PriceTermStructureAdapter::dayCounter() const { return priceCurve_->dayCounter(); }
67
68const QuantLib::ext::shared_ptr<PriceTermStructure>& PriceTermStructureAdapter::priceCurve() const { return priceCurve_; }
69
70const QuantLib::ext::shared_ptr<YieldTermStructure>& PriceTermStructureAdapter::discount() const { return discount_; }
71
72Natural PriceTermStructureAdapter::spotDays() const { return spotDays_; }
73
74const Calendar& PriceTermStructureAdapter::spotCalendar() const { return spotCalendar_; }
75
76DiscountFactor PriceTermStructureAdapter::discountImpl(Time t) const {
77 if (t == 0.0)
78 return 1.0;
79 // Returns discount factor exp(-s(t) * t) where s(t) is defined such that
80 // FP(0, t) = S(0) exp([z(t) - s(t)] t)
81 Real spotPrice;
82 if (spotQuote_.empty()) {
83 Time spotTime = timeFromReference(spotCalendar_.advance(referenceDate(), spotDays_ * Days));
84 spotPrice = priceCurve_->price(spotTime, true);
85 } else {
86 spotPrice = spotQuote_->value();
87 }
88 Real forwardPrice = priceCurve_->price(t, true);
89 DiscountFactor discount = discount_->discount(t, true);
90 return discount * forwardPrice / spotPrice;
91}
92
93} // namespace QuantExt
PriceTermStructureAdapter(const QuantLib::ext::shared_ptr< PriceTermStructure > &priceCurve, const QuantLib::ext::shared_ptr< QuantLib::YieldTermStructure > &discount, QuantLib::Natural spotDays=0, const QuantLib::Calendar &spotCalendar=QuantLib::NullCalendar())
CompiledFormula min(CompiledFormula x, const CompiledFormula &y)
PriceTermStructure adapter.