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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Nov 2 15:45:06 CET 2010


Author: braverock
Date: 2010-11-02 15:45:06 +0100 (Tue, 02 Nov 2010)
New Revision: 436

Modified:
   pkg/FinancialInstrument/DESCRIPTION
   pkg/FinancialInstrument/R/instrument.R
   pkg/FinancialInstrument/man/exchange_rate.Rd
   pkg/FinancialInstrument/man/future_series.Rd
   pkg/FinancialInstrument/man/getInstrument.Rd
   pkg/FinancialInstrument/man/instrument.Rd
Log:
- add arbitrary identifiers and other fields
- search identifiers with getInstrument if primary_id doesn't match
- update documentation

Modified: pkg/FinancialInstrument/DESCRIPTION
===================================================================
--- pkg/FinancialInstrument/DESCRIPTION	2010-11-01 13:04:23 UTC (rev 435)
+++ pkg/FinancialInstrument/DESCRIPTION	2010-11-02 14:45:06 UTC (rev 436)
@@ -1,15 +1,18 @@
 Package: FinancialInstrument
 Type: Package
 Title: Financial Instrument Model Infrastructure for R
-Version: 0.0.2
+Version: 0.1
 Date: $Date$
-Author: Peter Carl, Dirk Eddelbuettel, Jeffrey Ryan, Joshua Ulrich, Brian G. Peterson
+Author: Peter Carl, Dirk Eddelbuettel, Jeffrey Ryan, Joshua Ulrich,
+    Brian G. Peterson
 Maintainer: Peter Carl <peter at braverock.com>
-Description: Infrastructure for defining instruments meta-data and relationships. Provides support for multi-asset class and multi-currency
-    portfolios.  Still in heavy development.
+Description: Infrastructure for defining instruments meta-data and
+    relationships. Provides support for multi-asset class and
+    multi-currency portfolios.  Still in heavy development.
 License: GPL
 LazyLoad: yes
 Depends: R (>= 2.9.0), xts, zoo
 Suggests: quantmod
 Copyright: (c) 2004 - 2010
-Collate: 'buildSpread.R' 'instrument.R' 'synthetic.R' 'volep.R'
+Collate: 'buildSpread.R' 'instrument.R' 'splice.R' 'splooth.R'
+    'synthetic.R' 'volep.R'

Modified: pkg/FinancialInstrument/R/instrument.R
===================================================================
--- pkg/FinancialInstrument/R/instrument.R	2010-11-01 13:04:23 UTC (rev 435)
+++ pkg/FinancialInstrument/R/instrument.R	2010-11-02 14:45:06 UTC (rev 436)
@@ -16,8 +16,6 @@
         .instrument <<- new.env()
 }
 
-## we should probably assign instruments into a special namespace and create get* functions.  Jeff?
-
 #' class test for object supposedly of type 'instrument'
 #' @param x object to test for type
 #' @export
@@ -27,15 +25,24 @@
 
 #' instrument class constructors
 #' 
-#' All 'currency' instruments must be defined before instruments of other types may be defined
+#' All 'currency' instruments must be defined before instruments of other types may be defined.
 #' 
+#' In \dots you may pass any other arbitrary instrument fields.  
+#' S3 classes in \R are basically lists with a class attribute.
+#' We use this to our advantage to allow us to set arbitrary fields.
+#' 
+#' \code{identifiers} should be a named list to specify other identifiers beyond the \code{primary_id}.
+#' Please note that whenever possible, these should still be unique.  Perhaps Bloomberg, Reuters-RIC, CUSIP, etc.  
+#' The code will return the first (and only the first) match that it finds, starting with the primary_id, and then searching all instruments in the list alphabetically by primary_id.  
+#' This is robust enough if you take some care, though a more robust patch would be welcomed.
+#' 
 #' @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}}
 #' @param multiplier numeric multiplier to apply to the price in the instrument currency to get to notional value
 #' @param tick_size the tick increment of the instrument price in it's trading venue, as numeric quantity (e.g. 1/8 is .125)
