1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2010, 2011 Klaus Spanderen
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.
*/
/*! \file vanillaswingoption.cpp
\brief vanilla swing option class
*/
#include <ql/event.hpp>
#include <ql/instruments/vanillaswingoption.hpp>
namespace QuantLib {
namespace {
const Size secPerDay = 24u*3600u;
std::pair<std::vector<Date>, std::vector<Size> >
createDateTimes(const Date& from, const Date& to, Size stepSize) {
std::vector<Size> secs;
std::vector<Date> dates;
Date iterDate = from;
Size iterStepSize = 0u;
while (iterDate <= to) {
dates.push_back(iterDate);
secs.push_back(iterStepSize);
iterStepSize+=stepSize;
if (iterStepSize >= secPerDay) {
iterDate+=1L;
iterStepSize%=secPerDay;
}
}
return std::pair<std::vector<Date>,std::vector<Size> >(dates, secs);
}
}
SwingExercise::SwingExercise(const std::vector<Date>& dates,
const std::vector<Size>& seconds)
: BermudanExercise(dates),
seconds_(seconds.empty() ? std::vector<Size>(dates.size(), 0u)
: seconds) {
QL_REQUIRE(dates_.size() == seconds_.size(),
"dates and seconds must have the same size");
for (Size i=0; i < dates_.size(); ++i) {
QL_REQUIRE(seconds_[i] < secPerDay,
"a date can not have more than 24*3600 seconds");
if (i > 0) {
QL_REQUIRE(dates_[i-1] < dates_[i]
|| (dates_[i-1] == dates_[i]
&& seconds_[i-1] < seconds_[i]),
"date times must be sorted");
}
}
}
SwingExercise::SwingExercise(const Date& from,
const Date& to, Size stepSizeSecs)
: BermudanExercise(createDateTimes(from, to, stepSizeSecs).first),
seconds_(createDateTimes(from, to, stepSizeSecs).second) {
}
const std::vector<Size>& SwingExercise::seconds() const { return seconds_; }
std::vector<Time> SwingExercise::exerciseTimes(const DayCounter& dc,
const Date& refDate) const {
std::vector<Time> exerciseTimes;
exerciseTimes.reserve(dates().size());
for (Size i=0; i<dates().size(); ++i) {
Time t = dc.yearFraction(refDate, dates()[i]);
const Time dt
= dc.yearFraction(refDate, dates()[i]+Period(1u, Days)) - t;
t += dt*seconds()[i]/(24*3600.);
QL_REQUIRE(t >= 0, "exercise dates must not contain past date");
exerciseTimes.push_back(t);
}
return exerciseTimes;
}
void VanillaSwingOption::arguments::validate() const {
QL_REQUIRE(payoff, "no payoff given");
QL_REQUIRE(exercise, "no exercise given");
QL_REQUIRE(minExerciseRights <= maxExerciseRights,
"minExerciseRights <= maxExerciseRights")
QL_REQUIRE(exercise->dates().size() >= maxExerciseRights,
"number of exercise rights exceeds "
"number of exercise dates");
}
void VanillaSwingOption::setupArguments(
PricingEngine::arguments* args) const {
VanillaSwingOption::arguments* arguments =
dynamic_cast<VanillaSwingOption::arguments*>(args);
QL_REQUIRE(arguments != 0, "wrong argument type");
arguments->payoff
= boost::dynamic_pointer_cast<StrikedTypePayoff>(payoff_);
arguments->exercise
= boost::dynamic_pointer_cast<SwingExercise>(exercise_);
arguments->minExerciseRights = minExerciseRights_;
arguments->maxExerciseRights = maxExerciseRights_;
}
bool VanillaSwingOption::isExpired() const {
return detail::simple_event(exercise_->lastDate()).hasOccurred();
}
}
|