[Rquantlib-commits] r263 - in pkg/RQuantLib: . R inst src

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Jun 21 05:24:55 CEST 2010


Author: edd
Date: 2010-06-21 05:24:55 +0200 (Mon, 21 Jun 2010)
New Revision: 263

Modified:
   pkg/RQuantLib/NAMESPACE
   pkg/RQuantLib/R/calendars.R
   pkg/RQuantLib/R/zzz.R
   pkg/RQuantLib/inst/ChangeLog
   pkg/RQuantLib/src/bermudan.cpp
   pkg/RQuantLib/src/calendars.cpp
   pkg/RQuantLib/src/discount.cpp
   pkg/RQuantLib/src/utils.cpp
Log:
calendar setter for RQLContext
plus documentation and use


Modified: pkg/RQuantLib/NAMESPACE
===================================================================
--- pkg/RQuantLib/NAMESPACE	2010-06-20 14:18:47 UTC (rev 262)
+++ pkg/RQuantLib/NAMESPACE	2010-06-21 03:24:55 UTC (rev 263)
@@ -37,6 +37,7 @@
        "advance",
        "businessDaysBetween",
        "holidayList",
+       "setCalendarContext",
        ##--dayCounter.R
        "dayCount",
        "yearFraction",

Modified: pkg/RQuantLib/R/calendars.R
===================================================================
--- pkg/RQuantLib/R/calendars.R	2010-06-20 14:18:47 UTC (rev 262)
+++ pkg/RQuantLib/R/calendars.R	2010-06-21 03:24:55 UTC (rev 263)
@@ -22,86 +22,86 @@
 ## MA 02111-1307, USA
 
 businessDay <- function(calendar="TARGET", dates=Sys.Date()) {
-  stopifnot(is.character(calendar))
-  stopifnot(class(dates)=="Date")
-  val <- .Call("QL_isBusinessDay", calendar, dates, PACKAGE="RQuantLib")
-  val <- as.logical(val)
-  names(val) <- dates
-  val
+    stopifnot(is.character(calendar))
+    stopifnot(class(dates)=="Date")
+    val <- .Call("QL_isBusinessDay", calendar, dates, PACKAGE="RQuantLib")
+    val <- as.logical(val)
+    names(val) <- dates
+    val
 }
 
 isHoliday <- function(calendar="TARGET", dates=Sys.Date()) {
-  stopifnot(is.character(calendar))
-  stopifnot(class(dates)=="Date")
-  val <- .Call("QL_isHoliday", calendar, dates, PACKAGE="RQuantLib")
-  val <- as.logical(val)
-  names(val) <- dates
-  val
+    stopifnot(is.character(calendar))
+    stopifnot(class(dates)=="Date")
+    val <- .Call("QL_isHoliday", calendar, dates, PACKAGE="RQuantLib")
+    val <- as.logical(val)
+    names(val) <- dates
+    val
 }
 
 isWeekend <- function(calendar="TARGET", dates=Sys.Date()) {
-  stopifnot(is.character(calendar))
-  stopifnot(class(dates)=="Date")
-  val <- .Call("QL_isWeekend", calendar, dates, PACKAGE="RQuantLib")
-  val <- as.logical(val)
-  names(val) <- dates
-  val
+    stopifnot(is.character(calendar))
+    stopifnot(class(dates)=="Date")
+    val <- .Call("QL_isWeekend", calendar, dates, PACKAGE="RQuantLib")
+    val <- as.logical(val)
+    names(val) <- dates
+    val
 }
 
 isEndOfMonth <- function(calendar="TARGET", dates=Sys.Date()) {
-  stopifnot(is.character(calendar))
-  stopifnot(class(dates)=="Date")
-  val <- .Call("QL_isEndOfMonth", calendar, dates, PACKAGE="RQuantLib")
-  val <- as.logical(val)
-  names(val) <- dates
-  val
+    stopifnot(is.character(calendar))
+    stopifnot(class(dates)=="Date")
+    val <- .Call("QL_isEndOfMonth", calendar, dates, PACKAGE="RQuantLib")
+    val <- as.logical(val)
+    names(val) <- dates
+    val
 }
 
 endOfMonth <- function(calendar="TARGET", dates=Sys.Date()) {
-  stopifnot(is.character(calendar))
-  stopifnot(class(dates)=="Date")
-  val <- .Call("QL_endOfMonth", calendar, dates, PACKAGE="RQuantLib")
-  names(val) <- dates
-  val
+    stopifnot(is.character(calendar))
+    stopifnot(class(dates)=="Date")
+    val <- .Call("QL_endOfMonth", calendar, dates, PACKAGE="RQuantLib")
+    names(val) <- dates
+    val
 }
 
 adjust <- function(calendar="TARGET", dates=Sys.Date(), bdc = 0 ) {
-  stopifnot(is.character(calendar))
-  stopifnot(class(dates)=="Date")
-  val <- .Call("QL_adjust", calendar, as.double(bdc), dates, PACKAGE="RQuantLib")
-  names(val) <- dates
-  val
+    stopifnot(is.character(calendar))
+    stopifnot(class(dates)=="Date")
+    val <- .Call("QL_adjust", calendar, as.double(bdc), dates, PACKAGE="RQuantLib")
+    names(val) <- dates
+    val
 }
 
 advance <- function(calendar="TARGET", dates=Sys.Date(),
                     n, timeUnit, # call 1
                     period,      # call 2
                     bdc = 0, emr = 0) {
-  stopifnot(is.character(calendar))
-  stopifnot(class(dates)=="Date")
-  call1 <- missing(period) & !missing(n) & !missing(timeUnit)  ## or was it && ?
-  call2 <- !missing(period) & missing(n) & missing(timeUnit)
-  stopifnot(call1 | call2)
-  val <- NULL
-  if (call1)
-    val <- .Call("QL_advance1",
-                 calendar,
-                 list(amount = as.double(n),
-                      unit = as.double(timeUnit),
-                      bdc = as.double(bdc),
-                      emr = as.double(emr)),
-                 dates,
-                 PACKAGE="RQuantLib")
-  if (call2)
-    val <- .Call("QL_advance2",
-                 calendar,
-                 list(period = as.double(period),
-                      bdc = as.double(bdc),
-                      emr = as.double(emr)),
-                 dates,
-                 PACKAGE="RQuantLib")
-  stopifnot( !is.null(val) )
-  val
+    stopifnot(is.character(calendar))
+    stopifnot(class(dates)=="Date")
+    call1 <- missing(period) & !missing(n) & !missing(timeUnit)  ## or was it && ?
+    call2 <- !missing(period) & missing(n) & missing(timeUnit)
+    stopifnot(call1 | call2)
+    val <- NULL
+    if (call1)
+        val <- .Call("QL_advance1",
+                     calendar,
+                     list(amount = as.double(n),
+                          unit = as.double(timeUnit),
+                          bdc = as.double(bdc),
+                          emr = as.double(emr)),
+                     dates,
+                     PACKAGE="RQuantLib")
+    if (call2)
+        val <- .Call("QL_advance2",
+                     calendar,
+                     list(period = as.double(period),
+                          bdc = as.double(bdc),
+                          emr = as.double(emr)),
+                     dates,
+                     PACKAGE="RQuantLib")
+    stopifnot( !is.null(val) )
+    val
 }
 
 businessDaysBetween  <- function(calendar="TARGET",
@@ -110,29 +110,39 @@
                                  includeFirst = 1,
                                  includeLast = 0
                                  ) {
-  stopifnot(is.character(calendar))
-  stopifnot(class(from)=="Date")
-  stopifnot(class(to)=="Date")
-  val <- .Call("QL_businessDaysBetween",
-               calendar,
-               list(includeFirst = as.double(includeFirst),
-                    includeLast = as.double(includeLast)),
-               from, to,
-               PACKAGE="RQuantLib")
-  val <- val
-  val
+    stopifnot(is.character(calendar))
+    stopifnot(class(from)=="Date")
+    stopifnot(class(to)=="Date")
+    val <- .Call("QL_businessDaysBetween",
+                 calendar,
+                 list(includeFirst = as.double(includeFirst),
+                      includeLast = as.double(includeLast)),
+                 from, to,
+                 PACKAGE="RQuantLib")
+    val <- val
+    val
 }
 
 holidayList <- function(calendar="TARGET",
                         from=Sys.Date(),
                         to=Sys.Date() + 5,
                         includeWeekends=0) {
-  stopifnot(is.character(calendar))
-  stopifnot(class(from)=="Date")
-  stopifnot(class(to)=="Date")
-  val <- .Call("QL_holidayList",
-               calendar,
-               list(includeWeekends=as.double(includeWeekends), from=from, to=to),
-               PACKAGE="RQuantLib")
-  val
+    stopifnot(is.character(calendar))
+    stopifnot(class(from)=="Date")
+    stopifnot(class(to)=="Date")
+    val <- .Call("QL_holidayList",
+                 calendar,
+                 list(includeWeekends=as.double(includeWeekends), from=from, to=to),
+                 PACKAGE="RQuantLib")
+    val
 }
