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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Nov 18 15:10:56 CET 2010


Author: braverock
Date: 2010-11-18 15:10:55 +0100 (Thu, 18 Nov 2010)
New Revision: 458

Modified:
   pkg/FinancialInstrument/DESCRIPTION
   pkg/FinancialInstrument/R/instrument.R
   pkg/FinancialInstrument/R/synthetic.R
   pkg/FinancialInstrument/man/future_series.Rd
   pkg/FinancialInstrument/man/instrument.Rd
   pkg/FinancialInstrument/man/synthetic.ratio.Rd
Log:
- updates to make constructors more robust
- add assign_i parameter to instrument constructor, use in all type-specific constructors
- use make.names() instead of custom regex
- use main instrument constructor for constructing synthetics
- update roxygen docs

Modified: pkg/FinancialInstrument/DESCRIPTION
===================================================================
--- pkg/FinancialInstrument/DESCRIPTION	2010-11-17 20:37:49 UTC (rev 457)
+++ pkg/FinancialInstrument/DESCRIPTION	2010-11-18 14:10:55 UTC (rev 458)
@@ -1,7 +1,7 @@
 Package: FinancialInstrument
 Type: Package
 Title: Financial Instrument Model Infrastructure for R
-Version: 0.2
+Version: 0.2.1
 Date: $Date$
 Author: Peter Carl, Dirk Eddelbuettel, Jeffrey Ryan, Joshua Ulrich,
     Brian G. Peterson
@@ -14,4 +14,3 @@
 Depends: R (>= 2.11.1), xts, zoo
 Suggests: quantmod
 Copyright: (c) 2004 - 2010
-Collate: 'buildSpread.R' 'instrument.R' 'load.instruments.R' 'synthetic.R' 'volep.R'

Modified: pkg/FinancialInstrument/R/instrument.R
===================================================================
--- pkg/FinancialInstrument/R/instrument.R	2010-11-17 20:37:49 UTC (rev 457)
+++ pkg/FinancialInstrument/R/instrument.R	2010-11-18 14:10:55 UTC (rev 458)
@@ -37,12 +37,21 @@
 #' This is robust enough if you take some care, though a more robust patch would be welcomed.
 #' 
 #' The \code{primary_id} will be coerced within reason to a valid \R variable name by 
-#' replacing +-=^\%$#@:; with _ . 
+#' using \code{\link{make.names}}  Please use some care to choose your primary identifiers so that R won't complain.
+#' If you have better regular expression code, we'd be happy to include it.   
 #' 
 #' Identifiers will also try to be discovered as regular named arguments passed in via \code{...}.  
 #' We currently match any of the following: \code{"CUSIP","SEDOL","ISIN","OSI","Bloomberg","Reuters","X.RIC","CQG","TT","Yahoo","Google"}
 #' Others mat be specified using a named list of identifiers, as described above.
 #' 
+#' \code{assign_i} will use \code{\link{assign}} to place the constructed 
+#' instrument class object into the \code{.instrument} environment.  Most of the 
+#' special type-specific constructors will use \code{assign_i=TRUE} internally. 
+#' Calling with \code{assign_i=FALSE}, or not specifying it, will return an object and
+#' will \emph{not} store it.  Use this option ether to wrap calls to \code{instrument}
+#' prior to further processing (and presumably assignment) or to test your parameters
+#' before assignment.
+#' 
 #' @param primary_id string describing the unique ID for the instrument
 #' @param ... any other passthru parameters 
 #' @param currency string describing the currency ID of an object of type \code{\link{currency}}
@@ -51,6 +60,7 @@
 #' @param identifiers named list of any other identifiers that should also be stored for this instrument
 #' @param type instrument type to be appended to the class definition, typically not set by user
 #' @param underlying_id for derivatives, the identifier of the instrument that this one is derived from, may be NULL for cash settled instruments
+#' @param assign_i TRUE/FALSE if TRUE, assign the instrument to the .instrument environment, default FALSE
 #' @aliases 
 #' stock
 #' bond
@@ -64,12 +74,13 @@
 #' \code{\link{future_series}}
 #' \code{\link{load.instruments}}
 #' @export
-instrument<-function(primary_id , ..., currency , multiplier , tick_size=NULL, identifiers = NULL, type=NULL ){
+instrument<-function(primary_id , ..., currency , multiplier , tick_size=NULL, identifiers = NULL, type=NULL, assign_i=FALSE ){
   if(is.null(primary_id)) stop("you must specify a primary_id for the instrument")
   
-  primary_id<-gsub("[+-=^%$#@:;]","_",primary_id) # replace illegal characters
+  #primary_id<-gsub("[+-=^%$@:;]",".",primary_id) # replace illegal characters
+  primary_id<-make.names(primary_id)
   
-  if(!is.currency(currency)) stop("currency must be an object of type 'currency'")
+  if(!is.currency(currency)) stop("currency ",currency," must be an object of type 'currency'")
 
   if(!hasArg(identifiers) || is.null(identifiers)) identifiers = list()
   if(!is.list(identifiers)) {
@@ -95,21 +106,22 @@
   if(is.null(type)) tclass="instrument" else tclass = c(type,"instrument")
 
   tmpinstr <- list(primary_id = primary_id,
-                   type = type,
                    currency = currency,
                    multiplier = multiplier,
 				   tick_size=tick_size,
-                   identifiers = identifiers
+                   identifiers = identifiers,
+                   type = type
                    )
   if(length(arg)>=1) tmpinstr <- c(tmpinstr,arg)   
   class(tmpinstr)<-tclass
-  return(tmpinstr)
+  
+  if(assign_i)  assign(primary_id, tmpinstr, envir=as.environment(.instrument) )
+  else return(tmpinstr) 
 }
 
 #' @export
 stock <- function(primary_id , currency=NULL , multiplier=1 , tick_size=.01, identifiers = NULL, ...){
-	stock_temp=  instrument(primary_id=primary_id , currency=currency , multiplier=multiplier , tick_size=tick_size, identifiers = identifiers, ..., type="stock")
-	assign(primary_id, stock_temp, envir=as.environment(.instrument) )
+	stock_temp=  instrument(primary_id=primary_id , currency=currency , multiplier=multiplier , tick_size=tick_size, identifiers = identifiers, ..., type="stock", assign_i=TRUE)
 }
 
 #' @export
@@ -120,12 +132,14 @@
         if(!exists(underlying_id, where=.instrument,inherits=TRUE)) warning("underlying_id not found") # assumes that we know where to look
     }
 
-    future_temp = instrument(primary_id=primary_id , currency=currency , multiplier=multiplier , tick_size=tick_size, identifiers = identifiers, ... , type="future", underlying_id=underlying_id )
-  
-    assign(primary_id, future_temp, envir=as.environment(.instrument) )
+    future_temp = instrument(primary_id=primary_id , currency=currency , multiplier=multiplier , tick_size=tick_size, identifiers = identifiers, ... , type="future", underlying_id=underlying_id, assign_i=TRUE )
 }
 
 #' constructors for series contracts on instruments such as options and futures
+#' 
+#' In custom parameters for these series contracts, we have often found it
+#' useful to store attributes such as local roll-on and roll-off dates
+#' (rolling not on the \code{first_listed} or \code{expires}
 #' @param primary_id string describing the unique ID for the instrument
 #' @param suffix_id string suffix that should be associated with the series, usually something like 'Z9' or 'Mar10' denoting expiration and year
 #' @param first_traded string coercible to Date for first trading day
@@ -150,20 +164,21 @@
       message("updating existing first_traded and expires")
       temp_series$first_traded<-c(temp_series$first_traded,first_traded)
       temp_series$expires<-c(temp_series$expires,expires)
+      assign(paste(primary_id, suffix_id, sep="_"), temp_series, envir=as.environment(.instrument))
   } else {
-      temp_series = instrument( primary_id = contract$primary_id,
-                         suffix_id = suffix_id,
-                         currency = contract$currency,
-                         multiplier = contract$multiplier,
-						 tick_size=contract$tick_size,
-						 first_traded = first_traded,
-                         expires = expires,
-                         identifiers = identifiers,
-                         type=c("future_series", "future")
-                         ) 
+      temp_series = instrument( primary_id = paste(contract$primary_id,suffix_id,sep='_'),
+                                 suffix_id=suffix_id,
+                                 currency = contract$currency,
+                                 multiplier = contract$multiplier,
+        						 tick_size=contract$tick_size,
+        						 first_traded = first_traded,
+                                 expires = expires,
+                                 identifiers = identifiers,
+                                 type=c("future_series", "future"),
+                                 ...,
+                                 assign_i=TRUE
+                              ) 
   }
-
-  assign(paste(primary_id, suffix_id, sep="_"), temp_series, envir=as.environment(.instrument))
 }
 
 #' @export
@@ -176,38 +191,37 @@
       if(!exists(underlying_id, where=.instrument,inherits=TRUE)) warning("underlying_id not found") # assumes that we know where to look
   }
   ## now structure and return
