eddelbuettel / rquantlib

R interface to the QuantLib library
119 stars 50 forks source link

businessDaysBetween errors if either Date is stored as INTSXP #98

Closed joshuaulrich closed 6 years ago

joshuaulrich commented 6 years ago

businessDaysBetween() expects from and to to be Dates represented using REALSXP. There is an error if the Date happens to be stored as an INTSXP. The Details section of ?Date says:

It is intended that the date should be an integer, but this is not enforced in the internal representation.

It's not clear if "integer" means "INTSXP" or "whole number" in that context, but it's possible to have a Date that is an INTSXP (this case is also handled in xts).

Reproducible example below, with apologies that it also requires Rblpapi...

R> expiry <- Rblpapi::bdp("TYV7P 124.5 Comdty", "OPT_EXPIRE_DT")[1,1]
R> storage.mode(expiry)
# [1] "integer"
R> RQuantLib::businessDaysBetween("UnitedStates", expiry, Sys.Date())
Error in RQuantLib::businessDaysBetween("UnitedStates", Rblpapi::bdp("TYV7P 124.5 Comdty",  :
  REAL() can only be applied to a 'numeric', not a 'integer'

R> sessionInfo()
R version 3.4.2 (2017-09-28)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods
[7] base

other attached packages:
[1] foreach_1.4.3   RQuantLib_0.4.2 Rblpapi_0.3.6   quantmod_0.4-11
[5] TTR_0.23-2      xts_0.10-1      zoo_1.8-0

loaded via a namespace (and not attached):
[1] compiler_3.4.2   tools_3.4.2      curl_3.0
[4] Rcpp_0.12.13     codetools_0.2-15 grid_3.4.2
[7] iterators_1.0.8  greeks_0.5-4     lattice_0.20-35
eddelbuettel commented 6 years ago
R> expiry <- Rblpapi::bdp("TYV7P 124.5 Comdty", "OPT_EXPIRE_DT")[1,1]
R> expiry
[1] "2017-09-22"
R> class(expiry)
[1] "Date"
R> library(RQuantLib)
R> RQuantLib::businessDaysBetween("UnitedStates", expiry, Sys.Date())
[1] 64
R> 
R> packageVersion("Rblpapi")
[1] ‘0.3.6.2’
R>  packageVersion("RQuantLib")
[1] ‘0.4.4’
R> 
eddelbuettel commented 6 years ago

Can we close this?

joshuaulrich commented 6 years ago

Closing, since this is not reproducible using RQuantLib 0.4.4 (QuantLib-1.11) on Windows. Sorry for the noise.

eddelbuettel commented 6 years ago

No apologies needed. Thank you for actually getting 0.4.4 onto Windows.