[Rquantlib-commits] r254 - in pkg/RQuantLib: inst src

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Jun 16 18:12:35 CEST 2010


Author: edd
Date: 2010-06-16 18:12:35 +0200 (Wed, 16 Jun 2010)
New Revision: 254

Modified:
   pkg/RQuantLib/inst/ChangeLog
   pkg/RQuantLib/src/asian.cpp
   pkg/RQuantLib/src/rquantlib.hpp
   pkg/RQuantLib/src/utils.cpp
   pkg/RQuantLib/src/vanilla.cpp
Log:
added getOptionType() helper


Modified: pkg/RQuantLib/inst/ChangeLog
===================================================================
--- pkg/RQuantLib/inst/ChangeLog	2010-06-16 15:32:50 UTC (rev 253)
+++ pkg/RQuantLib/inst/ChangeLog	2010-06-16 16:12:35 UTC (rev 254)
@@ -1,5 +1,9 @@
 2010-06-16  Dirk Eddelbuettel  <edd at debian.org>
 
+	* src/utils.cpp: Added simple getOptionType() helper
+	* src/rquantlib.hpp: Added simple getOptionType() helper definition
+	* src/*cpp: Use getOptionType()
+
 	* src/asian.cpp: Converted to new API
 
 2010-06-15  Dirk Eddelbuettel  <edd at debian.org>

Modified: pkg/RQuantLib/src/asian.cpp
===================================================================
--- pkg/RQuantLib/src/asian.cpp	2010-06-16 15:32:50 UTC (rev 253)
+++ pkg/RQuantLib/src/asian.cpp	2010-06-16 16:12:35 UTC (rev 254)
@@ -40,16 +40,7 @@
         int length = int(maturity*360 + 0.5); // FIXME: this could be better
         double volatility = Rcpp::as<double>(rparam["volatility"]);
 
-        Option::Type optionType = Option::Call;
-        if (type=="call") {
-            optionType = Option::Call;
-        }
-        else if (type == "put") {
-            optionType = Option::Put;
-        }
-        else {
-            throw std::range_error("Unknown option " + type);
-        }
+        Option::Type optionType = getOptionType(type);
 
         Average::Type averageType = Average::Geometric;
         if (avgType=="geometric"){

Modified: pkg/RQuantLib/src/rquantlib.hpp
===================================================================
--- pkg/RQuantLib/src/rquantlib.hpp	2010-06-16 15:32:50 UTC (rev 253)
+++ pkg/RQuantLib/src/rquantlib.hpp	2010-06-16 16:12:35 UTC (rev 254)
@@ -30,12 +30,8 @@
 
 using namespace QuantLib;
 
+#include <Rcpp.h>
 
-//#include <R.h>
-//#include <Rinternals.h>
-
-#include "Rcpp.h"
-
 //#define NULL_RateHelper (boost::shared_ptr<RateHelper>)Null<boost::shared_ptr<RateHelper> >()
 
 // Prototypes for convenience functions (some macros)
@@ -162,4 +158,8 @@
                                             const Handle<YieldTermStructure>& iborStrc);
 Calendar* getCalendar(SEXP calParameters);
 Period periodByTimeUnit(int length, std::string unit);
+
+// simple option type creator based on string
+Option::Type getOptionType(const std::string &t);
+
 #endif

Modified: pkg/RQuantLib/src/utils.cpp
===================================================================
--- pkg/RQuantLib/src/utils.cpp	2010-06-16 15:32:50 UTC (rev 253)
+++ pkg/RQuantLib/src/utils.cpp	2010-06-16 16:12:35 UTC (rev 254)
@@ -24,8 +24,20 @@
 // Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 // MA 02111-1307, USA
 
-#include "rquantlib.hpp"
+#include <rquantlib.hpp>
 
+Option::Type getOptionType(const std::string &type) {
+    Option::Type optionType;
+    if (type=="call") {
+        optionType = Option::Call;
+    } else if (type=="put") {
+        optionType = Option::Put;
+    } else {
+        throw std::range_error("Unknown option " + type);
+    }
+    return optionType;
+}
+
 // cf QuantLib-0.9.0/test-suite/europeanoption.cpp
 boost::shared_ptr<VanillaOption>
 makeOption(const boost::shared_ptr<StrikedTypePayoff>& payoff,

Modified: pkg/RQuantLib/src/vanilla.cpp
===================================================================
--- pkg/RQuantLib/src/vanilla.cpp	2010-06-16 15:32:50 UTC (rev 253)
+++ pkg/RQuantLib/src/vanilla.cpp	2010-06-16 16:12:35 UTC (rev 254)
@@ -39,14 +39,7 @@
         int length = int(maturity*360 + 0.5); // FIXME: this could be better
         double volatility = Rcpp::as<double>(rparam["volatility"]);
     
-        Option::Type optionType=Option::Call;
-        if (type=="call") {
-            optionType = Option::Call;
-        } else if (type=="put") {
-            optionType = Option::Put;
-        } else {
-            throw std::range_error("Unknown option " + type);
-        }
+        Option::Type optionType = getOptionType(type);
 
         Date today = Date::todaysDate();
 
@@ -99,14 +92,7 @@
         int length = int(maturity*360 + 0.5); // FIXME: this could be better
         double volatility = Rcpp::as<double>(rparam["volatility"]);
         
-        Option::Type optionType = Option::Call;
-        if (type=="call") {
-            optionType = Option::Call;
-        } else if (type=="put") {
-            optionType = Option::Put;
-        } else {
-            throw std::range_error("Unknown option " + type);
-        }
+        Option::Type optionType = getOptionType(type);
 
         // new framework as per QuantLib 0.3.5, updated for 0.3.7
         // updated again for 0.9.0, see eg test-suite/americanoption.cpp



More information about the Rquantlib-commits mailing list