| 1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | |
| 3 | /* |
| 4 | Copyright (C) 2006 Warren Chou |
| 5 | Copyright (C) 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 | |
| 21 | #include <ql/instruments/lookbackoption.hpp> |
| 22 | |
| 23 | namespace QuantLib { |
| 24 | |
| 25 | ContinuousFloatingLookbackOption::ContinuousFloatingLookbackOption( |
| 26 | Real minmax, |
| 27 | const ext::shared_ptr<TypePayoff>& payoff, |
| 28 | const ext::shared_ptr<Exercise>& exercise) |
| 29 | : OneAssetOption(payoff, exercise), |
| 30 | minmax_(minmax) {} |
| 31 | |
| 32 | void ContinuousFloatingLookbackOption::setupArguments( |
| 33 | PricingEngine::arguments* args) const { |
| 34 | |
| 35 | OneAssetOption::setupArguments(args); |
| 36 | |
| 37 | auto* moreArgs = dynamic_cast<ContinuousFloatingLookbackOption::arguments*>(args); |
| 38 | QL_REQUIRE(moreArgs != nullptr, "wrong argument type" ); |
| 39 | moreArgs->minmax = minmax_; |
| 40 | } |
| 41 | |
| 42 | void ContinuousFloatingLookbackOption::arguments::validate() const { |
| 43 | |
| 44 | OneAssetOption::arguments::validate(); |
| 45 | |
| 46 | QL_REQUIRE(minmax != Null<Real>(), "null prior extremum" ); |
| 47 | QL_REQUIRE(minmax >= 0.0, "nonnegative prior extremum required: " |
| 48 | << minmax << " not allowed" ); |
| 49 | } |
| 50 | |
| 51 | |
| 52 | ContinuousFixedLookbackOption::ContinuousFixedLookbackOption( |
| 53 | Real minmax, |
| 54 | const ext::shared_ptr<StrikedTypePayoff>& payoff, |
| 55 | const ext::shared_ptr<Exercise>& exercise) |
| 56 | : OneAssetOption(payoff, exercise), |
| 57 | minmax_(minmax) {} |
| 58 | |
| 59 | void ContinuousFixedLookbackOption::setupArguments( |
| 60 | PricingEngine::arguments* args) const { |
| 61 | |
| 62 | OneAssetOption::setupArguments(args); |
| 63 | |
| 64 | auto* moreArgs = dynamic_cast<ContinuousFixedLookbackOption::arguments*>(args); |
| 65 | QL_REQUIRE(moreArgs != nullptr, "wrong argument type" ); |
| 66 | moreArgs->minmax = minmax_; |
| 67 | } |
| 68 | |
| 69 | void ContinuousFixedLookbackOption::arguments::validate() const { |
| 70 | |
| 71 | OneAssetOption::arguments::validate(); |
| 72 | |
| 73 | QL_REQUIRE(minmax != Null<Real>(), "null prior extremum" ); |
| 74 | QL_REQUIRE(minmax >= 0.0, "nonnegative prior extremum required: " |
| 75 | << minmax << " not allowed" ); |
| 76 | } |
| 77 | |
| 78 | ContinuousPartialFloatingLookbackOption::ContinuousPartialFloatingLookbackOption( |
| 79 | Real minmax, |
| 80 | Real lambda, |
| 81 | Date lookbackPeriodEnd, |
| 82 | const ext::shared_ptr<TypePayoff>& payoff, |
| 83 | const ext::shared_ptr<Exercise>& exercise) |
| 84 | : ContinuousFloatingLookbackOption(minmax, payoff, exercise), |
| 85 | lambda_(lambda), |
| 86 | lookbackPeriodEnd_(lookbackPeriodEnd) {} |
| 87 | |
| 88 | void ContinuousPartialFloatingLookbackOption::setupArguments( |
| 89 | PricingEngine::arguments* args) const { |
| 90 | |
| 91 | ContinuousFloatingLookbackOption::setupArguments(args); |
| 92 | |
| 93 | auto* moreArgs = dynamic_cast<ContinuousPartialFloatingLookbackOption::arguments*>(args); |
| 94 | QL_REQUIRE(moreArgs != nullptr, "wrong argument type" ); |
| 95 | moreArgs->lambda = lambda_; |
| 96 | moreArgs->lookbackPeriodEnd = lookbackPeriodEnd_; |
| 97 | } |
| 98 | |
| 99 | void ContinuousPartialFloatingLookbackOption::arguments::validate() const { |
| 100 | |
| 101 | ContinuousFloatingLookbackOption::arguments::validate(); |
| 102 | |
| 103 | ext::shared_ptr<EuropeanExercise> europeanExercise = |
| 104 | ext::dynamic_pointer_cast<EuropeanExercise>(r: exercise); |
| 105 | QL_REQUIRE(lookbackPeriodEnd <= europeanExercise->lastDate(), |
| 106 | "lookback start date must be earlier than exercise date" ); |
| 107 | |
| 108 | ext::shared_ptr<FloatingTypePayoff> floatingTypePayoff = |
| 109 | ext::dynamic_pointer_cast<FloatingTypePayoff>(r: payoff); |
| 110 | |
| 111 | if (floatingTypePayoff->optionType() == Option::Call) { |
| 112 | QL_REQUIRE(lambda >= 1.0, |
| 113 | "lambda should be greater than or equal to 1 for calls" ); |
| 114 | } |
| 115 | if (floatingTypePayoff->optionType() == Option::Put) { |
| 116 | QL_REQUIRE(lambda <= 1.0, |
| 117 | "lambda should be smaller than or equal to 1 for puts" ); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | ContinuousPartialFixedLookbackOption::ContinuousPartialFixedLookbackOption( |
| 122 | Date lookbackPeriodStart, |
| 123 | const ext::shared_ptr<StrikedTypePayoff>& payoff, |
| 124 | const ext::shared_ptr<Exercise>& exercise) |
| 125 | : ContinuousFixedLookbackOption(0, payoff, exercise), |
| 126 | lookbackPeriodStart_(lookbackPeriodStart) {} |
| 127 | |
| 128 | void ContinuousPartialFixedLookbackOption::setupArguments( |
| 129 | PricingEngine::arguments* args) const { |
| 130 | |
| 131 | ContinuousFixedLookbackOption::setupArguments(args); |
| 132 | |
| 133 | auto* moreArgs = dynamic_cast<ContinuousPartialFixedLookbackOption::arguments*>(args); |
| 134 | QL_REQUIRE(moreArgs != nullptr, "wrong argument type" ); |
| 135 | moreArgs->lookbackPeriodStart = lookbackPeriodStart_; |
| 136 | } |
| 137 | |
| 138 | void ContinuousPartialFixedLookbackOption::arguments::validate() const { |
| 139 | |
| 140 | ContinuousFixedLookbackOption::arguments::validate(); |
| 141 | |
| 142 | ext::shared_ptr<EuropeanExercise> europeanExercise = |
| 143 | ext::dynamic_pointer_cast<EuropeanExercise>(r: exercise); |
| 144 | QL_REQUIRE(lookbackPeriodStart <= europeanExercise->lastDate(), |
| 145 | "lookback start date must be earlier than exercise date" ); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | |