Skip to content

Commit

Permalink
Rename helper to getFutureDate, use for dividend dates too
Browse files Browse the repository at this point in the history
  • Loading branch information
eddelbuettel committed Jul 14, 2024
1 parent 9189b6d commit b17c68f
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 30 deletions.
2 changes: 1 addition & 1 deletion ChangeLog
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
2024-07-14 Dirk Eddelbuettel <[email protected]>

* src/utils.cpp (getExerciseDate): New helper function
* src/utils.cpp (getFutureDate): New helper function
* inst/include/rquantlib_internal.h: Declaration
* src/asian.cpp: Use new helper function
* src/barrier_binary.cpp: Idem
Expand Down
2 changes: 1 addition & 1 deletion inst/include/rquantlib_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,6 @@ QuantLib::Duration::Type getDurationType(const double n);
QuantLib::Date advanceDate(QuantLib::Date issueDate, int days);

// utils.cpp
QuantLib::Date getExerciseDate(const QuantLib::Date today, double maturity);
QuantLib::Date getFutureDate(const QuantLib::Date today, double maturity);

#endif
8 changes: 2 additions & 6 deletions src/asian.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Rcpp::List asianOptionEngine(std::string averageType,
if (averageType=="geometric"){
auto engine = qlext::make_shared<QuantLib::AnalyticContinuousGeometricAveragePriceAsianEngine>(stochProcess);

QuantLib::Date exDate = getExerciseDate(today, maturity);
QuantLib::Date exDate = getFutureDate(today, maturity);

auto exercise = qlext::make_shared<QuantLib::EuropeanExercise>(exDate);
QuantLib::ContinuousAveragingAsianOption option(QuantLib::Average::Geometric, payoff, exercise);
Expand Down Expand Up @@ -103,11 +103,7 @@ Rcpp::List asianOptionEngine(std::string averageType,
fixingDates[0] = today + QuantLib::Integer(timeIncrements[0] * 360 + 0.5);
for (QuantLib::Size i=1; i<fixings; i++) {
timeIncrements[i] = i*dt + first;
#ifdef QL_HIGH_RESOLUTION_DATE
fixingDates[i]= QuantLib::Date(today.dateTime() + boost::posix_time::minutes(boost::uint64_t(timeIncrements[i] * 360 * 24 * 60)));
#else
fixingDates[i] = today + QuantLib::Integer(timeIncrements[i]*360+0.5);
#endif
fixingDates[i] = getFutureDate(today, timeIncrements[i]);
}
QuantLib::Real runningSum = 0.0;
QuantLib::Size pastFixing = 0;
Expand Down
6 changes: 3 additions & 3 deletions src/barrier_binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Rcpp::List binaryOptionEngine(std::string binType,
// cf QuantLib-0.9.0/test-suite/digitaloption.cpp
QuantLib::Date today = QuantLib::Date::todaysDate();
QuantLib::Settings::instance().evaluationDate() = today;
QuantLib::Date exDate = getExerciseDate(today, maturity);
QuantLib::Date exDate = getFutureDate(today, maturity);

QuantLib::DayCounter dc = QuantLib::Actual360();
namespace qlext = QuantLib::ext; // convenience namespace shortcut
Expand Down Expand Up @@ -118,7 +118,7 @@ double binaryOptionImpliedVolatilityEngine(std::string type,
QuantLib::Date today = QuantLib::Date::todaysDate();
QuantLib::Settings::instance().evaluationDate() = today;
QuantLib::DayCounter dc = QuantLib::Actual360();
QuantLib::Date exDate = getExerciseDate(today, maturity);
QuantLib::Date exDate = getFutureDate(today, maturity);
namespace qlext = QuantLib::ext; // convenience namespace shortcut
auto spot = qlext::make_shared<QuantLib::SimpleQuote>(underlying);
auto qRate = qlext::make_shared<QuantLib::SimpleQuote>(dividendYield);
Expand Down Expand Up @@ -177,7 +177,7 @@ Rcpp::List barrierOptionEngine(std::string barrType,
QuantLib::Date today = QuantLib::Date::todaysDate();
QuantLib::Settings::instance().evaluationDate() = today;
QuantLib::DayCounter dc = QuantLib::Actual360();
QuantLib::Date exDate = getExerciseDate(today, maturity);
QuantLib::Date exDate = getFutureDate(today, maturity);
namespace qlext = QuantLib::ext; // convenience namespace shortcut
auto spot = qlext::make_shared<QuantLib::SimpleQuote>(underlying);
auto qRate = qlext::make_shared<QuantLib::SimpleQuote>(dividendYield);
Expand Down
4 changes: 2 additions & 2 deletions src/implieds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ double europeanOptionImpliedVolatilityEngine(std::string type,

QuantLib::Date today = QuantLib::Date::todaysDate();
QuantLib::Settings::instance().evaluationDate() = today;
QuantLib::Date exDate = getExerciseDate(today, maturity);
QuantLib::Date exDate = getFutureDate(today, maturity);

// new framework as per QuantLib 0.3.5
// updated for 0.3.7
Expand Down Expand Up @@ -85,7 +85,7 @@ double americanOptionImpliedVolatilityEngine(std::string type,

QuantLib::Date today = QuantLib::Date::todaysDate();
QuantLib::Settings::instance().evaluationDate() = today;
QuantLib::Date exDate = getExerciseDate(today, maturity);
QuantLib::Date exDate = getFutureDate(today, maturity);

// new framework as per QuantLib 0.3.5
QuantLib::DayCounter dc = QuantLib::Actual360();
Expand Down
4 changes: 2 additions & 2 deletions src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ Rcpp::LogicalVector getQuantLibCapabilities() {
Rcpp::Named("negativeRates") = hasNegativeRates);
}

QuantLib::Date getExerciseDate(const QuantLib::Date today, double maturity) {
QuantLib::Date getFutureDate(const QuantLib::Date today, double maturity) {
// depending on the compile-time option, this is either intra-day or not
#ifdef QL_HIGH_RESOLUTION_DATE

Expand All @@ -701,7 +701,7 @@ QuantLib::Date getExerciseDate(const QuantLib::Date today, double maturity) {

#else

int length = int(maturity*360 + 0.5); // FIXME: this could be better
int length = int(maturity * 360 + 0.5); // FIXME: this could be better
return QuantLib::Date{today + length};

#endif
Expand Down
20 changes: 5 additions & 15 deletions src/vanilla.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Rcpp::List europeanOptionEngine(std::string type,

QuantLib::Option::Type optionType = getOptionType(type);
QuantLib::Date today = QuantLib::Date::todaysDate();
QuantLib::Date exDate = getExerciseDate(today, maturity);
QuantLib::Date exDate = getFutureDate(today, maturity);
QuantLib::Settings::instance().evaluationDate() = today;
QuantLib::DayCounter dc = QuantLib::Actual360();
namespace qlext = QuantLib::ext; // convenience namespace shortcut
Expand All @@ -58,12 +58,7 @@ Rcpp::List europeanOptionEngine(std::string type,
std::vector<QuantLib::Date> discDivDates(n);
std::vector<double> discDividends(n);
for (int i = 0; i < n; i++) {
#ifdef QL_HIGH_RESOLUTION_DATE
boost::posix_time::time_duration discreteDividendLength = boost::posix_time::minutes(boost::uint64_t(divtimes[i] * 360 * 24 * 60));
discDivDates[i] = QuantLib::Date(today.dateTime() + discreteDividendLength);
#else
discDivDates[i] = today + int(divtimes[i] * 360 + 0.5);
#endif
discDivDates[i] = getFutureDate(today, divtimes[i]);
discDividends[i] = divvalues[i];
}

Expand Down Expand Up @@ -118,7 +113,7 @@ Rcpp::List americanOptionEngine(std::string type,

QuantLib::Date today = QuantLib::Date::todaysDate();
QuantLib::Settings::instance().evaluationDate() = today;
QuantLib::Date exDate = getExerciseDate(today, maturity);
QuantLib::Date exDate = getFutureDate(today, maturity);
QuantLib::DayCounter dc = QuantLib::Actual360();
namespace qlext = QuantLib::ext; // convenience namespace shortcut
auto spot = qlext::make_shared<QuantLib::SimpleQuote>(underlying);
Expand Down Expand Up @@ -146,12 +141,7 @@ Rcpp::List americanOptionEngine(std::string type,
std::vector<QuantLib::Date> discDivDates(n);
std::vector<double> discDividends(n);
for (int i = 0; i < n; i++) {
#ifdef QL_HIGH_RESOLUTION_DATE
boost::posix_time::time_duration discreteDividendLength = boost::posix_time::minutes(boost::uint64_t(divtimes[i] * 360 * 24 * 60));
discDivDates[i] = QuantLib::Date(today.dateTime() + discreteDividendLength);
#else
discDivDates[i] = today + int(divtimes[i] * 360 + 0.5);
#endif
discDivDates[i] = getFutureDate(today, divtimes[i]);
discDividends[i] = divvalues[i];
}

Expand Down Expand Up @@ -239,7 +229,7 @@ Rcpp::List europeanOptionArraysEngine(std::string type, Rcpp::NumericMatrix par)
auto rRate = qlext::make_shared<QuantLib::SimpleQuote>(riskFreeRate);
auto rTS = flatRate(today, rRate, dc); // cf src/utils.cpp

QuantLib::Date exDate = getExerciseDate(today, maturity);
QuantLib::Date exDate = getFutureDate(today, maturity);
auto exercise = qlext::make_shared<QuantLib::EuropeanExercise>(exDate);
auto payoff = qlext::make_shared<QuantLib::PlainVanillaPayoff>(optionType, strike);
auto option = makeOption(payoff, exercise, spot, qTS, rTS, volTS);
Expand Down

0 comments on commit b17c68f

Please sign in to comment.