-#' @param identifiers character vector of any other identifiers that should also be stored for this instrument
-#' @param type instrument type to be appended to the class definition
+#' @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
 #' @aliases 
 #' stock
@@ -63,17 +70,17 @@
   
   if(is.null(type)) tclass="instrument" else tclass = c(type,"instrument")
 
-  ## now structure and return
-  return(structure( list(primary_id = primary_id,
-                         type = type,
-                         currency = currency,
-                         multiplier = multiplier,
-						 tick_size=tick_size,
-                         identifiers = identifiers
-                        ),
-                    class = tclass
-                  ) # end structure
-          )
+  tmpinstr <- list(primary_id = primary_id,
+                   type = type,
+                   currency = currency,
+                   multiplier = multiplier,
+				   tick_size=tick_size,
+                   identifiers = identifiers
+                   )
+                  
+  tmpinstr <- c(tmpinstr,list(...))   
+  class(tmpinstr)<-tclass
+  return(tmpinstr)
 }
 
 #' @export
@@ -84,25 +91,15 @@
 
 #' @export
 future <- function(primary_id , currency , multiplier , tick_size=NULL, identifiers = NULL, ..., underlying_id=NULL){
-  future_temp = instrument(primary_id=primary_id , currency=currency , multiplier=multiplier , tick_size=tick_size, identifiers = identifiers, ... , type="future" )
+    if(is.null(underlying_id)) {
+        warning("underlying_id should only be NULL for cash-settled futures")
+    } else {
+        if(!exists(underlying_id, where=.instrument,inherits=TRUE)) warning("underlying_id not found") # assumes that we know where to look
+    }
 
-  if(is.null(underlying_id)) {
-      warning("underlying_id should only be NULL for cash-settled futures")
-  } else {
-      if(!exists(underlying_id, where=.instrument,inherits=TRUE)) warning("underlying_id not found") # assumes that we know where to look
-  }
-  ## now structure and return
-  assign(primary_id, structure( list(primary_id = future_temp$primary_id,
-                         currency = future_temp$currency,
-                         multiplier = future_temp$multiplier,
-						 tick_size=future_temp$tick_size,
-						 identifiers = future_temp$identifiers,
-                         underlying_id = future_temp$underlying_id
-                        ),
-                    class=c("future","instrument")
-                  ), # end structure
-         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(primary_id, future_temp, envir=as.environment(.instrument) )
 }
 
 #' constructors for series contracts on instruments such as options and futures
@@ -110,7 +107,7 @@
 #' @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
 #' @param expires string coercible to Date for expiration date
-#' @param identifiers character vector of any other identifiers that should also be stored for this instrument
+#' @param identifiers named list of any other identifiers that should also be stored for this instrument
 #' @param ... any other passthru parameters
 #' @aliases 
 #' option_series
@@ -129,19 +126,18 @@
   if(inherits(temp_series,"future_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)
+      temp_series$expires<-c(temp_series$expires,expires)
   } else {
-    temp_series = structure( list(primary_id = contract$primary_id,
+      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
-                        ),
-                    class=c("future_series", "future", "instrument")
-             ) # end structure
+                         identifiers = identifiers,
+                         type=c("future_series", "future")
+                         ) 
   }
 
   assign(paste(primary_id, suffix_id, sep="_"), temp_series, envir=as.environment(.instrument))
@@ -157,17 +153,9 @@
       if(!exists(underlying_id, where=.instrument,inherits=TRUE)) warning("underlying_id not found") # assumes that we know where to look
   }
   ## now structure and return
-  assign(primary_id, structure( list(primary_id = option_temp$primary_id,
-                         currency = option_temp$currency,
-                         multiplier = option_temp$multiplier,
-						 tick_size = option_temp$tick_size,
-                         identifiers = option_temp$identifiers,
-                         underlying_id = option_temp$underlying_id
-                        ),
-                    class=c("option","instrument")
-                  ), # end structure
-         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(primary_id, option_temp, envir=as.environment(.instrument) )
 }
 
 #' @export
