Skip to content

Commit 2f08bf3

Browse files
committed
Expanded interface for EuropeanOptionImpliedVolatility
1 parent c17640b commit 2f08bf3

File tree

8 files changed

+184
-48
lines changed

8 files changed

+184
-48
lines changed

ChangeLog

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,26 @@
11
2026-02-27 Dirk Eddelbuettel <edd@debian.org>
22

3+
* src/implieds.cpp (europeanOptionImpliedVolatilityEngine): More
4+
careful about use of evaluation date, new parameter daycounter
5+
(europeanOptionImpliedVolatilityEngineByDate): New (experimental)
6+
variant taking explicit expiry date as argument, also daycounter
7+
* R/implied.R (EuropeanOptionImpliedVolatility)
8+
(EuropeanOptionImpliedVolatility.default): Expose new interface
9+
* man/EuropeanOptionImpliedVolatility.Rd: Updated
10+
* inst/include/RQuantLib_RcppExports.h: Regenerated
11+
* src/RcppExports.cpp: Idem
12+
* R/RcppExports.R: Idem
13+
* NAMESPACE: Added two omitted 'ImpliedVolatility' S3 exports
14+
15+
2026-02-26 Dirk Eddelbuettel <edd@debian.org>
16+
317
* src/implieds.cpp (americanOptionImpliedVolatilityEngine): More
418
careful about use of evaluation date, new parameter daycounter
519
(americanOptionImpliedVolatilityEngineByDate): New (experimental)
620
variant taking explicit expiry date as argument, also daycounter
721
* R/implied.R (AmericanOptionImpliedVolatility)
822
(AmericanOptionImpliedVolatility.default): Expose new interface
23+
* man/AmericanOptionImpliedVolatility.Rd: Updated
924
* inst/include/RQuantLib_RcppExports.h: Regenerated
1025
* src/RcppExports.cpp: Idem
1126
* R/RcppExports.R: Idem

R/RcppExports.R

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,12 @@ calibrateHullWhiteUsingSwapsEngine <- function(termStrcDateVec, termStrcZeroVec,
185185
.Call(`_RQuantLib_calibrateHullWhiteUsingSwapsEngine`, termStrcDateVec, termStrcZeroVec, swapDF, iborDateVec, iborZeroVec, iborType, evalDate)
186186
}
187187

