Skip to content

Commit

Permalink
Merge pull request #805.
Browse files Browse the repository at this point in the history
Monte Carlo double barrier engine
  • Loading branch information
lballabio committed May 1, 2020
2 parents 1dbc772 + ff9d9f4 commit 60b2be1
Show file tree
Hide file tree
Showing 10 changed files with 529 additions and 1 deletion.
2 changes: 2 additions & 0 deletions QuantLib.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,7 @@
<ClInclude Include="ql\experimental\barrieroption\all.hpp" />
<ClInclude Include="ql\experimental\barrieroption\analyticdoublebarrierbinaryengine.hpp" />
<ClInclude Include="ql\experimental\barrieroption\analyticdoublebarrierengine.hpp" />
<ClInclude Include="ql\experimental\barrieroption\mcdoublebarrierengine.hpp" />
<ClInclude Include="ql\experimental\barrieroption\binomialdoublebarrierengine.hpp" />
<ClInclude Include="ql\experimental\barrieroption\discretizeddoublebarrieroption.hpp" />
<ClInclude Include="ql\experimental\barrieroption\doublebarrieroption.hpp" />
Expand Down Expand Up @@ -1892,6 +1893,7 @@
<ClCompile Include="ql\experimental\averageois\makearithmeticaverageois.cpp" />
<ClCompile Include="ql\experimental\barrieroption\analyticdoublebarrierbinaryengine.cpp" />
<ClCompile Include="ql\experimental\barrieroption\analyticdoublebarrierengine.cpp" />
<ClCompile Include="ql\experimental\barrieroption\mcdoublebarrierengine.cpp" />
<ClCompile Include="ql\experimental\barrieroption\discretizeddoublebarrieroption.cpp" />
<ClCompile Include="ql\experimental\barrieroption\doublebarrieroption.cpp" />
<ClCompile Include="ql\experimental\barrieroption\doublebarriertype.cpp" />
Expand Down
6 changes: 6 additions & 0 deletions QuantLib.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -3546,6 +3546,9 @@
<ClInclude Include="ql\experimental\barrieroption\analyticdoublebarrierengine.hpp">
<Filter>experimental\barrieroption</Filter>
</ClInclude>
<ClInclude Include="ql\experimental\barrieroption\mcdoublebarrierengine.hpp">
<Filter>experimental\barrieroption</Filter>
</ClInclude>
<ClInclude Include="ql\experimental\barrieroption\binomialdoublebarrierengine.hpp">
<Filter>experimental\barrieroption</Filter>
</ClInclude>
Expand Down Expand Up @@ -6402,6 +6405,9 @@
<ClCompile Include="ql\experimental\barrieroption\analyticdoublebarrierengine.cpp">
<Filter>experimental\barrieroption</Filter>
</ClCompile>
<ClCompile Include="ql\experimental\barrieroption\mcdoublebarrierengine.cpp">
<Filter>experimental\barrieroption</Filter>
</ClCompile>
<ClCompile Include="ql\experimental\barrieroption\discretizeddoublebarrieroption.cpp">
<Filter>experimental\barrieroption</Filter>
</ClCompile>
Expand Down
2 changes: 2 additions & 0 deletions ql/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ set(QuantLib_SRC
experimental/averageois/makearithmeticaverageois.cpp
experimental/barrieroption/analyticdoublebarrierbinaryengine.cpp
experimental/barrieroption/analyticdoublebarrierengine.cpp
experimental/barrieroption/mcdoublebarrierengine.cpp
experimental/barrieroption/discretizeddoublebarrieroption.cpp
experimental/barrieroption/doublebarrieroption.cpp
experimental/barrieroption/doublebarriertype.cpp
Expand Down Expand Up @@ -965,6 +966,7 @@ set(QuantLib_HDR
experimental/barrieroption/all.hpp
experimental/barrieroption/analyticdoublebarrierbinaryengine.hpp
experimental/barrieroption/analyticdoublebarrierengine.hpp
experimental/barrieroption/mcdoublebarrierengine.hpp
experimental/barrieroption/binomialdoublebarrierengine.hpp
experimental/barrieroption/discretizeddoublebarrieroption.hpp
experimental/barrieroption/doublebarrieroption.hpp
Expand Down
2 changes: 2 additions & 0 deletions ql/experimental/barrieroption/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ this_include_HEADERS = \
all.hpp \
analyticdoublebarrierbinaryengine.hpp \
analyticdoublebarrierengine.hpp \
mcdoublebarrierengine.hpp \
binomialdoublebarrierengine.hpp \
discretizeddoublebarrieroption.hpp \
doublebarrieroption.hpp \
Expand All @@ -20,6 +21,7 @@ this_include_HEADERS = \
cpp_files = \
analyticdoublebarrierbinaryengine.cpp \
analyticdoublebarrierengine.cpp \
mcdoublebarrierengine.cpp \
discretizeddoublebarrieroption.cpp \
doublebarrieroption.cpp \
doublebarriertype.cpp \
Expand Down
1 change: 1 addition & 0 deletions ql/experimental/barrieroption/all.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <ql/experimental/barrieroption/analyticdoublebarrierbinaryengine.hpp>
#include <ql/experimental/barrieroption/analyticdoublebarrierengine.hpp>
#include <ql/experimental/barrieroption/mcdoublebarrierengine.hpp>
#include <ql/experimental/barrieroption/binomialdoublebarrierengine.hpp>
#include <ql/experimental/barrieroption/discretizeddoublebarrieroption.hpp>
#include <ql/experimental/barrieroption/doublebarrieroption.hpp>
Expand Down
98 changes: 98 additions & 0 deletions ql/experimental/barrieroption/mcdoublebarrierengine.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*
Copyright (C) 2020 Lew Wei Hao
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it
under the terms of the QuantLib license. You should have received a
copy of the license along with this program; if not, please email
<quantlib-dev@lists.sf.net>. The license is also available online at
<http://quantlib.org/license.shtml>.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/

#include <ql/experimental/barrieroption/mcdoublebarrierengine.hpp>

namespace QuantLib {

DoubleBarrierPathPricer::DoubleBarrierPathPricer(
DoubleBarrier::Type barrierType,
Real barrierLow,
Real barrierHigh,
Real rebate,
Option::Type type,
Real strike,
const std::vector<DiscountFactor>& discounts)
: barrierType_(barrierType), barrierLow_(barrierLow), barrierHigh_(barrierHigh),
rebate_(rebate), payoff_(type, strike), discounts_(discounts) {
QL_REQUIRE(strike>=0.0,
"strike less than zero not allowed");
QL_REQUIRE(barrierLow>0.0,
"low barrier less/equal zero not allowed");
QL_REQUIRE(barrierHigh>0.0,
"high barrier less/equal zero not allowed");
}

Real DoubleBarrierPathPricer::operator()(const Path& path) const {
static Size null = Null<Size>();
Size n = path.length();
QL_REQUIRE(n>1, "the path cannot be empty");

bool isOptionActive = false;
Size knockNode = null;
Real terminal_price = path.back();
Real new_asset_price;
Size i;

switch (barrierType_) {
case DoubleBarrier::KnockOut:
isOptionActive = true;
for (i = 0; i < n-1; i++) {
new_asset_price = path[i + 1];

if (new_asset_price >= barrierHigh_ || new_asset_price <= barrierLow_){
isOptionActive = false;
if (knockNode == null)
knockNode = i+1;
break;
}
}
break;
case DoubleBarrier::KnockIn:
isOptionActive = false;
for (i = 0; i < n-1; i++) {
new_asset_price = path[i + 1];

if (new_asset_price >= barrierHigh_ || new_asset_price <= barrierLow_){
isOptionActive = true;
if (knockNode == null)
knockNode = i+1;
break;
}
}
break;
default:
QL_FAIL("unknown barrier type");
}

if (isOptionActive) {
return payoff_(terminal_price) * discounts_.back();
} else {
switch (barrierType_) {
case DoubleBarrier::KnockOut:
return rebate_*discounts_[knockNode];
case DoubleBarrier::KnockIn:
return rebate_*discounts_.back();
default:
QL_FAIL("unknown barrier type");
}
}
}

}
Loading

0 comments on commit 60b2be1

Please sign in to comment.