-  option_temp = instrument(primary_id=primary_id , currency=currency , multiplier=multiplier , tick_size=tick_size, identifiers = identifiers, ... , type="option", underlying_id=underlying_id )
-  
-  assign(primary_id, option_temp, envir=as.environment(.instrument) )
+  option_temp = instrument(primary_id=primary_id , currency=currency , multiplier=multiplier , tick_size=tick_size, identifiers = identifiers, ... , type="option", underlying_id=underlying_id, assign_i=TRUE )
 }
 
 #' @export
 option_series <- function(primary_id , suffix_id, first_traded=NULL, expires=NULL, callput=c("call","put"), identifiers = NULL, ...){
-  contract<-try(getInstrument(primary_id))
-  if(!inherits(contract,"option")) stop("options contract spec must be defined first")
-  ## with options series we probably need to be more sophisticated,
-  ## and find the existing series from prior periods (probably years)
-  ## and then add the first_traded and expires to the time series
-  if(length(callput)==2) stop("value of callput must be specified as 'call' or 'put'")
-  temp_series<-try(getInstrument(paste(primary_id, suffix_id,sep="_")),silent=TRUE)
-  if(inherits(temp_series,"option_series")) {
-    message("updating existing first_traded and expires")
-    temp_series$first_traded<-c(temp_series$first_traded,first_traded)
-    temp_series$expires<-c(temp_series$expires,expires)
-  } else {
-      temp_series = instrument( primary_id = contract$primary_id,
-              suffix_id = suffix_id,
-              currency = contract$currency,
-              multiplier = contract$multiplier,
-              tick_size=contract$tick_size,
-              first_traded = first_traded,
-              expires = expires,
-              identifiers = identifiers,
-              type=c("option_series", "option")
-      ) 
-  }
-
-  assign(paste(primary_id, suffix_id,sep="_"), temp_series, envir=as.environment(.instrument))
+    contract<-try(getInstrument(primary_id))
+    if(!inherits(contract,"option")) stop("options contract spec must be defined first")
+    ## with options series we probably need to be more sophisticated,
+    ## and find the existing series from prior periods (probably years)
+    ## and then add the first_traded and expires to the time series
+    if(length(callput)==2) stop("value of callput must be specified as 'call' or 'put'")
+    temp_series<-try(getInstrument(paste(primary_id, suffix_id,sep="_")),silent=TRUE)
+    if(inherits(temp_series,"option_series")) {
+        message("updating existing first_traded and expires")
+        temp_series$first_traded<-c(temp_series$first_traded,first_traded)
+        temp_series$expires<-c(temp_series$expires,expires)
+        assign(paste(primary_id, suffix_id,sep="_"), temp_series, envir=as.environment(.instrument))
+    } else {
+        temp_series = instrument( primary_id = paste(contract$primary_id,suffix_id,sep='_'),
+                                    suffix_id = suffix_id,
+                                    currency = contract$currency,
+                                    multiplier = contract$multiplier,
+                                    tick_size=contract$tick_size,
+                                    first_traded = first_traded,
+                                    expires = expires,
+                                    identifiers = identifiers,
+                                    ...,
+                                    type=c("option_series", "option"),
+                                    assign_i=TRUE
+                                ) 
+    }
 }
 
 #' @export
@@ -247,14 +261,12 @@
   if(!exists(second_currency, where=.instrument,inherits=TRUE)) warning("second_currency not found") # assumes that we know where to look
 
   ## now structure and return
-  exrate_temp=  instrument(primary_id=primary_id , currency=currency , multiplier=1 , tick_size=.01, identifiers = identifiers, ..., secon_currency=second_currency, type=c("exchange_rate","currency"))
-  assign(primary_id, exrate_temp, envir=as.environment(.instrument) )
+  exrate_temp=  instrument(primary_id=primary_id , currency=currency , multiplier=1 , tick_size=.01, identifiers = identifiers, ..., secon_currency=second_currency, type=c("exchange_rate","currency"), assign_i=TRUE)
 }
 
 #TODO  auction dates, coupons, etc for govmt. bonds
 bond <- function(primary_id , currency , multiplier, tick_size=NULL , identifiers = NULL, ...){
-    bond_temp = instrument(primary_id=primary_id , currency=currency , multiplier=multiplier , tick_size=tick_size, identifiers = identifiers, ..., type="bond" )
-    assign( primary_id, bond_temp, envir=as.environment(.instrument) )
+    bond_temp = instrument(primary_id=primary_id , currency=currency , multiplier=multiplier , tick_size=tick_size, identifiers = identifiers, ..., type="bond", assign_i=TRUE )
 }
 
 

