QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
kirkspreadoptionengine.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2011 Master IMAFA - Polytech'Nice Sophia - Université de Nice Sophia Antipolis
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/exercise.hpp>
21#include <ql/experimental/exoticoptions/kirkspreadoptionengine.hpp>
22#include <ql/math/distributions/normaldistribution.hpp>
23#include <utility>
24
25using namespace std;
26
27namespace QuantLib {
28
29 KirkSpreadOptionEngine::KirkSpreadOptionEngine(ext::shared_ptr<BlackProcess> process1,
30 ext::shared_ptr<BlackProcess> process2,
31 Handle<Quote> correlation)
32 : process1_(std::move(process1)), process2_(std::move(process2)), rho_(std::move(correlation)) {
36 }
37
39
40 // First: tests on types
41 QL_REQUIRE(arguments_.exercise->type() == Exercise::European,
42 "not an European Option");
43
44 ext::shared_ptr<PlainVanillaPayoff> payoff =
45 ext::dynamic_pointer_cast<PlainVanillaPayoff>(arguments_.payoff);
46 QL_REQUIRE(payoff, "not a plain-vanilla payoff");
47
48 // forward values - futures, so b=0
49 Real forward1 = process1_->stateVariable()->value();
50 Real forward2 = process2_->stateVariable()->value();
51
52 Date exerciseDate = arguments_.exercise->lastDate();
53
54 // Volatilities
55 Real sigma1 =
56 process1_->blackVolatility()->blackVol(exerciseDate,forward1);
57 Real sigma2 =
58 process2_->blackVolatility()->blackVol(exerciseDate,forward2);
59
60 DiscountFactor riskFreeDiscount =
61 process1_->riskFreeRate()->discount(exerciseDate);
62
63 Real strike = payoff->strike();
64
65 // Unique F (forward) value for pricing
66 Real F = forward1/(forward2+strike);
67
68 // Its volatility
69 Real sigma =
70 sqrt(pow(sigma1,2)
71 + pow((sigma2*(forward2/(forward2+strike))),2)
72 - 2*rho_->value()*sigma1*sigma2*(forward2/(forward2+strike)));
73
74 // Day counter and Dates handling variables
75 DayCounter rfdc = process1_->riskFreeRate()->dayCounter();
76 Time t = rfdc.yearFraction(process1_->riskFreeRate()->referenceDate(),
77 arguments_.exercise->lastDate());
78
79 // Black-Scholes solution values
80 Real d1 = (log(F)+ 0.5*pow(sigma,2)*t) / (sigma*sqrt(t));
81 Real d2 = d1 - sigma*sqrt(t);
82
85 Real Nd1 = cum(d1);
86 Real Nd2 = cum(d2);
87 Real NMd1 = cum(-d1);
88 Real NMd2 = cum(-d2);
89
90 Option::Type optionType = payoff->optionType();
91
92 if (optionType==Option::Call) {
93 results_.value = riskFreeDiscount*(F*Nd1-Nd2)*(forward2+strike);
94 } else {
95 results_.value = riskFreeDiscount*(NMd2 -F*NMd1)*(forward2+strike);
96 }
97
98 Real callValue = optionType == Option::Call ? results_.value : riskFreeDiscount*(F*Nd1-Nd2)*(forward2+strike);
99 results_.theta = -((log(riskFreeDiscount)/t)*callValue
100 + riskFreeDiscount*(forward1*sigma)/(2*sqrt(t))*pdf(d1));
101 }
102
103}
104
Cumulative normal distribution function.
Concrete date class.
Definition: date.hpp:125
day counter class
Definition: daycounter.hpp:44
Time yearFraction(const Date &, const Date &, const Date &refPeriodStart=Date(), const Date &refPeriodEnd=Date()) const
Returns the period between two dates as a fraction of year.
Definition: daycounter.hpp:128
Shared handle to an observable.
Definition: handle.hpp:41
KirkSpreadOptionEngine(ext::shared_ptr< BlackProcess > process1, ext::shared_ptr< BlackProcess > process2, Handle< Quote > correlation)
ext::shared_ptr< BlackProcess > process1_
ext::shared_ptr< BlackProcess > process2_
Normal distribution function.
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:228
ext::shared_ptr< Exercise > exercise
Definition: option.hpp:65
ext::shared_ptr< Payoff > payoff
Definition: option.hpp:64
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
Real DiscountFactor
discount factor between dates
Definition: types.hpp:66
Definition: any.hpp:35
STL namespace.