/src/quantlib/ql/pricingengines/swap/treeswapengine.cpp
Line | Count | Source |
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 | | <https://www.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/swap/discretizedswap.hpp> |
22 | | #include <ql/pricingengines/swap/treeswapengine.hpp> |
23 | | #include <utility> |
24 | | |
25 | | namespace QuantLib { |
26 | | |
27 | | TreeVanillaSwapEngine::TreeVanillaSwapEngine(const ext::shared_ptr<ShortRateModel>& model, |
28 | | Size timeSteps, |
29 | | Handle<YieldTermStructure> termStructure) |
30 | 0 | : LatticeShortRateModelEngine<VanillaSwap::arguments, VanillaSwap::results>(model, timeSteps), |
31 | 0 | termStructure_(std::move(termStructure)) { |
32 | 0 | registerWith(termStructure_); |
33 | 0 | } |
34 | | |
35 | | TreeVanillaSwapEngine::TreeVanillaSwapEngine(const ext::shared_ptr<ShortRateModel>& model, |
36 | | const TimeGrid& timeGrid, |
37 | | Handle<YieldTermStructure> termStructure) |
38 | 0 | : LatticeShortRateModelEngine<VanillaSwap::arguments, VanillaSwap::results>(model, timeGrid), |
39 | 0 | termStructure_(std::move(termStructure)) { |
40 | 0 | registerWith(termStructure_); |
41 | 0 | } |
42 | | |
43 | 0 | void TreeVanillaSwapEngine::calculate() const { |
44 | |
|
45 | 0 | QL_REQUIRE(!model_.empty(), "no model specified"); |
46 | | |
47 | 0 | Date referenceDate; |
48 | 0 | DayCounter dayCounter; |
49 | |
|
50 | 0 | ext::shared_ptr<TermStructureConsistentModel> tsmodel = |
51 | 0 | ext::dynamic_pointer_cast<TermStructureConsistentModel>(*model_); |
52 | 0 | if (tsmodel != nullptr) { |
53 | 0 | referenceDate = tsmodel->termStructure()->referenceDate(); |
54 | 0 | dayCounter = tsmodel->termStructure()->dayCounter(); |
55 | 0 | } else { |
56 | 0 | referenceDate = termStructure_->referenceDate(); |
57 | 0 | dayCounter = termStructure_->dayCounter(); |
58 | 0 | } |
59 | |
|
60 | 0 | DiscretizedSwap swap(arguments_, referenceDate, dayCounter); |
61 | 0 | std::vector<Time> times = swap.mandatoryTimes(); |
62 | |
|
63 | 0 | ext::shared_ptr<Lattice> lattice; |
64 | 0 | if (lattice_ != nullptr) { |
65 | 0 | lattice = lattice_; |
66 | 0 | } else { |
67 | 0 | TimeGrid timeGrid(times.begin(), times.end(), timeSteps_); |
68 | 0 | lattice = model_->tree(timeGrid); |
69 | 0 | } |
70 | |
|
71 | 0 | Time maxTime = *std::max_element(times.begin(), times.end()); |
72 | 0 | swap.initialize(lattice, maxTime); |
73 | 0 | swap.rollback(0.0); |
74 | |
|
75 | 0 | results_.value = swap.presentValue(); |
76 | 0 | } |
77 | | |
78 | | } |