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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Mar 24 01:53:24 CET 2012


Author: gsee
Date: 2012-03-24 01:53:23 +0100 (Sat, 24 Mar 2012)
New Revision: 978

Added:
   pkg/FinancialInstrument/man/add.identifier.Rd
Modified:
   pkg/FinancialInstrument/DESCRIPTION
   pkg/FinancialInstrument/NAMESPACE
   pkg/FinancialInstrument/R/instrument.R
   pkg/FinancialInstrument/man/instrument_attr.Rd
Log:
 - add function add.identifier
 - instrument_attr: if trying to set identifiers, make sure identifiers is a 
      list; if not, call add.identifier
 - instrument_attr: replace ifs with if() {} else if()

Modified: pkg/FinancialInstrument/DESCRIPTION
===================================================================
--- pkg/FinancialInstrument/DESCRIPTION	2012-03-24 00:10:48 UTC (rev 977)
+++ pkg/FinancialInstrument/DESCRIPTION	2012-03-24 00:53:23 UTC (rev 978)
@@ -11,7 +11,7 @@
     meta-data and relationships. Provides support for
     multi-asset class and multi-currency portfolios. Still
     in heavy development.
-Version: 0.13.1
+Version: 0.13.2
 URL: https://r-forge.r-project.org/projects/blotter/
 Date: $Date$
 Depends:

Modified: pkg/FinancialInstrument/NAMESPACE
===================================================================
--- pkg/FinancialInstrument/NAMESPACE	2012-03-24 00:10:48 UTC (rev 977)
+++ pkg/FinancialInstrument/NAMESPACE	2012-03-24 00:53:23 UTC (rev 978)
@@ -1,4 +1,5 @@
 export(.to_daily)
+export(add.identifier)
 export(bond_series)
 export(bond)
 export(build_series_symbols)

Modified: pkg/FinancialInstrument/R/instrument.R
===================================================================
--- pkg/FinancialInstrument/R/instrument.R	2012-03-24 00:10:48 UTC (rev 977)
+++ pkg/FinancialInstrument/R/instrument.R	2012-03-24 00:53:23 UTC (rev 978)
@@ -1006,6 +1006,12 @@
 #' If \code{attr} is \dQuote{src}, \code{value} will be used in a call to \code{setSymbolLookup}.  
 #' Other checks are in place to make sure that \dQuote{currency} remains a \code{\link{currency}} object and that
 #' \dQuote{multiplier} and \dQuote{tick_size} can only be changed to reasonable values.
+#' 
+#' If \code{attr} is \dQuote{identifiers} and \code{value} is \code{NULL}, 
+#' \code{identifiers} will be set to \code{list()}.  If \code{value} is not a 
+#' list, \code{\link{add.identifier}} will be called with \code{value}.
+#' \code{add.identifier} will convert \code{value} to a list and append it to
+#' the current \code{identifiers}
 #' @param primary_id primary_id of the instrument that will be updated
 #' @param attr name of the slot that will be added or changed
 #' @param value what to assign to the \code{attr} slot of the \code{primary_id} instrument
@@ -1035,34 +1041,74 @@
     instr <- try(getInstrument(primary_id, silent=TRUE))
     if (inherits(instr, 'try-error') || !is.instrument(instr))
         stop(paste('instrument ',primary_id,' must be defined first.',sep=''))
-    instr[[attr]] <- value
-    if (attr == 'primary_id') rm(list = primary_id, pos = FinancialInstrument:::.instrument)
-    if (attr == 'currency' && !is.instrument(getInstrument(value,type='currency',silent=TRUE)))
-        stop("currency ", value, " must be an object of type 'currency'")
-    if (attr == 'multiplier' && (!is.numeric(value) || length(value) > 1))
-        stop("multiplier must be a single number")
-    if (attr == 'tick_size' && (!is.null(value) && (!is.numeric(value) || length(value) > 1)))
-        stop("tick_size must be NULL or a single number")
-    if (attr == 'type') {
+    if (attr == 'primary_id') {
+        rm(list = primary_id, pos = FinancialInstrument:::.instrument)
+    } else if (attr == 'currency') {
+        if (!is.currency.name(value)) {
+            stop("currency ", value, " must be an object of type 'currency'")
+        }
+    } else if (attr == 'multiplier') {
+        if (!is.numeric(value) || length(value) > 1) {
+            stop("multiplier must be a single number")
+        }
+    } else if (attr == 'tick_size') {
+        if (!is.null(value) && (!is.numeric(value) || length(value) > 1)) {
+            stop("tick_size must be NULL or a single number")
+        }
+    } else if (attr == 'type') {
         tclass <- unique(c(value, "instrument"))
         class(instr) <- tclass
-    }
-    if (attr == 'IB') {
+    } else if (attr == 'IB') {
         if (inherits(value, 'twsContract')) {
-            class(instr) <- unique(c(class(instr)[1], 'twsInstrument', class(instr)[-1]))
+            class(instr) <- unique(c(class(instr)[1], 'twsInstrument', 
+                                     class(instr)[-1]))
         } else {
             warning('non-twsContract assigned to $IB')
             class(instr) <- class(instr)[!class(instr) %in% 'twsInstrument']
         }
-    }
-    if (attr == 'src') {
+    } else if (attr == 'src') {
         sarg <- list()
         sarg[[instr$primary_id]] <- value
         setSymbolLookup(sarg)
+    } else if (attr == 'identifiers') {
+        if (length(value) == 0) {
+            value <- list()
+        } else if (!is.list(value)) {
+            #warning("identifiers must be a list. Appending current identifiers.")
+            # add.identifier will convert to list
+            return(add.identifier(primary_id, value))
+        }
     }
+    instr[[attr]] <- value
     assign(instr$primary_id, instr, pos=FinancialInstrument:::.instrument)
 }
 
