QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
treeswaptionengine.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2001, 2002, 2003 Sadruddin Rejeb
5 Copyright (C) 2005, 2007 StatPro Italia srl
6
7 This file is part of QuantLib, a free-software/open-source library
8 for financial quantitative analysts and developers - http://quantlib.org/
9
10 QuantLib is free software: you can redistribute it and/or modify it
11 under the terms of the QuantLib license. You should have received a
12 copy of the license along with this program; if not, please email
13 <quantlib-dev@lists.sf.net>. The license is also available online at
14 <http://quantlib.org/license.shtml>.
15
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the license for more details.
19*/
20
21#include <ql/pricingengines/swaption/discretizedswaption.hpp>
22#include <ql/pricingengines/swaption/treeswaptionengine.hpp>
23#include <utility>
24
25namespace QuantLib {
26
27 TreeSwaptionEngine::TreeSwaptionEngine(const ext::shared_ptr<ShortRateModel>& model,
28 Size timeSteps,
29 Handle<YieldTermStructure> termStructure)
31 termStructure_(std::move(termStructure)) {
33 }
34
35 TreeSwaptionEngine::TreeSwaptionEngine(const ext::shared_ptr<ShortRateModel>& model,
36 const TimeGrid& timeGrid,
37 Handle<YieldTermStructure> termStructure)
39 termStructure_(std::move(termStructure)) {
41 }
42
44 Size timeSteps,
45 Handle<YieldTermStructure> termStructure)
47 termStructure_(std::move(termStructure)) {
49 }
50
52
53 QL_REQUIRE(arguments_.settlementMethod != Settlement::ParYieldCurve,
54 "cash settled (ParYieldCurve) swaptions not priced with "
55 "TreeSwaptionEngine");
56 QL_REQUIRE(!model_.empty(), "no model specified");
57
58 Date referenceDate;
59 DayCounter dayCounter;
60
61 ext::shared_ptr<TermStructureConsistentModel> tsmodel =
62 ext::dynamic_pointer_cast<TermStructureConsistentModel>(*model_);
63 if (tsmodel != nullptr) {
64 referenceDate = tsmodel->termStructure()->referenceDate();
65 dayCounter = tsmodel->termStructure()->dayCounter();
66 } else {
67 referenceDate = termStructure_->referenceDate();
68 dayCounter = termStructure_->dayCounter();
69 }
70
71 DiscretizedSwaption swaption(arguments_, referenceDate, dayCounter);
72 ext::shared_ptr<Lattice> lattice;
73
74 if (lattice_ != nullptr) {
75 lattice = lattice_;
76 } else {
77 std::vector<Time> times = swaption.mandatoryTimes();
78 TimeGrid timeGrid(times.begin(), times.end(), timeSteps_);
79 lattice = model_->tree(timeGrid);
80 }
81
82 std::vector<Time> stoppingTimes(arguments_.exercise->dates().size());
83 for (Size i=0; i<stoppingTimes.size(); ++i)
84 stoppingTimes[i] =
85 dayCounter.yearFraction(referenceDate,
86 arguments_.exercise->date(i));
87
88 swaption.initialize(lattice, stoppingTimes.back());
89
90 Time nextExercise =
91 *std::find_if(stoppingTimes.begin(), stoppingTimes.end(),
92 [](Time t){ return t >= 0.0; });
93 swaption.rollback(nextExercise);
94
95 results_.value = swaption.presentValue();
96 }
97
98}
Concrete date class.
Definition: date.hpp:125
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
void initialize(const ext::shared_ptr< Lattice > &, Time t)
std::vector< Time > mandatoryTimes() const override
Shared handle to an observable.
Definition: handle.hpp:41
bool empty() const
checks if the contained shared pointer points to anything
Definition: handle.hpp:166
Engine for a short-rate model specialized on a lattice.
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:228
Swaption class
Definition: swaption.hpp:81
time grid class
Definition: timegrid.hpp:43
void calculate() const override
Handle< YieldTermStructure > termStructure_
TreeSwaptionEngine(const ext::shared_ptr< ShortRateModel > &, Size timeSteps, Handle< YieldTermStructure > termStructure=Handle< YieldTermStructure >())
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
STL namespace.