forked from eddelbuettel/rquantlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbarrier_binary.cpp
More file actions
241 lines (208 loc) · 12.1 KB
/
Copy pathbarrier_binary.cpp
File metadata and controls
241 lines (208 loc) · 12.1 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
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
//
// RQuantLib -- R interface to the QuantLib libraries
//
// Copyright (C) 2002 - 2014 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::export]]
Rcpp::List binaryOptionEngine(std::string binType,
std::string type,
std::string excType,
double underlying,
double strike,
double dividendYield,
double riskFreeRate,
double maturity,
double volatility,
double cashPayoff) {
int length = int(maturity*360 + 0.5); // FIXME: this could be better, but same rounding in QL
QuantLib::Option::Type optionType = getOptionType(type);
// new QuantLib 0.3.5 framework: digitals, updated for 0.3.7
// updated again for QuantLib 0.9.0,
// cf QuantLib-0.9.0/test-suite/digitaloption.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;
if (binType=="cash") {
boost::shared_ptr<QuantLib::StrikedTypePayoff> con(new QuantLib::CashOrNothingPayoff(optionType, strike, cashPayoff));
payoff = con;
} else if (binType=="asset") {
boost::shared_ptr<QuantLib::StrikedTypePayoff> aon(new QuantLib::AssetOrNothingPayoff(optionType, strike));
payoff = aon;
} else if (binType=="gap") {
boost::shared_ptr<QuantLib::StrikedTypePayoff> gap(new QuantLib::GapPayoff(optionType, strike, cashPayoff));
payoff = gap;
} else {
throw std::range_error("Unknown binary option type " + binType);
}
QuantLib::Date exDate = today + length;
boost::shared_ptr<QuantLib::Exercise> exercise;
if (excType=="american") {
boost::shared_ptr<QuantLib::Exercise> amEx(new QuantLib::AmericanExercise(today, exDate));
exercise = amEx;
} else if (excType=="european") {
boost::shared_ptr<QuantLib::Exercise> euEx(new QuantLib::EuropeanExercise(exDate));
exercise = euEx;
} else {
throw std::range_error("Unknown binary exercise type " + excType);
}
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;
if (excType=="american") {
boost::shared_ptr<QuantLib::PricingEngine> amEng(new QuantLib::AnalyticDigitalAmericanEngine(stochProcess));
engine = amEng;
} else if (excType=="european") {
boost::shared_ptr<QuantLib::PricingEngine> euEng(new QuantLib::AnalyticEuropeanEngine(stochProcess));
engine = euEng;
} else {
throw std::range_error("Unknown binary exercise type " + excType);
}
QuantLib::VanillaOption opt(payoff, exercise);
opt.setPricingEngine(engine);
Rcpp::List rl = Rcpp::List::create(Rcpp::Named("value") = opt.NPV(),
Rcpp::Named("delta") = opt.delta(),
Rcpp::Named("gamma") = opt.gamma(),
Rcpp::Named("vega") = (excType=="european") ? opt.vega() : R_NaN,
Rcpp::Named("theta") = (excType=="european") ? opt.theta() : R_NaN,
Rcpp::Named("rho") = (excType=="european") ? opt.rho() : R_NaN,
Rcpp::Named("divRho") = (excType=="european") ? opt.dividendRho() : R_NaN);
return rl;
}
// dumped core when we tried last
// no longer under 0.3.10 and g++ 4.0.1 (Aug 2005)
// [[Rcpp::export]]
double binaryOptionImpliedVolatilityEngine(std::string type,
double value,
double underlying,
double strike,
double dividendYield,
double riskFreeRate,
double maturity,
double volatility,
double cashPayoff) {
int length = int(maturity*360 + 0.5); // FIXME: this could be better
QuantLib::Option::Type optionType = getOptionType(type);
// updated again for QuantLib 0.9.0,
// cf QuantLib-0.9.0/test-suite/digitaloption.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::CashOrNothingPayoff(optionType, strike, cashPayoff));
QuantLib::Date exDate = today + length;
boost::shared_ptr<QuantLib::Exercise> exercise(new QuantLib::EuropeanExercise(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)));
//boost::shared_ptr<PricingEngine> engine(new AnalyticEuropeanEngine(stochProcess));
boost::shared_ptr<QuantLib::PricingEngine> engine(new QuantLib::AnalyticBarrierEngine(stochProcess));
QuantLib::VanillaOption opt(payoff, exercise);
opt.setPricingEngine(engine);
return opt.impliedVolatility(value, stochProcess);
}
// [[Rcpp::export]]
Rcpp::List barrierOptionEngine(std::string barrType,
std::string type,
double underlying,
double strike,
double dividendYield,
double riskFreeRate,
double maturity,
double volatility,
double barrier,
double rebate) {
int length = int(maturity*360 + 0.5); // FIXME: this could be better
QuantLib::Barrier::Type barrierType = QuantLib::Barrier::DownIn;
if (barrType=="downin") {
barrierType = QuantLib::Barrier::DownIn;
} else if (barrType=="upin") {
barrierType = QuantLib::Barrier::UpIn;
} else if (barrType=="downout") {
barrierType = QuantLib::Barrier::DownOut;
} else if (barrType=="upout") {
barrierType = QuantLib::Barrier::UpOut;
} else {
throw std::range_error("Unknown barrier type " + type);
}
QuantLib::Option::Type optionType = getOptionType(type);
// new QuantLib 0.3.5 framework, updated for 0.3.7
// updated again for QuantLib 0.9.0,
// cf QuantLib-0.9.0/test-suite/barrieroption.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);
QuantLib::Date exDate = today + length;
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::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)));
// Size timeSteps = 1;
// bool antitheticVariate = false;
// bool controlVariate = false;
// Size requiredSamples = 10000;
// double requiredTolerance = 0.02;
// Size maxSamples = 1000000;
// bool isBiased = false;
boost::shared_ptr<QuantLib::PricingEngine> engine(new QuantLib::AnalyticBarrierEngine(stochProcess));
// need to explicitly reference BarrierOption from QuantLib here
QuantLib::BarrierOption barrierOption(barrierType,
barrier,
rebate,
payoff,
exercise);
barrierOption.setPricingEngine(engine);
Rcpp::List rl = Rcpp::List::create(Rcpp::Named("value") = barrierOption.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);
return rl;
}