+
+setCalendarContext <- function(calendar="TARGET",
+                               fixingDays = 2,
+                               settleDate = Sys.Date() + 2) {
+    val <- .Call("QL_setContext",
+                 list(calendar = calendar,
+                      fixingDays = fixingDays,
+                      settleDate = settleDate),
+                 PACKAGE="RQuantLib")
+}

Modified: pkg/RQuantLib/R/zzz.R
===================================================================
--- pkg/RQuantLib/R/zzz.R	2010-06-20 14:18:47 UTC (rev 262)
+++ pkg/RQuantLib/R/zzz.R	2010-06-21 03:24:55 UTC (rev 263)
@@ -1,6 +1,6 @@
 ## RQuantLib -- R interface to the QuantLib libraries
 ##
-## Copyright (C) 2002 - 2009 Dirk Eddelbuettel <edd at debian.org>
+## Copyright (C) 2002 - 2010  Dirk Eddelbuettel <edd at debian.org>
 ##
 ## $Id: zzz.R,v 1.3 2005/10/05 03:03:54 edd Exp $
 ##
@@ -20,7 +20,7 @@
 ## Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 ## MA 02111-1307, USA
 
-.First.lib <- function(lib, pkg) {
-  #cat("This is a pre-release. The interface might change...\n")
-  library.dynam("RQuantLib", pkg, lib )
-}
+#.First.lib <- function(lib, pkg) {
+#  #cat("This is a pre-release. The interface might change...\n")
+#  library.dynam("RQuantLib", pkg, lib )
+#}

