/src/quantlib/ql/quotes/forwardswapquote.cpp
Line | Count | Source |
1 | | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* |
4 | | Copyright (C) 2007 Ferdinando Ametrano |
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 | | <https://www.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/quotes/forwardswapquote.hpp> |
21 | | #include <ql/settings.hpp> |
22 | | #include <utility> |
23 | | |
24 | | namespace QuantLib { |
25 | | |
26 | | ForwardSwapQuote::ForwardSwapQuote(ext::shared_ptr<SwapIndex> swapIndex, |
27 | | Handle<Quote> spread, |
28 | | const Period& fwdStart) |
29 | 0 | : swapIndex_(std::move(swapIndex)), spread_(std::move(spread)), fwdStart_(fwdStart) { |
30 | 0 | registerWith(swapIndex_); |
31 | 0 | registerWith(spread_); |
32 | 0 | registerWith(Settings::instance().evaluationDate()); |
33 | 0 | evaluationDate_ = Settings::instance().evaluationDate(); |
34 | 0 | initializeDates(); |
35 | 0 | } Unexecuted instantiation: QuantLib::ForwardSwapQuote::ForwardSwapQuote(boost::shared_ptr<QuantLib::SwapIndex>, QuantLib::Handle<QuantLib::Quote>, QuantLib::Period const&) Unexecuted instantiation: QuantLib::ForwardSwapQuote::ForwardSwapQuote(boost::shared_ptr<QuantLib::SwapIndex>, QuantLib::Handle<QuantLib::Quote>, QuantLib::Period const&) |
36 | | |
37 | 0 | void ForwardSwapQuote::initializeDates() { |
38 | 0 | valueDate_ = swapIndex_->fixingCalendar().advance( |
39 | 0 | evaluationDate_, |
40 | 0 | swapIndex_->fixingDays()*Days, |
41 | 0 | Following); |
42 | 0 | startDate_ = swapIndex_->fixingCalendar().advance(valueDate_, |
43 | 0 | fwdStart_, |
44 | 0 | Following); |
45 | 0 | fixingDate_ = swapIndex_->fixingDate(startDate_); |
46 | 0 | swap_ = swapIndex_->underlyingSwap(fixingDate_); |
47 | 0 | } |
48 | | |
49 | 0 | void ForwardSwapQuote::update() { |
50 | 0 | if (evaluationDate_ != Settings::instance().evaluationDate()) { |
51 | 0 | evaluationDate_ = Settings::instance().evaluationDate(); |
52 | 0 | initializeDates(); |
53 | 0 | } |
54 | 0 | LazyObject::update(); |
55 | 0 | } |
56 | | |
57 | 0 | const Date& ForwardSwapQuote::valueDate() const { |
58 | 0 | calculate(); |
59 | 0 | return valueDate_; |
60 | 0 | } |
61 | | |
62 | 0 | const Date& ForwardSwapQuote::startDate() const { |
63 | 0 | calculate(); |
64 | 0 | return startDate_; |
65 | 0 | } |
66 | | |
67 | 0 | const Date& ForwardSwapQuote::fixingDate() const { |
68 | 0 | calculate(); |
69 | 0 | return fixingDate_; |
70 | 0 | } |
71 | | |
72 | 0 | Real ForwardSwapQuote::value() const { |
73 | 0 | calculate(); |
74 | 0 | return result_; |
75 | 0 | } |
76 | | |
77 | 0 | bool ForwardSwapQuote::isValid() const { |
78 | 0 | bool swapIndexIsValid = true; |
79 | 0 | try { |
80 | 0 | swap_->recalculate(); |
81 | 0 | } catch (...) { |
82 | 0 | swapIndexIsValid = false; |
83 | 0 | } |
84 | 0 | bool spreadIsValid = spread_.empty() ? true : spread_->isValid(); |
85 | 0 | return swapIndexIsValid && spreadIsValid; |
86 | 0 | } |
87 | | |
88 | 0 | void ForwardSwapQuote::performCalculations() const { |
89 | | // we didn't register as observers - force calculation |
90 | 0 | swap_->recalculate(); |
91 | | // weak implementation... to be improved |
92 | 0 | static const Spread basisPoint = 1.0e-4; |
93 | 0 | Real floatingLegNPV = swap_->floatingLegNPV(); |
94 | 0 | Spread spread = spread_.empty() ? 0.0 : spread_->value(); |
95 | 0 | Real spreadNPV = swap_->floatingLegBPS()/basisPoint*spread; |
96 | 0 | Real totNPV = - (floatingLegNPV+spreadNPV); |
97 | 0 | result_ = totNPV/(swap_->fixedLegBPS()/basisPoint); |
98 | 0 | } |
99 | | } |