@@ -184,18 +172,16 @@
     temp_series$first_traded<-c(temp_series$first_traded,first_traded)
     temp_series$expires<-c(temp_series$expires,expires)
   } else {
-    temp_series = structure( list(primary_id = contract$primary_id,
-                         suffix_id = suffix_id,
-                         first_traded = first_traded,
-                         currency = contract$currency,
-                         multiplier = contract$multiplier,
-						 tick_size=contract$tick_size,
-                         expires = expires,
-                         callput = callput,
-                         identifiers = identifiers
-                        ),
-                    class=c("option_series", "option", "instrument")
-             ) # end structure
+      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))
@@ -204,17 +190,16 @@
 #' @export
 currency <- function(primary_id , currency=NULL , multiplier=1 , identifiers = NULL, ...){
   ## now structure and return
-  assign(primary_id, structure( list(primary_id = primary_id,
-                         type = "currency",
-                         currency = primary_id,
-                         multiplier = 1,
-                         tick_size=.01,
-                         identifiers = identifiers
-                        ),
-                    class=c("currency","instrument")
-                  ), # end structure
-         envir=as.environment(.instrument)
+  currency_temp <- list(primary_id = primary_id,
+          currency = primary_id,
+          multiplier = 1,
+          tick_size= .01,
+          identifiers = identifiers
   )
+  currency_temp <- c(currency_temp,list(...))   
+  
+  class(currency_temp)<-c("currency","instrument")
+  assign(primary_id, currency_temp, envir=as.environment(.instrument) )
 }
 
 #' class test for object supposedly of type 'currency'
@@ -229,7 +214,7 @@
 #' @param primary_id string identifier, usually expressed as a currency pair 'USDYEN' or 'EURGBP'
 #' @param currency string identifying front currency
 #' @param second_currency string identifying second currency
-#' @param identifiers character vector of any other identifiers that should also be stored for this instrument
+#' @param identifiers named list of any other identifiers that should also be stored for this instrument
 #' @param ... any other passthru parameters
 #' @export
 exchange_rate <- function (primary_id , currency , second_currency, identifiers = NULL, ...){
@@ -239,17 +224,8 @@
   if(!exists(second_currency, where=.instrument,inherits=TRUE)) warning("second_currency not found") # assumes that we know where to look
 
   ## now structure and return
-  assign(primary_id, structure( list(primary_id = primary_id,
-                         currency = currency,
-                         multiplier=1,
-                         tick_size=.01,
-                         second_currency = second_currency,
-                         identifiers = identifiers
-                        ),
-                    class=c("exchange_rate","instrument")
-                  ), # end structure
-         envir=as.environment(.instrument)
-  )
+  exrate_temp=  instrument(primary_id=primary_id , currency=primary_id , 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) )
 }
 
 #@TODO: government bond
@@ -260,13 +236,21 @@
 }
 
 