Modified: pkg/FinancialInstrument/R/synthetic.R
===================================================================
--- pkg/FinancialInstrument/R/synthetic.R	2010-11-17 20:37:49 UTC (rev 457)
+++ pkg/FinancialInstrument/R/synthetic.R	2010-11-18 14:10:55 UTC (rev 458)
@@ -12,20 +12,9 @@
 ###############################################################################
 
 #' @export
-synthetic <- function(primary_id , currency , multiplier=1, identifiers = NULL, ..., members=NULL, cl=c("synthetic", "instrument"))
+synthetic <- function(primary_id , currency , multiplier=1, identifiers = NULL, ..., members=NULL, type=c("synthetic", "instrument"))
 {
-
-    synthetic_temp = instrument(primary_id , currency , multiplier , identifiers = identifiers, ..., type=NULL )
-    
-    ## now structure and return
-    return(structure( list(primary_id = synthetic_temp$primary_id,
-                            currency = synthetic_temp$currency,
-                            multiplier = synthetic_temp$multiplier,
-                            identifiers = synthetic_temp$identifiers,
-                            memberlist = members 
-                    ),
-                    class=cl )
-           )
+    synthetic_temp = instrument(primary_id , currency , multiplier , identifiers = identifiers, ..., type=type, members=members, assign_i=TRUE )    
 }
 
 #' constructors for synthetic instruments
@@ -34,7 +23,7 @@
 #' @param multiplier numeric multiplier to apply to the price in the instrument currency to get to notional value
 #' @param identifiers character vector of any other identifiers that should also be stored for this instrument
 #' @param ... any other passthru parameters 
-#' @param cl class string, should not be set by users
+#' @param type class string, should not be set by users
 #' @param members character vector of instrument identifiers that make up the synthetic
 #' @param memberratio numeric vector of ratio relationships between members, e.g. c(4,3) for a 4:3 spread
 #' @aliases
@@ -42,7 +31,7 @@
 #' spread 
 #' synthetic.ratio
 #' @export
-synthetic.ratio <- function(primary_id , currency , multiplier=1, identifiers = NULL, ..., cl=c("synthetic.ratio","synthetic","instrument"), members, memberratio)
+synthetic.ratio <- function(primary_id , currency , multiplier=1, identifiers = NULL, ..., type=c("synthetic.ratio","synthetic","instrument"), members, memberratio)
 {
     #TODO make sure that with options/futures or other  instruments that we have you use the base contract
     if(!is.list(members)){
@@ -69,22 +58,11 @@
         warning("passing in members as a list not fully tested")
         memberlist=members
     }
-    synthetic_temp = synthetic(primary_id , currency , multiplier=multiplier , identifiers = identifiers, members=members , memberratio=memberratio, ...  )
-    ## now structure and return
-    assign(primary_id, structure( list(primary_id = synthetic_temp$primary_id,
-                            currency = synthetic_temp$currency,
-                            multiplier = synthetic_temp$multiplier,
-                            identifiers = synthetic_temp$identifiers,
-                            memberlist = memberlist
-                    ),
-                    class=cl
-            ), # end structure
-            envir=as.environment(.instrument)
-    )
+    synthetic_temp = synthetic(primary_id , currency , multiplier=multiplier , identifiers = identifiers, members=memberlist , memberratio=memberratio, ... ,type=type, assign_i=TRUE )
 }
 
 #' @export
 spread <- function(primary_id , currency , members, memberratio, ..., multiplier=1, identifiers = NULL)
 {
-    synthetic.ratio(primary_id , currency , multiplier=multiplier, identifiers = NULL, cl=c("spread","synthetic.ratio","synthetic","instrument"), members=members, memberratio=memberratio, ...=...)
+    synthetic.ratio(primary_id , currency , multiplier=multiplier, identifiers = NULL, type=c("spread","synthetic.ratio","synthetic","instrument"), members=members, memberratio=memberratio, ...=..., assign_i=TRUE)
 }

Modified: pkg/FinancialInstrument/man/future_series.Rd
===================================================================
--- pkg/FinancialInstrument/man/future_series.Rd	2010-11-17 20:37:49 UTC (rev 457)
+++ pkg/FinancialInstrument/man/future_series.Rd	2010-11-18 14:10:55 UTC (rev 458)
@@ -3,6 +3,9 @@
 \usage{future_series(primary_id, suffix_id, first_traded, expires,
     identifiers, ...)}
 \description{constructors for series contracts on instruments such as options and futures}