+                       
+#' add an identifier to an \code{instrument}
+#' @param primary_id primary_id of an \code{\link{instrument}}
+#' @param identifier the identifier to add.  Either a list, or a string that
+#'   will be coerced to a list.
+#' @param ... parameters to pass through to \code{\link{getInstrument}}
+#' @return called for side-effect
+#' @author Garrett See
+#' @seealso \code{\link{instrument_attr}}
+#' @examples
+#' \dontrun{
+#' stock("XXX", currency("USD"))
+#' add.identifier("XXX", list(yahoo="^XXX")) 
+#' getInstrument("^XXX")
+#' add.identifier("^XXX", "x3")
+#' all.equal(getInstrument("x3"), getInstrument("XXX")) #TRUE
+#' }
+#' @export
+add.identifier <- function(primary_id, identifier, ...) {
+   ident <- getInstrument(primary_id, ...)[["identifiers"]]
+   if (!is.list(identifier)) identifier <- as.list(identifier)
+   instrument_attr(primary_id, "identifiers",  c(ident, identifier))
+}
+   
+                       
+
 #' instrument class print method
 #' 
 #' @method print instrument

Added: pkg/FinancialInstrument/man/add.identifier.Rd
===================================================================
--- pkg/FinancialInstrument/man/add.identifier.Rd	                        (rev 0)
+++ pkg/FinancialInstrument/man/add.identifier.Rd	2012-03-24 00:53:23 UTC (rev 978)
@@ -0,0 +1,38 @@
+\name{add.identifier}
+\alias{add.identifier}
+\title{add an identifier to an \code{instrument}}
+\usage{
+  add.identifier(primary_id, identifier, ...)
+}
+\arguments{
+  \item{primary_id}{primary_id of an
+  \code{\link{instrument}}}
+
+  \item{identifier}{the identifier to add.  Either a list,
+  or a string that will be coerced to a list.}
+
+  \item{...}{parameters to pass through to
+  \code{\link{getInstrument}}}
+}
+\value{
+  called for side-effect
+}
+\description{
+  add an identifier to an \code{instrument}
+}
+\examples{
+\dontrun{
+stock("XXX", currency("USD"))
+add.identifier("XXX", list(yahoo="^XXX"))
+getInstrument("^XXX")
+add.identifier("^XXX", "x3")
+all.equal(getInstrument("x3"), getInstrument("XXX")) #TRUE
+}
+}
+\author{
+  Garrett See
+}
+\seealso{
+  \code{\link{instrument_attr}}
+}
+

Modified: pkg/FinancialInstrument/man/instrument_attr.Rd
===================================================================
--- pkg/FinancialInstrument/man/instrument_attr.Rd	2012-03-24 00:10:48 UTC (rev 977)
+++ pkg/FinancialInstrument/man/instrument_attr.Rd	2012-03-24 00:53:23 UTC (rev 978)
@@ -34,6 +34,14 @@
   \code{\link{currency}} object and that
   \dQuote{multiplier} and \dQuote{tick_size} can only be
   changed to reasonable values.
+
+  If \code{attr} is \dQuote{identifiers} and \code{value}
+  is \code{NULL}, \code{identifiers} will be set to
+  \code{list()}.  If \code{value} is not a list,
+  \code{\link{add.identifier}} will be called with
+  \code{value}. \code{add.identifier} will convert
+  \code{value} to a list and append it to the current
+  \code{identifiers}
 }
 \note{
   you can remove an attribute/slot from an instrument by



More information about the Blotter-commits mailing list