QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
mcdoublebarrierengine.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2020 Lew Wei Hao
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 <http://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/experimental/barrieroption/mcdoublebarrierengine.hpp>
21#include <utility>
22
23namespace QuantLib {
24
26 Real barrierLow,
27 Real barrierHigh,
28 Real rebate,
29 Option::Type type,
30 Real strike,
31 std::vector<DiscountFactor> discounts)
32 : barrierType_(barrierType), barrierLow_(barrierLow), barrierHigh_(barrierHigh),
33 rebate_(rebate), payoff_(type, strike), discounts_(std::move(discounts)) {
34 QL_REQUIRE(strike>=0.0,
35 "strike less than zero not allowed");
36 QL_REQUIRE(barrierLow>0.0,
37 "low barrier less/equal zero not allowed");
38 QL_REQUIRE(barrierHigh>0.0,
39 "high barrier less/equal zero not allowed");
40 }
41
43 static Size null = Null<Size>();
44 Size n = path.length();
45 QL_REQUIRE(n>1, "the path cannot be empty");
46
47 bool isOptionActive = false;
48 Size knockNode = null;
49 Real terminal_price = path.back();
50 Real new_asset_price;
51 Size i;
52
53 switch (barrierType_) {
55 isOptionActive = true;
56 for (i = 0; i < n-1; i++) {
57 new_asset_price = path[i + 1];
58
59 if (new_asset_price >= barrierHigh_ || new_asset_price <= barrierLow_){
60 isOptionActive = false;
61 if (knockNode == null)
62 knockNode = i+1;
63 break;
64 }
65 }
66 break;
68 isOptionActive = false;
69 for (i = 0; i < n-1; i++) {
70 new_asset_price = path[i + 1];
71
72 if (new_asset_price >= barrierHigh_ || new_asset_price <= barrierLow_){
73 isOptionActive = true;
74 if (knockNode == null)
75 knockNode = i+1;
76 break;
77 }
78 }
79 break;
80 default:
81 QL_FAIL("unknown barrier type");
82 }
83
84 if (isOptionActive) {
85 return payoff_(terminal_price) * discounts_.back();
86 } else {
87 switch (barrierType_) {
89 return rebate_*discounts_[knockNode];
91 return rebate_*discounts_.back();
92 default:
93 QL_FAIL("unknown barrier type");
94 }
95 }
96 }
97
98}
std::vector< DiscountFactor > discounts_
DoubleBarrierPathPricer(DoubleBarrier::Type barrierType, Real barrierLow, Real barrieHigh, Real rebate, Option::Type type, Real strike, std::vector< DiscountFactor > discounts)
Real operator()(const Path &path) const override
template class providing a null value for a given type.
Definition: null.hpp:76
single-factor random walk
Definition: path.hpp:40
Size length() const
Definition: path.hpp:94
Real back() const
final asset value
Definition: path.hpp:130
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
STL namespace.