+\details{In custom parameters for these series contracts, we have often found it
+useful to store attributes such as local roll-on and roll-off dates
+(rolling not on the \code{first_listed} or \code{expires}}
 \alias{option_series}
 \alias{future_series}
 \arguments{\item{primary_id}{string describing the unique ID for the instrument}

Modified: pkg/FinancialInstrument/man/instrument.Rd
===================================================================
--- pkg/FinancialInstrument/man/instrument.Rd	2010-11-17 20:37:49 UTC (rev 457)
+++ pkg/FinancialInstrument/man/instrument.Rd	2010-11-18 14:10:55 UTC (rev 458)
@@ -1,7 +1,7 @@
 \name{instrument}
 \title{instrument class constructors...}
 \usage{instrument(primary_id, ..., currency, multiplier, tick_size,
-    identifiers, type)}
+    identifiers, type, assign_i=FALSE)}
 \description{instrument class constructors}
 \details{All 'currency' instruments must be defined before instruments of other types may be defined.
 
@@ -15,7 +15,20 @@
 This is robust enough if you take some care, though a more robust patch would be welcomed.
 
 The \code{primary_id} will be coerced within reason to a valid \R variable name by 
-replacing +-=^\%$#}
+using \code{\link{make.names}}  Please use some care to choose your primary identifiers so that R won't complain.
+If you have better regular expression code, we'd be happy to include it.   
+
+Identifiers will also try to be discovered as regular named arguments passed in via \code{...}.  
+We currently match any of the following: \code{"CUSIP","SEDOL","ISIN","OSI","Bloomberg","Reuters","X.RIC","CQG","TT","Yahoo","Google"}
+Others mat be specified using a named list of identifiers, as described above.
+
+\code{assign_i} will use \code{\link{assign}} to place the constructed 
+instrument class object into the \code{.instrument} environment.  Most of the 
+special type-specific constructors will use \code{assign_i=TRUE} internally. 
+Calling with \code{assign_i=FALSE}, or not specifying it, will return an object and
+will \emph{not} store it.  Use this option ether to wrap calls to \code{instrument}
+prior to further processing (and presumably assignment) or to test your parameters
+before assignment.}
 \alias{stock}
 \alias{bond}
 \alias{future}
@@ -33,4 +46,5 @@
 \item{tick_size}{the tick increment of the instrument price in it's trading venue, as numeric quantity (e.g. 1/8 is .125)}
 \item{identifiers}{named list of any other identifiers that should also be stored for this instrument}
 \item{type}{instrument type to be appended to the class definition, typically not set by user}
-\item{underlying_id}{for derivatives, the identifier of the instrument that this one is derived from, may be NULL for cash settled instruments}}
+\item{underlying_id}{for derivatives, the identifier of the instrument that this one is derived from, may be NULL for cash settled instruments}
+\item{assign_i}{TRUE/FALSE if TRUE, assign the instrument to the .instrument environment, default FALSE}}

Modified: pkg/FinancialInstrument/man/synthetic.ratio.Rd
===================================================================
--- pkg/FinancialInstrument/man/synthetic.ratio.Rd	2010-11-17 20:37:49 UTC (rev 457)
+++ pkg/FinancialInstrument/man/synthetic.ratio.Rd	2010-11-18 14:10:55 UTC (rev 458)
@@ -1,7 +1,7 @@
 \name{synthetic.ratio}
 \title{constructors for synthetic instruments...}
 \usage{synthetic.ratio(primary_id, currency, multiplier=1, identifiers, ...,
-    cl=c("synthetic.ratio", "synthetic", "instrument"), members,
+    type=c("synthetic.ratio", "synthetic", "instrument"), members,
     memberratio)}
 \description{constructors for synthetic instruments}
 \alias{synthetic}
@@ -12,6 +12,6 @@
 \item{multiplier}{numeric multiplier to apply to the price in the instrument currency to get to notional value}
 \item{identifiers}{character vector of any other identifiers that should also be stored for this instrument}
 \item{...}{any other passthru parameters}
-\item{cl}{class string, should not be set by users}
+\item{type}{class string, should not be set by users}
 \item{members}{character vector of instrument identifiers that make up the synthetic}
 \item{memberratio}{numeric vector of ratio relationships between members, e.g. c(4,3) for a 4:3 spread}}



More information about the Blotter-commits mailing list