1 /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 3 /* 4 Copyright (C) 2003, 2004 Ferdinando Ametrano 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/asianoption.hpp> 22 #include <ql/time/date.hpp> 23 24 namespace QuantLib { 25 DiscreteAveragingAsianOption(Average::Type averageType,Real runningAccumulator,Size pastFixings,const std::vector<Date> & fixingDates,const ext::shared_ptr<StrikedTypePayoff> & payoff,const ext::shared_ptr<Exercise> & exercise)26 DiscreteAveragingAsianOption::DiscreteAveragingAsianOption( 27 Average::Type averageType, 28 Real runningAccumulator, 29 Size pastFixings, 30 const std::vector<Date>& fixingDates, 31 const ext::shared_ptr<StrikedTypePayoff>& payoff, 32 const ext::shared_ptr<Exercise>& exercise) 33 : OneAssetOption(payoff, exercise), 34 averageType_(averageType), runningAccumulator_(runningAccumulator), 35 pastFixings_(pastFixings), fixingDates_(fixingDates) { 36 std::sort(fixingDates_.begin(), fixingDates_.end()); 37 } 38 setupArguments(PricingEngine::arguments * args) const39 void DiscreteAveragingAsianOption::setupArguments( 40 PricingEngine::arguments* args) const { 41 42 OneAssetOption::setupArguments(args); 43 44 DiscreteAveragingAsianOption::arguments* moreArgs = 45 dynamic_cast<DiscreteAveragingAsianOption::arguments*>(args); 46 QL_REQUIRE(moreArgs != 0, "wrong argument type"); 47 moreArgs->averageType = averageType_; 48 moreArgs->runningAccumulator = runningAccumulator_; 49 moreArgs->pastFixings = pastFixings_; 50 moreArgs->fixingDates = fixingDates_; 51 } 52 validate() const53 void DiscreteAveragingAsianOption::arguments::validate() const { 54 55 OneAssetOption::arguments::validate(); 56 57 QL_REQUIRE(Integer(averageType) != -1, "unspecified average type"); 58 QL_REQUIRE(pastFixings != Null<Size>(), "null past-fixing number"); 59 QL_REQUIRE(runningAccumulator != Null<Real>(), "null running product"); 60 switch (averageType) { 61 case Average::Arithmetic: 62 QL_REQUIRE(runningAccumulator >= 0.0, 63 "non negative running sum required: " 64 << runningAccumulator << " not allowed"); 65 break; 66 case Average::Geometric: 67 QL_REQUIRE(runningAccumulator > 0.0, 68 "positive running product required: " 69 << runningAccumulator << " not allowed"); 70 break; 71 default: 72 QL_FAIL("invalid average type"); 73 } 74 75 // check fixingTimes_ here 76 } 77 78 79 80 ContinuousAveragingAsianOption(Average::Type averageType,const ext::shared_ptr<StrikedTypePayoff> & payoff,const ext::shared_ptr<Exercise> & exercise)81 ContinuousAveragingAsianOption::ContinuousAveragingAsianOption( 82 Average::Type averageType, 83 const ext::shared_ptr<StrikedTypePayoff>& payoff, 84 const ext::shared_ptr<Exercise>& exercise) 85 : OneAssetOption(payoff, exercise), 86 averageType_(averageType) {} 87 setupArguments(PricingEngine::arguments * args) const88 void ContinuousAveragingAsianOption::setupArguments( 89 PricingEngine::arguments* args) const { 90 91 OneAssetOption::setupArguments(args); 92 93 ContinuousAveragingAsianOption::arguments* moreArgs = 94 dynamic_cast<ContinuousAveragingAsianOption::arguments*>(args); 95 QL_REQUIRE(moreArgs != 0, "wrong argument type"); 96 moreArgs->averageType = averageType_; 97 } 98 validate() const99 void ContinuousAveragingAsianOption::arguments::validate() const { 100 101 OneAssetOption::arguments::validate(); 102 103 QL_REQUIRE(Integer(averageType) != -1, "unspecified average type"); 104 } 105 106 } 107 108