[Blotter-commits] r781 - in pkg/FinancialInstrument: R man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Sep 17 15:55:00 CEST 2011


Author: gsee
Date: 2011-09-17 15:55:00 +0200 (Sat, 17 Sep 2011)
New Revision: 781

Modified:
   pkg/FinancialInstrument/R/instrument.R
   pkg/FinancialInstrument/R/load.instruments.R
   pkg/FinancialInstrument/man/instrument.auto.Rd
Log:
 - add default_type and multiplier formal args to instrument.auto
 - tweaks to load.instruments to make it easier to use a data.frame (like that produced by stockSymbols())


Modified: pkg/FinancialInstrument/R/instrument.R
===================================================================
--- pkg/FinancialInstrument/R/instrument.R	2011-09-16 15:07:53 UTC (rev 780)
+++ pkg/FinancialInstrument/R/instrument.R	2011-09-17 13:55:00 UTC (rev 781)
@@ -625,7 +625,9 @@
 #' \dQuote{USD} will be defined and used to create the instrument.
 #' @param primary_id charater primary identifier of instrument to be created
 #' @param currency character name of currency that instrument will be denominated it. Default=\dQuote{USD}
+#' @param multiplier numeric product multiplier
 #' @param silent TRUE/FALSE. silence warnings?
+#' @param default_type What type of instrument to make if it is not clear from the primary_id. ("stock", "future", etc.)
 #' @param ... other passthrough parameters
 #' @return Primarily called for its side-effect, but will return the name of the instrument that was created
 #' @note This is not intended to be used to create instruments of type \code{stock}, \code{future}, \code{option},
@@ -650,7 +652,7 @@
 #' getInstrument("VX_H11") #made a future_series
 #' }
 #' @export
-instrument.auto <- function(primary_id, currency='USD', silent=FALSE, ...) {
+instrument.auto <- function(primary_id, currency='USD', multiplier=1, silent=FALSE, default_type='NULL', ...) {
 ##TODO: check formals against dots and remove duplicates from dots before calling constructors to avoid
 # 'formal argument "multiplier" matched by multiple actual arguments'
     if (!is.currency(currency)) {
@@ -674,9 +676,9 @@
         if (is.instrument(root)) {
             return(future_series(primary_id,defined.by='auto',...))
         } else if (!silent) {
-            warning(paste(primary_id,"appears to be a future_series,", 
-                    "but its root cannot be found.", 
-                    "Creating basic instrument instead."))
+            warning(paste(primary_id," appears to be a future_series, ", 
+                    "but its root cannot be found. ", 
+                    "Creating _", default_type, "_ instrument instead.", sep=""))
             warned <- TRUE
         }
     }
@@ -685,9 +687,9 @@
         if (is.instrument(root)) {
             return(option_series(primary_id, defined.by='auto', ...))
         } else if (!silent) {
-            warning(paste(primary_id,"appears to be an option_series,", 
-                "but its root cannot be found.", 
-                "Creating basic instrument instead."))
+            warning(paste(primary_id," appears to be an option_series, ", 
+                "but its root cannot be found. ", 
+                "Creating _", default_type, "_ instrument instead.", sep=""))
             warned <- TRUE
         }
     } 
@@ -696,6 +698,17 @@
     if (any(pid$type == 'synthetic')) {
         return(synthetic(members=strsplit(primary_id,"\\.")[[1]], currency=currency, defined.by='auto', ...) )
     } 
+    if(is.function(try(match.fun(default_type),silent=TRUE))) {
+        if (!silent && !warned) 
+            warning('Creating a _', default_type, '_ instrument because ', 
+                    primary_id, ' is not of an unambiguous format.') 
+            dargs <- list(...)
+            dargs$primary_id <- primary_id
+            dargs$currency <- currency
+            dargs$multiplier <- multiplier
+            dargs$defined.by='auto'
+         return(do.call(default_type, dargs))
+    }
     if (!silent && !warned) 
         warning(paste(primary_id, 'is not of an unambiguous format.', 
                 'Creating basic instrument with multiplier 1.')) 

Modified: pkg/FinancialInstrument/R/load.instruments.R
===================================================================
--- pkg/FinancialInstrument/R/load.instruments.R	2011-09-16 15:07:53 UTC (rev 780)
+++ pkg/FinancialInstrument/R/load.instruments.R	2011-09-17 13:55:00 UTC (rev 781)
@@ -47,6 +47,7 @@
 #' load.instruments(system.file('data/currencies.csv',package='FinancialInstrument'))
 #' load.instruments(system.file('data/root_contracts.csv',package='FinancialInstrument'))
 #' load.instruments(system.file('data/future_series.csv',package='FinancialInstrument'))
+#'
 #' }
 #' @export
 load.instruments <- function (file=NULL, ..., metadata=NULL, id_col=1, default_type='stock') {
@@ -70,11 +71,17 @@
     } else {
         set_primary<-FALSE
     }
+    dotargs<-list(...)
     if(!any(grepl('type',colnames(filedata)))) {
-        warning("metadata does not appear to contain instrument type, using ",default_type,". This may produce incorrect valuations.")
-        filedata$type<-rep(default_type,nrow(filedata))
+        if (is.null(dotargs$type)) {
+            warning("metadata does not appear to contain instrument type, using ",default_type,". This may produce incorrect valuations.")
+            filedata$type<-rep(default_type,nrow(filedata))
+        } else {
+            filedata$type <- rep(default_type, nrow(filedata))
+            dotargs$type <- NULL        
+        }
     }
-    dotargs<-list('...')
+    if (!is.null(dotargs$currency) && !is.currency(dotargs$currency)) currency(dotargs$currency)
     
     #now process the data
     for(rn in 1:nrow(filedata)){
@@ -108,7 +115,7 @@
             if(!is.null(arg$RIC)){
                 if(substr(arg$RIC,1,1)==1) arg$RIC <- substr(arg$RIC,2,nchar(arg$RIC))
             }            
-            if(length(dotargs)) args<-c(args,dotargs)
+            if(length(dotargs)) arg<-c(arg,dotargs)
             
             if(is.function(try(match.fun(type),silent=TRUE))){
                 out <- try(do.call(type,arg))

Modified: pkg/FinancialInstrument/man/instrument.auto.Rd
===================================================================
--- pkg/FinancialInstrument/man/instrument.auto.Rd	2011-09-16 15:07:53 UTC (rev 780)
+++ pkg/FinancialInstrument/man/instrument.auto.Rd	2011-09-17 13:55:00 UTC (rev 781)
@@ -2,8 +2,8 @@
 \alias{instrument.auto}
 \title{Create an instrument based on name alone}
 \usage{
-  instrument.auto(primary_id, currency = "USD", silent =
-  FALSE, ...)
+  instrument.auto(primary_id, currency = "USD", multiplier
+  = 1, silent = FALSE, default_type = "NULL", ...)
 }
 \arguments{
   \item{primary_id}{charater primary identifier of
@@ -12,8 +12,14 @@
   \item{currency}{character name of currency that
   instrument will be denominated it. Default=\dQuote{USD}}
 
+  \item{multiplier}{numeric product multiplier}
+
   \item{silent}{TRUE/FALSE. silence warnings?}
 
+  \item{default_type}{What type of instrument to make if it
+  is not clear from the primary_id. ("stock", "future",
+  etc.)}
+
   \item{...}{other passthrough parameters}
 }
 \value{



More information about the Blotter-commits mailing list