-#' primare accessor function for getting objects of type 'instrument'
+#' primary accessor function for getting objects of type 'instrument'
 #' @param x string identifier of instrument to retrieve
 #' @param Dates date range to retrieve 'as of', may not currently be implemented
 #' @export
 getInstrument <- function(x, Dates=NULL){
-    tmp_instr<-get(x,pos=.instrument) #removed inherits=TRUE
+    tmp_instr<-try(get(x,pos=.instrument),silent=TRUE) #removed inherits=TRUE
     if(inherits(tmp_instr,"try-error") | !is.instrument(tmp_instr)){
+        #first search
+        instr_list<-ls(pos=.instrument)
+        for (instr in instr_list){
+            tmp_instr<-try(get(instr,pos=.instrument),silent=TRUE)
+            if(is.instrument(tmp_instr) && length(grep(x,tmp_instr$identifiers))) {
+                return(tmp_instr)
+            }
+        }
         warning(paste("Instrument",x," not found, please create it first."))
         return(FALSE)
     } else{

Modified: pkg/FinancialInstrument/man/exchange_rate.Rd
===================================================================
--- pkg/FinancialInstrument/man/exchange_rate.Rd	2010-11-01 13:04:23 UTC (rev 435)
+++ pkg/FinancialInstrument/man/exchange_rate.Rd	2010-11-02 14:45:06 UTC (rev 436)
@@ -6,5 +6,5 @@
 \arguments{\item{primary_id}{string identifier, usually expressed as a currency pair 'USDYEN' or 'EURGBP'}
 \item{currency}{string identifying front currency}
 \item{second_currency}{string identifying second currency}
-\item{identifiers}{character vector of any other identifiers that should also be stored for this instrument}
+\item{identifiers}{named list of any other identifiers that should also be stored for this instrument}
 \item{...}{any other passthru parameters}}

Modified: pkg/FinancialInstrument/man/future_series.Rd
===================================================================
--- pkg/FinancialInstrument/man/future_series.Rd	2010-11-01 13:04:23 UTC (rev 435)
+++ pkg/FinancialInstrument/man/future_series.Rd	2010-11-02 14:45:06 UTC (rev 436)
@@ -9,5 +9,5 @@
 \item{suffix_id}{string suffix that should be associated with the series, usually something like 'Z9' or 'Mar10' denoting expiration and year}
 \item{first_traded}{string coercible to Date for first trading day}
 \item{expires}{string coercible to Date for expiration date}
-\item{identifiers}{character vector of any other identifiers that should also be stored for this instrument}
+\item{identifiers}{named list of any other identifiers that should also be stored for this instrument}
 \item{...}{any other passthru parameters}}

Modified: pkg/FinancialInstrument/man/getInstrument.Rd
===================================================================
--- pkg/FinancialInstrument/man/getInstrument.Rd	2010-11-01 13:04:23 UTC (rev 435)
+++ pkg/FinancialInstrument/man/getInstrument.Rd	2010-11-02 14:45:06 UTC (rev 436)
@@ -1,7 +1,7 @@
 \name{getInstrument}
 \alias{getInstrument}
-\title{primare accessor function for getting objects of type 'instrument'...}
+\title{primary accessor function for getting objects of type 'instrument'...}
 \usage{getInstrument(x, Dates)}
-\description{primare accessor function for getting objects of type 'instrument'}
+\description{primary accessor function for getting objects of type 'instrument'}
 \arguments{\item{x}{string identifier of instrument to retrieve}
 \item{Dates}{date range to retrieve 'as of', may not currently be implemented}}

Modified: pkg/FinancialInstrument/man/instrument.Rd
===================================================================
--- pkg/FinancialInstrument/man/instrument.Rd	2010-11-01 13:04:23 UTC (rev 435)
+++ pkg/FinancialInstrument/man/instrument.Rd	2010-11-02 14:45:06 UTC (rev 436)
@@ -3,7 +3,16 @@
 \usage{instrument(primary_id, ..., currency, multiplier, tick_size,
     identifiers, type)}
 \description{instrument class constructors}
-\details{All 'currency' instruments must be defined before instruments of other types may be defined}
+\details{All 'currency' instruments must be defined before instruments of other types may be defined.
+
+In \dots you may pass any other arbitrary instrument fields.  
+S3 classes in \R are basically lists with a class attribute.
+We use this to our advantage to allow us to set arbitrary fields.
+
+\code{identifiers} should be a named list to specify other identifiers beyond the \code{primary_id}.
+Please note that whenever possible, these should still be unique.  Perhaps Bloomberg, Reuters-RIC, CUSIP, etc.  
+The code will return the first (and only the first) match that it finds, starting with the primary_id, and then searching all instruments in the list alphabetically by primary_id.  
+This is robust enough if you take some care, though a more robust patch would be welcomed.}
 \alias{stock}
 \alias{bond}
 \alias{future}
@@ -18,6 +27,6 @@
 \item{currency}{string describing the currency ID of an object of type \code{\link{currency}}}
 \item{multiplier}{numeric multiplier to apply to the price in the instrument currency to get to notional value}
 \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}{character vector of any other identifiers that should also be stored for this instrument}
-\item{type}{instrument type to be appended to the class definition}
+\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}}



More information about the Blotter-commits mailing list