Skip to content
Navigation Menu
{{ message }}
forked from eddelbuettel/rquantlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvanilla.cpp
More file actions
316 lines (267 loc) · 15.3 KB
/
Copy pathvanilla.cpp
File metadata and controls
316 lines (267 loc) · 15.3 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
//
// RQuantLib -- R interface to the QuantLib libraries
//
// Copyright (C) 2002 - 2015 Dirk Eddelbuettel <edd@debian.org>
//
// This file is part of RQuantLib.
//
// RQuantLib is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// RQuantLib 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
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with RQuantLib. If not, see <http://www.gnu.org/licenses/>.
#include <rquantlib.h>
// [[Rcpp::interfaces(r, cpp)]]
// [[Rcpp::export]]
Rcpp::List europeanOptionEngine(std::string type,
double underlying,
double strike,
double dividendYield,
double riskFreeRate,
double maturity,
double volatility,
std::vector<double> discreteDividends,
std::vector<double> discreteDividendsTimeUntil) {
#ifdef QL_HIGH_RESOLUTION_DATE
// in minutes
boost::posix_time::time_duration length = boost::posix_time::minutes(maturity * 360 * 24 * 60);
#else
int length = int(maturity*360 + 0.5); // FIXME: this could be better
#endif
QuantLib::Option::Type optionType = getOptionType(type);
QuantLib::Date today = QuantLib::Date::todaysDate();
QuantLib::Settings::instance().evaluationDate() = today;
// new framework as per QuantLib 0.3.5
QuantLib::DayCounter dc = QuantLib::Actual360();
boost::shared_ptr<QuantLib::SimpleQuote> spot(new QuantLib::SimpleQuote( underlying ));
boost::shared_ptr<QuantLib::SimpleQuote> vol(new QuantLib::SimpleQuote( volatility ));
boost::shared_ptr<QuantLib::BlackVolTermStructure> volTS = flatVol(today, vol, dc);
boost::shared_ptr<QuantLib::SimpleQuote> qRate(new QuantLib::SimpleQuote( dividendYield ));
boost::shared_ptr<QuantLib::YieldTermStructure> qTS = flatRate(today, qRate, dc);
boost::shared_ptr<QuantLib::SimpleQuote> rRate(new QuantLib::SimpleQuote( riskFreeRate ));
boost::shared_ptr<QuantLib::YieldTermStructure> rTS = flatRate(today, rRate, dc);
int n = discreteDividendsTimeUntil.size();
std::vector<QuantLib::Date> discreteDividendDates;
#ifdef QL_HIGH_RESOLUTION_DATE
QuantLib::Date exDate(today.dateTime() + length);
if(discreteDividends[0] != 0) {
boost::posix_time::time_duration discreteDividendLength;
for (int i = 0; i < n; i++) {
discreteDividendLength = boost::posix_time::minutes(discreteDividendsTimeUntil[i] * 360 * 24 * 60);
QuantLib::Date dt(today.dateTime() + discreteDividendLength);
discreteDividendDates.push_back(dt);
}
}
#else
QuantLib::Date exDate = today + length;
if(discreteDividends[0] != 0) {
for (int i = 0; i < n; i++) {
discreteDividendDates[i] = today + int(discreteDividendsTimeUntil[i] * 360 + 0.5);
}
}
#endif
boost::shared_ptr<QuantLib::Exercise> exercise(new QuantLib::EuropeanExercise(exDate));
boost::shared_ptr<QuantLib::StrikedTypePayoff> payoff(new QuantLib::PlainVanillaPayoff(optionType, strike));
if(discreteDividends[0] != 0) {
boost::shared_ptr<QuantLib::BlackScholesMertonProcess>
stochProcess(new QuantLib::BlackScholesMertonProcess(QuantLib::Handle<QuantLib::Quote>(spot),
QuantLib::Handle<QuantLib::YieldTermStructure>(qTS),
QuantLib::Handle<QuantLib::YieldTermStructure>(rTS),
QuantLib::Handle<QuantLib::BlackVolTermStructure>(volTS)));
boost::shared_ptr<QuantLib::PricingEngine> engine(
new QuantLib::AnalyticDividendEuropeanEngine(stochProcess));
QuantLib::DividendVanillaOption option(payoff, exercise,
discreteDividendDates, discreteDividends);
option.setPricingEngine(engine);
return Rcpp::List::create(Rcpp::Named("value") = option.NPV(),
Rcpp::Named("delta") = option.delta(),
Rcpp::Named("gamma") = option.gamma(),
Rcpp::Named("vega") = option.vega(),
Rcpp::Named("theta") = option.theta(),
Rcpp::Named("rho") = option.rho(),
Rcpp::Named("divRho") = R_NaReal);
}
else {
boost::shared_ptr<QuantLib::VanillaOption> option = makeOption(payoff, exercise, spot, qTS, rTS, volTS);
return Rcpp::List::create(Rcpp::Named("value") = option->NPV(),
Rcpp::Named("delta") = option->delta(),
Rcpp::Named("gamma") = option->gamma(),
Rcpp::Named("vega") = option->vega(),
Rcpp::Named("theta") = option->theta(),
Rcpp::Named("rho") = option->rho(),
Rcpp::Named("divRho") = option->dividendRho());
}
}
// [[Rcpp::export]]
Rcpp::List americanOptionEngine(std::string type,
double underlying,
double strike,
double dividendYield,
double riskFreeRate,
double maturity,
double volatility,
int timeSteps,
int gridPoints,
std::string engine,
std::vector<double> discreteDividends,
std::vector<double> discreteDividendsTimeUntil) {
#ifdef QL_HIGH_RESOLUTION_DATE
// in minutes
boost::posix_time::time_duration length = boost::posix_time::minutes(maturity * 360 * 24 * 60);
#else
int length = int(maturity * 360 + 0.5); // FIXME: this could be better
#endif
QuantLib::Option::Type optionType = getOptionType(type);
// new framework as per QuantLib 0.3.5, updated for 0.3.7
// updated again for 0.9.0, see eg test-suite/americanoption.cpp
QuantLib::Date today = QuantLib::Date::todaysDate();
QuantLib::Settings::instance().evaluationDate() = today;
QuantLib::DayCounter dc = QuantLib::Actual360();
boost::shared_ptr<QuantLib::SimpleQuote> spot(new QuantLib::SimpleQuote(underlying));
boost::shared_ptr<QuantLib::SimpleQuote> qRate(new QuantLib::SimpleQuote(dividendYield));
boost::shared_ptr<QuantLib::YieldTermStructure> qTS = flatRate(today,qRate,dc);
boost::shared_ptr<QuantLib::SimpleQuote> rRate(new QuantLib::SimpleQuote(riskFreeRate));
boost::shared_ptr<QuantLib::YieldTermStructure> rTS = flatRate(today,rRate,dc);
boost::shared_ptr<QuantLib::SimpleQuote> vol(new QuantLib::SimpleQuote(volatility));
boost::shared_ptr<QuantLib::BlackVolTermStructure> volTS = flatVol(today, vol, dc);
boost::shared_ptr<QuantLib::StrikedTypePayoff> payoff(new QuantLib::PlainVanillaPayoff(optionType, strike));
int n = discreteDividendsTimeUntil.size();
std::vector<QuantLib::Date> discreteDividendDates;
#ifdef QL_HIGH_RESOLUTION_DATE
QuantLib::Date exDate(today.dateTime() + length);
if(discreteDividends[0] != 0) {
boost::posix_time::time_duration discreteDividendLength;
for (int i = 0; i < n; i++) {
discreteDividendLength = boost::posix_time::minutes(discreteDividendsTimeUntil[i] * 360 * 24 * 60);
QuantLib::Date dt(today.dateTime() + discreteDividendLength);
discreteDividendDates.push_back(dt);
}
}
#else
QuantLib::Date exDate = today + length;
if(discreteDividends[0] != 0) {
std::vector<int> discreteDividendLengths;
for (int i = 0; i < n; i++) {
discreteDividendLengths.push_back(int(discreteDividendsTimeUntil[i] * 360 + 0.5));
discreteDividendDates.push_back(today + discreteDividendLengths[i]);
}
}
#endif
boost::shared_ptr<QuantLib::Exercise> exercise(new QuantLib::AmericanExercise(today, exDate));
boost::shared_ptr<QuantLib::BlackScholesMertonProcess>
stochProcess(new QuantLib::BlackScholesMertonProcess(QuantLib::Handle<QuantLib::Quote>(spot),
QuantLib::Handle<QuantLib::YieldTermStructure>(qTS),
QuantLib::Handle<QuantLib::YieldTermStructure>(rTS),
QuantLib::Handle<QuantLib::BlackVolTermStructure>(volTS)));
if(discreteDividends[0] != 0) {
QuantLib::DividendVanillaOption option(payoff, exercise,
discreteDividendDates, discreteDividends);
if (engine=="BaroneAdesiWhaley") {
Rf_warning("Discrete dividends, engine switched to CrankNicolson");
engine = "CrankNicolson";
}
if (engine=="CrankNicolson") { // FDDividendAmericanEngine only works with CrankNicolson
// suggestion by Bryan Lewis: use CrankNicolson for greeks
boost::shared_ptr<QuantLib::PricingEngine>
fdcnengine(new QuantLib::FDDividendAmericanEngine<QuantLib::CrankNicolson>(stochProcess, timeSteps, gridPoints));
option.setPricingEngine(fdcnengine);
return Rcpp::List::create(Rcpp::Named("value") = option.NPV(),
Rcpp::Named("delta") = option.delta(),
Rcpp::Named("gamma") = option.gamma(),
Rcpp::Named("vega") = R_NaReal,
Rcpp::Named("theta") = R_NaReal,
Rcpp::Named("rho") = R_NaReal,
Rcpp::Named("divRho") = R_NaReal);
} else {
throw std::range_error("Unknown engine " + engine);
}
} else {
QuantLib::VanillaOption option(payoff, exercise);
if (engine=="BaroneAdesiWhaley") {
// new from 0.3.7 BaroneAdesiWhaley
boost::shared_ptr<QuantLib::PricingEngine> engine(new QuantLib::BaroneAdesiWhaleyApproximationEngine(stochProcess));
option.setPricingEngine(engine);
return Rcpp::List::create(Rcpp::Named("value") = option.NPV(),
Rcpp::Named("delta") = R_NaReal,
Rcpp::Named("gamma") = R_NaReal,
Rcpp::Named("vega") = R_NaReal,
Rcpp::Named("theta") = R_NaReal,
Rcpp::Named("rho") = R_NaReal,
Rcpp::Named("divRho") = R_NaReal);
} else if (engine=="CrankNicolson") {
// suggestion by Bryan Lewis: use CrankNicolson for greeks
boost::shared_ptr<QuantLib::PricingEngine>
fdcnengine(new QuantLib::FDAmericanEngine<QuantLib::CrankNicolson>(stochProcess, timeSteps, gridPoints));
option.setPricingEngine(fdcnengine);
return Rcpp::List::create(Rcpp::Named("value") = option.NPV(),
Rcpp::Named("delta") = option.delta(),
Rcpp::Named("gamma") = option.gamma(),
Rcpp::Named("vega") = R_NaReal,
Rcpp::Named("theta") = R_NaReal,
Rcpp::Named("rho") = R_NaReal,
Rcpp::Named("divRho") = R_NaReal);
} else {
throw std::range_error("Unknown engine " + engine);
}
}
}
// [[Rcpp::export]]
Rcpp::List europeanOptionArraysEngine(std::string type, Rcpp::NumericMatrix par) {
QuantLib::Option::Type optionType = getOptionType(type);
int n = par.nrow();
Rcpp::NumericVector value(n), delta(n), gamma(n), vega(n), theta(n), rho(n), divrho(n);
QuantLib::Date today = QuantLib::Date::todaysDate();
QuantLib::Settings::instance().evaluationDate() = today;
QuantLib::DayCounter dc = QuantLib::Actual360();
for (int i=0; i<n; i++) {
double underlying = par(i, 0); // first column
double strike = par(i, 1); // second column
QuantLib::Spread dividendYield = par(i, 2); // third column
QuantLib::Rate riskFreeRate = par(i, 3); // fourth column
QuantLib::Time maturity = par(i, 4); // fifth column
#ifdef QL_HIGH_RESOLUTION_DATE
// in minutes
boost::posix_time::time_duration length = boost::posix_time::minutes(maturity * 360 * 24 * 60);
#else
int length = int(maturity*360 + 0.5); // FIXME: this could be better
#endif
double volatility = par(i, 5); // sixth column
boost::shared_ptr<QuantLib::SimpleQuote> spot(new QuantLib::SimpleQuote( underlying ));
boost::shared_ptr<QuantLib::SimpleQuote> vol(new QuantLib::SimpleQuote( volatility ));
boost::shared_ptr<QuantLib::BlackVolTermStructure> volTS = flatVol(today, vol, dc);
boost::shared_ptr<QuantLib::SimpleQuote> qRate(new QuantLib::SimpleQuote( dividendYield ));
boost::shared_ptr<QuantLib::YieldTermStructure> qTS = flatRate(today, qRate, dc);
boost::shared_ptr<QuantLib::SimpleQuote> rRate(new QuantLib::SimpleQuote( riskFreeRate ));
boost::shared_ptr<QuantLib::YieldTermStructure> rTS = flatRate(today, rRate, dc);
#ifdef QL_HIGH_RESOLUTION_DATE
QuantLib::Date exDate(today.dateTime() + length);
#else
QuantLib::Date exDate = today + length;
#endif
boost::shared_ptr<QuantLib::Exercise> exercise(new QuantLib::EuropeanExercise(exDate));
boost::shared_ptr<QuantLib::StrikedTypePayoff> payoff(new QuantLib::PlainVanillaPayoff(optionType, strike));
boost::shared_ptr<QuantLib::VanillaOption> option = makeOption(payoff, exercise, spot, qTS, rTS, volTS);
value[i] = option->NPV();
delta[i] = option->delta();
gamma[i] = option->gamma();
vega[i] = option->vega();
theta[i] = option->theta();
rho[i] = option->rho();
divrho[i] = option->dividendRho();
}
return Rcpp::List::create(Rcpp::Named("value") = value,
Rcpp::Named("delta") = delta,
Rcpp::Named("gamma") = gamma,
Rcpp::Named("vega") = vega,
Rcpp::Named("theta") = theta,
Rcpp::Named("rho") = rho,
Rcpp::Named("divRho") = divrho);
}
You can’t perform that action at this time.
