/src/quantlib/ql/pricingengines/vanilla/discretizedvanillaoption.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* |
4 | | Copyright (C) 2002, 2003 Sadruddin Rejeb |
5 | | Copyright (C) 2004 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/vanilla/discretizedvanillaoption.hpp> |
22 | | #include <vector> |
23 | | |
24 | | namespace QuantLib { |
25 | | |
26 | | DiscretizedVanillaOption::DiscretizedVanillaOption( |
27 | | const VanillaOption::arguments& args, |
28 | | const StochasticProcess& process, |
29 | | const TimeGrid& grid) |
30 | 0 | : arguments_(args) { |
31 | 0 | stoppingTimes_.resize(args.exercise->dates().size()); |
32 | 0 | for (Size i=0; i<stoppingTimes_.size(); ++i) { |
33 | 0 | stoppingTimes_[i] = |
34 | 0 | process.time(args.exercise->date(i)); |
35 | 0 | if (!grid.empty()) { |
36 | | // adjust to the given grid |
37 | 0 | stoppingTimes_[i] = grid.closestTime(stoppingTimes_[i]); |
38 | 0 | } |
39 | 0 | } |
40 | 0 | } |
41 | | |
42 | 0 | void DiscretizedVanillaOption::reset(Size size) { |
43 | 0 | values_ = Array(size, 0.0); |
44 | 0 | adjustValues(); |
45 | 0 | } |
46 | | |
47 | 0 | void DiscretizedVanillaOption::postAdjustValuesImpl() { |
48 | |
|
49 | 0 | Time now = time(); |
50 | 0 | switch (arguments_.exercise->type()) { |
51 | 0 | case Exercise::American: |
52 | 0 | if (now <= stoppingTimes_[1] && |
53 | 0 | now >= stoppingTimes_[0]) |
54 | 0 | applySpecificCondition(); |
55 | 0 | break; |
56 | 0 | case Exercise::European: |
57 | 0 | if (isOnTime(stoppingTimes_[0])) |
58 | 0 | applySpecificCondition(); |
59 | 0 | break; |
60 | 0 | case Exercise::Bermudan: |
61 | 0 | for (Real stoppingTime : stoppingTimes_) { |
62 | 0 | if (isOnTime(stoppingTime)) |
63 | 0 | applySpecificCondition(); |
64 | 0 | } |
65 | 0 | break; |
66 | 0 | default: |
67 | 0 | QL_FAIL("invalid option type"); |
68 | 0 | } |
69 | 0 | } |
70 | | |
71 | 0 | void DiscretizedVanillaOption::applySpecificCondition() { |
72 | 0 | Array grid = method()->grid(time()); |
73 | 0 | for (Size j=0; j<values_.size(); j++) { |
74 | 0 | values_[j] = std::max(values_[j], |
75 | 0 | (*arguments_.payoff)(grid[j])); |
76 | 0 | } |
77 | 0 | } |
78 | | |
79 | | } |
80 | | |