188-
europeanOptionImpliedVolatilityEngine <- function(type, value, underlying, strike, dividendYield, riskFreeRate, maturity, volatility) {
189-
.Call(`_RQuantLib_europeanOptionImpliedVolatilityEngine`, type, value, underlying, strike, dividendYield, riskFreeRate, maturity, volatility)
188+
europeanOptionImpliedVolatilityEngine <- function(type, value, underlying, strike, dividendYield, riskFreeRate, maturity, volatility, dayCounter) {
189+
.Call(`_RQuantLib_europeanOptionImpliedVolatilityEngine`, type, value, underlying, strike, dividendYield, riskFreeRate, maturity, volatility, dayCounter)
190+
}
191+
192+
europeanOptionImpliedVolatilityEngineByDate <- function(type, value, underlying, strike, dividendYield, riskFreeRate, exDate, volatility, dayCounter) {
193+
.Call(`_RQuantLib_europeanOptionImpliedVolatilityEngineByDate`, type, value, underlying, strike, dividendYield, riskFreeRate, exDate, volatility, dayCounter)
190194
}
191195

192196
americanOptionImpliedVolatilityEngine <- function(type, value, underlying, strike, dividendYield, riskFreeRate, maturity, volguess, timesteps, gridpoints, dayCounter) {

R/implied.R

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## RQuantLib -- R interface to the QuantLib libraries
22
##
3-
## Copyright (C) 2002 - 2014 Dirk Eddelbuettel <edd@debian.org>
3+
## Copyright (C) 2002 - 2026 Dirk Eddelbuettel <edd@debian.org>
44
##
55
## This file is part of RQuantLib.
66
##
@@ -22,18 +22,24 @@
2222
EuropeanOptionImpliedVolatility <- function(type, value, underlying,
2323
strike, dividendYield,
2424
riskFreeRate, maturity,
25-
volatility) {
25+
volatility, dayCounter=0) {
2626
UseMethod("EuropeanOptionImpliedVolatility")
2727
}
2828

2929
EuropeanOptionImpliedVolatility.default <- function(type, value, underlying,
3030
strike, dividendYield,
3131
riskFreeRate, maturity,
32-
volatility) {
32+
volatility, dayCounter=0) {
3333

34-
val <- europeanOptionImpliedVolatilityEngine(type, value, underlying, strike,
35-
dividendYield, riskFreeRate,
36-
maturity, volatility)
34+
if (inherits(maturity, "Date")) {
35+
val <- europeanOptionImpliedVolatilityEngineByDate(type, value, underlying, strike,
36+
dividendYield, riskFreeRate,
37+
maturity, volatility, dayCounter)
38+
} else {
39+
val <- europeanOptionImpliedVolatilityEngine(type, value, underlying, strike,
40+
dividendYield, riskFreeRate,
41+
maturity, volatility, dayCounter)
42+
}
3743
class(val) <- c("EuropeanOptionImpliedVolatility","ImpliedVolatility")
3844
val
3945
}
@@ -88,7 +94,7 @@ print.ImpliedVolatility <- function(x, digits=3, ...) {
8894
summary.ImpliedVolatility <- function(object, digits=3, ...) {
8995
impvol <- object[[1]]
9096
cat("Implied Volatility for", class(object)[1], "is", round(impvol, digits), "\n")
91-
cat("with parameters\n")
92-
print(unlist(object[[2]]))
97+
#cat("with parameters\n")
98+
#print(unlist(object[[2]]))
9399
invisible(object)
94100
}

inst/include/RQuantLib_RcppExports.h

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -486,17 +486,38 @@ namespace RQuantLib {
486486
return Rcpp::as<bool >(rcpp_result_gen);
487487
}
488488

489-
inline double europeanOptionImpliedVolatilityEngine(std::string type, double value, double underlying, double strike, double dividendYield, double riskFreeRate, double maturity, double volatility) {
490-
typedef SEXP(*Ptr_europeanOptionImpliedVolatilityEngine)(SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP);
489+
inline double europeanOptionImpliedVolatilityEngine(std::string type, double value, double underlying, double strike, double dividendYield, double riskFreeRate, double maturity, double volatility, int dayCounter) {
490+
typedef SEXP(*Ptr_europeanOptionImpliedVolatilityEngine)(SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP);
491491
static Ptr_europeanOptionImpliedVolatilityEngine p_europeanOptionImpliedVolatilityEngine = NULL;
492492
if (p_europeanOptionImpliedVolatilityEngine == NULL) {
493-
validateSignature("double(*europeanOptionImpliedVolatilityEngine)(std::string,double,double,double,double,double,double,double)");
493+
validateSignature("double(*europeanOptionImpliedVolatilityEngine)(std::string,double,double,double,double,double,double,double,int)");
494494
p_europeanOptionImpliedVolatilityEngine = (Ptr_europeanOptionImpliedVolatilityEngine)R_GetCCallable("RQuantLib", "_RQuantLib_europeanOptionImpliedVolatilityEngine");
495495
}
496496
RObject rcpp_result_gen;
497497
{
498498
RNGScope RCPP_rngScope_gen;
499-
rcpp_result_gen = p_europeanOptionImpliedVolatilityEngine(Shield<SEXP>(Rcpp::wrap(type)), Shield<SEXP>(Rcpp::wrap(value)), Shield<SEXP>(Rcpp::wrap(underlying)), Shield<SEXP>(Rcpp::wrap(strike)), Shield<SEXP>(Rcpp::wrap(dividendYield)), Shield<SEXP>(Rcpp::wrap(riskFreeRate)), Shield<SEXP>(Rcpp::wrap(maturity)), Shield<SEXP>(Rcpp::wrap(volatility)));
499+
rcpp_result_gen = p_europeanOptionImpliedVolatilityEngine(Shield<SEXP>(Rcpp::wrap(type)), Shield<SEXP>(Rcpp::wrap(value)), Shield<SEXP>(Rcpp::wrap(underlying)), Shield<SEXP>(Rcpp::wrap(strike)), Shield<SEXP>(Rcpp::wrap(dividendYield)), Shield<SEXP>(Rcpp::wrap(riskFreeRate)), Shield<SEXP>(Rcpp::wrap(maturity)), Shield<SEXP>(Rcpp::wrap(volatility)), Shield<SEXP>(Rcpp::wrap(dayCounter)));
500+
}
501+
if (rcpp_result_gen.inherits("interrupted-error"))
502+
throw Rcpp::internal::InterruptedException();
503+
if (Rcpp::internal::isLongjumpSentinel(rcpp_result_gen))
504+
throw Rcpp::LongjumpException(rcpp_result_gen);
505+
if (rcpp_result_gen.inherits("try-error"))
506+
throw Rcpp::exception(Rcpp::as<std::string>(rcpp_result_gen).c_str());
507+
return Rcpp::as<double >(rcpp_result_gen);
508+
}
509+
510+
inline double europeanOptionImpliedVolatilityEngineByDate(std::string type, double value, double underlying, double strike, double dividendYield, double riskFreeRate, QuantLib::Date exDate, double volatility, int dayCounter) {
511+
typedef SEXP(*Ptr_europeanOptionImpliedVolatilityEngineByDate)(SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP);
512+
static Ptr_europeanOptionImpliedVolatilityEngineByDate p_europeanOptionImpliedVolatilityEngineByDate = NULL;
513+
if (p_europeanOptionImpliedVolatilityEngineByDate == NULL) {
514+
validateSignature("double(*europeanOptionImpliedVolatilityEngineByDate)(std::string,double,double,double,double,double,QuantLib::Date,double,int)");
515+
p_europeanOptionImpliedVolatilityEngineByDate = (Ptr_europeanOptionImpliedVolatilityEngineByDate)R_GetCCallable("RQuantLib", "_RQuantLib_europeanOptionImpliedVolatilityEngineByDate");
516+
}
517+
RObject rcpp_result_gen;
518+
{
519+
RNGScope RCPP_rngScope_gen;
520+
rcpp_result_gen = p_europeanOptionImpliedVolatilityEngineByDate(Shield<SEXP>(Rcpp::wrap(type)), Shield<SEXP>(Rcpp::wrap(value)), Shield<SEXP>(Rcpp::wrap(underlying)), Shield<SEXP>(Rcpp::wrap(strike)), Shield<SEXP>(Rcpp::wrap(dividendYield)), Shield<SEXP>(Rcpp::wrap(riskFreeRate)), Shield<SEXP>(Rcpp::wrap(exDate)), Shield<SEXP>(Rcpp::wrap(volatility)), Shield<SEXP>(Rcpp::wrap(dayCounter)));
500521
}
501522
if (rcpp_result_gen.inherits("interrupted-error"))
502523
throw Rcpp::internal::InterruptedException();

man/AmericanOptionImpliedVolatility.Rd

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
\usage{
1010
\method{AmericanOptionImpliedVolatility}{default}(type, value,
1111
underlying, strike,dividendYield, riskFreeRate, maturity, volatility,
12-
timeSteps=150, gridPoints=151)
12+
timeSteps=150, gridPoints=151, dayCounter=0)
1313
}
1414
\arguments{
1515
\item{type}{A string with one of the values \code{call} or \code{put}}
@@ -18,14 +18,16 @@
1818
\item{strike}{Strike price of the option}
1919
\item{dividendYield}{Continuous dividend yield (as a fraction) of the stock}
2020
\item{riskFreeRate}{Risk-free rate}
21-
\item{maturity}{Time to maturity (in fractional years)}
21+
\item{maturity}{Time to maturity in either fractional years or as a
22+
\sQuote{Date} object specifying the expiry date)}
2223
\item{volatility}{Initial guess for the volatility of the underlying
2324
stock}
2425
\item{timeSteps}{Time steps for the Finite Differences method, default
2526
value is 150}
2627
\item{gridPoints}{Grid points for the Finite Differences method,
2728
default value is 151}
28-
29+
\item{dayCounter}{Integer denoting the selected QuantLib day counter
30+
convention, default value is 0 for \sQuote{Actual360}}
2931
}
3032
\value{
3133
The \code{AmericanOptionImpliedVolatility} function returns an numeric
@@ -37,7 +39,7 @@
3739

3840
Please see any decent Finance textbook for background reading, and the
3941
\code{QuantLib} documentation for details on the \code{QuantLib}
40-
implementation.
42+
implementation.
4143
}
4244
\references{\url{https://www.quantlib.org/} for details on \code{QuantLib}.}
4345
\author{Dirk Eddelbuettel \email{edd@debian.org} for the \R interface;
@@ -49,7 +51,6 @@
4951
\examples{
5052
AmericanOptionImpliedVolatility(type="call", value=11.10, underlying=100,
5153
strike=100, dividendYield=0.01, riskFreeRate=0.03,
52-
maturity=0.5, volatility=0.4)
54+
maturity=0.5, volatility=0.4, dayCounter=1)
5355
}
5456
\keyword{misc}
55-

man/EuropeanOptionImpliedVolatility.Rd

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
the other required parameters to value an option.}
99
\usage{
1010
\method{EuropeanOptionImpliedVolatility}{default}(type, value,
11-
underlying, strike, dividendYield, riskFreeRate, maturity, volatility)
11+
underlying, strike, dividendYield, riskFreeRate, maturity, volatility, dayCounter=0)
1212
}
1313
\arguments{
1414
\item{type}{A string with one of the values \code{call} or \code{put}}
@@ -17,8 +17,11 @@
1717
\item{strike}{Strike price of the option}
1818
\item{dividendYield}{Continuous dividend yield (as a fraction) of the stock}
1919
\item{riskFreeRate}{Risk-free rate}
20-
\item{maturity}{Time to maturity (in fractional years)}
20+
\item{maturity}{Time to maturity in either fractional years or as a
21+
\sQuote{Date} object specifying the expiry date)}
2122
\item{volatility}{Initial guess for the volatility of the underlying stock}
23+
\item{dayCounter}{Integer denoting the selected QuantLib day counter
24+
convention, default value is 0 for \sQuote{Actual360}}
2225
}
2326
\value{
2427
The \code{EuropeanOptionImpliedVolatility} function returns an numeric
@@ -31,7 +34,7 @@
3134

3235
Please see any decent Finance textbook for background reading, and the
3336
\code{QuantLib} documentation for details on the \code{QuantLib}
34-
implementation.
37+
implementation.
3538
}
3639
\references{\url{https://www.quantlib.org/} for details on \code{QuantLib}.}
3740
\author{Dirk Eddelbuettel \email{edd@debian.org} for the \R interface;
@@ -42,8 +45,6 @@
4245
\examples{
4346
EuropeanOptionImpliedVolatility(type="call", value=11.10, underlying=100,
4447
strike=100, dividendYield=0.01, riskFreeRate=0.03,
45-
maturity=0.5, volatility=0.4)
48+
maturity=0.5, volatility=0.4, dayCounter=1)
4649
}
4750
\keyword{misc}
48-
49-

0 commit comments

Comments
 (0)