QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
treeswapengine.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) 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
23#include <utility>
24
25namespace QuantLib {
26
27 TreeVanillaSwapEngine::TreeVanillaSwapEngine(const ext::shared_ptr<ShortRateModel>& model,
28 Size timeSteps,
29 Handle<YieldTermStructure> termStructure)
31 termStructure_(std::move(termStructure)) {
33 }
34
35 TreeVanillaSwapEngine::TreeVanillaSwapEngine(const ext::shared_ptr<ShortRateModel>& model,
36 const TimeGrid& timeGrid,
37 Handle<YieldTermStructure> termStructure)
39 termStructure_(std::move(termStructure)) {
41 }
42
44
45 QL_REQUIRE(!model_.empty(), "no model specified");
46
47 Date referenceDate;
48 DayCounter dayCounter;
49
50 ext::shared_ptr<TermStructureConsistentModel> tsmodel =
51 ext::dynamic_pointer_cast<TermStructureConsistentModel>(*model_);
52 if (tsmodel != nullptr) {
53 referenceDate = tsmodel->termStructure()->referenceDate();
54 dayCounter = tsmodel->termStructure()->dayCounter();
55 } else {
56 referenceDate = termStructure_->referenceDate();
57 dayCounter = termStructure_->dayCounter();
58 }
59
60 DiscretizedSwap swap(arguments_, referenceDate, dayCounter);
61 std::vector<Time> times = swap.mandatoryTimes();
62
63 ext::shared_ptr<Lattice> lattice;
64 if (lattice_ != nullptr) {
65 lattice = lattice_;
66 } else {
67 TimeGrid timeGrid(times.begin(), times.end(), timeSteps_);
68 lattice = model_->tree(timeGrid);
69 }
70
71 Time maxTime = *std::max_element(times.begin(), times.end());
72 swap.initialize(lattice, maxTime);
73 swap.rollback(0.0);
74
75 results_.value = swap.presentValue();
76 }
77
78}
Concrete date class.
Definition: date.hpp:125
day counter class
Definition: daycounter.hpp:44
Shared handle to an observable.
Definition: handle.hpp:41
bool empty() const
checks if the contained shared pointer points to anything
Definition: handle.hpp:191
Engine for a short-rate model specialized on a lattice.
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:228
time grid class
Definition: timegrid.hpp:43
TreeVanillaSwapEngine(const ext::shared_ptr< ShortRateModel > &, Size timeSteps, Handle< YieldTermStructure > termStructure=Handle< YieldTermStructure >())
void calculate() const override
Handle< YieldTermStructure > termStructure_
Plain-vanilla swap: fix vs ibor leg.
Definition: vanillaswap.hpp:65
Discretized swap class.
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
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
void swap(Array &v, Array &w) noexcept
Definition: array.hpp:903
STL namespace.
Numerical lattice engine for swaps.