Modified: pkg/RQuantLib/inst/ChangeLog
===================================================================
--- pkg/RQuantLib/inst/ChangeLog	2010-06-20 14:18:47 UTC (rev 262)
+++ pkg/RQuantLib/inst/ChangeLog	2010-06-21 03:24:55 UTC (rev 263)
@@ -1,5 +1,14 @@
 2010-06-20  Dirk Eddelbuettel  <edd at debian.org>
 
+	* R/calendar.R: New helper function setCalendarContext()
+	setting calendar, fixingDays and settleDate
+	* src/calendar.cpp: Implementation, setting RQLContext
+	* R/setCalendarContext.Rd: Documentation
+
+	* src/bermudan.cpp: take calendar info from RQLContext
+	* src/discount.cpp: idem
+	* src/utils.cpp: idem
+
 	* src/*.cpp: Some minor cleanup and reindentation,
 	  ensure Settings::instance().evaluationDate() is set
 

Modified: pkg/RQuantLib/src/bermudan.cpp
===================================================================
--- pkg/RQuantLib/src/bermudan.cpp	2010-06-20 14:18:47 UTC (rev 262)
+++ pkg/RQuantLib/src/bermudan.cpp	2010-06-21 03:24:55 UTC (rev 263)
@@ -80,10 +80,9 @@
             interpHow  = Rcpp::as<std::string>(rparam["interpHow"]);
         }
 
-        Calendar calendar = TARGET();
-        Integer fixingDays = 2;
-        RQLContext::instance().calendar = calendar;
-        RQLContext::instance().fixingDays = fixingDays;
+        // initialise from the singleton instance
+        Calendar calendar = RQLContext::instance().calendar;
+        //Integer fixingDays = RQLContext::instance().fixingDays;
 
         // Any DayCounter would be fine.
         // ActualActual::ISDA ensures that 30 years is 30.0

Modified: pkg/RQuantLib/src/calendars.cpp
===================================================================
--- pkg/RQuantLib/src/calendars.cpp	2010-06-20 14:18:47 UTC (rev 262)
+++ pkg/RQuantLib/src/calendars.cpp	2010-06-21 03:24:55 UTC (rev 263)
@@ -85,6 +85,28 @@
     return pcal;
 }
 
+RcppExport SEXP QL_setContext(SEXP parSEXP) {
+
+    try {
+        Rcpp::List par(parSEXP);        
+
+        // set fixingDays and settleDate
+        RQLContext::instance().fixingDays = Rcpp::as<int>(par["fixingDays"]);
+        RQLContext::instance().settleDate = 
+            Date(dateFromR( RcppDate(Rcpp::as<int>(par["settleDate"])) ));
+
+        boost::shared_ptr<Calendar> pcal( getCalendar(Rcpp::as<std::string>(par["calendar"])) );
+        RQLContext::instance().calendar = *pcal; // set calendar in global singleton
+
+
+    } catch(std::exception &ex) { 
+        forward_exception_to_r(ex); 
+    } catch(...) { 
+        ::Rf_error("c++ exception (unknown reason)"); 
+    }
+    return R_NilValue;
+}
+
 RcppExport SEXP QL_isBusinessDay(SEXP calSexp, SEXP dateSexp){
 
     try {

Modified: pkg/RQuantLib/src/discount.cpp
===================================================================
--- pkg/RQuantLib/src/discount.cpp	2010-06-20 14:18:47 UTC (rev 262)
+++ pkg/RQuantLib/src/discount.cpp	2010-06-21 03:24:55 UTC (rev 263)
@@ -50,10 +50,9 @@
             flatQuotes = false;
         }
 
-        Calendar calendar = TARGET();
-        RQLContext::instance().calendar = calendar;
-        Integer fixingDays = 2;
-        RQLContext::instance().fixingDays = fixingDays;
+        // initialise from the singleton instance
+        Calendar calendar = RQLContext::instance().calendar;
+        //Integer fixingDays = RQLContext::instance().fixingDays;
 
         // Any DayCounter would be fine.
         // ActualActual::ISDA ensures that 30 years is 30.0

Modified: pkg/RQuantLib/src/utils.cpp
===================================================================
--- pkg/RQuantLib/src/utils.cpp	2010-06-20 14:18:47 UTC (rev 262)
+++ pkg/RQuantLib/src/utils.cpp	2010-06-21 03:24:55 UTC (rev 263)
@@ -132,10 +132,9 @@
             flatQuotes = false;
         }
         
-        Calendar calendar = TARGET();
-        RQLContext::instance().calendar = calendar;
-        Integer fixingDays = 2;
-        RQLContext::instance().fixingDays = fixingDays;
+        // initialise from the singleton instance
+        Calendar calendar = RQLContext::instance().calendar;
+        //Integer fixingDays = RQLContext::instance().fixingDays;
 
         // Any DayCounter would be fine;  ActualActual::ISDA ensures that 30 years is 30.0
         DayCounter termStructureDayCounter = ActualActual(ActualActual::ISDA);



More information about the Rquantlib-commits mailing list