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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sun Jul 10 22:16:03 CEST 2011


Author: gsee
Date: 2011-07-10 22:16:02 +0200 (Sun, 10 Jul 2011)
New Revision: 670

Added:
   pkg/FinancialInstrument/R/instrument.table.R
   pkg/FinancialInstrument/man/instrument.table.Rd
Modified:
   pkg/FinancialInstrument/NAMESPACE
   pkg/FinancialInstrument/R/buildHierarchy.R
Log:
- instrument.table function added and exported
- avoid some warnings in buildHierarchy


Modified: pkg/FinancialInstrument/NAMESPACE
===================================================================
--- pkg/FinancialInstrument/NAMESPACE	2011-07-10 18:57:16 UTC (rev 669)
+++ pkg/FinancialInstrument/NAMESPACE	2011-07-10 20:16:02 UTC (rev 670)
@@ -19,6 +19,7 @@
 export(bond)
 export(bond_series)
 export(getInstrument)
+export(instrument.table)
 export(load.instruments)
 export(setSymbolLookup.FI)
 export(getSymbols.FI)

Modified: pkg/FinancialInstrument/R/buildHierarchy.R
===================================================================
--- pkg/FinancialInstrument/R/buildHierarchy.R	2011-07-10 18:57:16 UTC (rev 669)
+++ pkg/FinancialInstrument/R/buildHierarchy.R	2011-07-10 20:16:02 UTC (rev 670)
@@ -36,7 +36,7 @@
         attrs = NA
         for(level in levels){
             attr = unname(tmp_instr[eval(level)])
-            if(!is.na(attrs))
+            if(!all(is.na(attrs)))
                 attrs = cbind(attrs, attr)
             else
                 attrs = attr

Added: pkg/FinancialInstrument/R/instrument.table.R
===================================================================
--- pkg/FinancialInstrument/R/instrument.table.R	                        (rev 0)
+++ pkg/FinancialInstrument/R/instrument.table.R	2011-07-10 20:16:02 UTC (rev 670)
@@ -0,0 +1,52 @@
+#' Create data.frame with attributes of all instruments
+#' 
+#' A wrapper for \code{\link{buildHierarchy}}, that defaults to returning all attributes.
+#' By default it looks for the instrument with the most attribute levels, and uses those attributes
+#' for columns.  If you would prefer to use the attribute levels of a given instrument to build the columns, 
+#' use \code{attrs.of}.
+#'
+#' if there are some attributes that you do not want to be included in the returned data.frame, 
+#' specify them with \code{exclude}. 
+#' @param symbols A vector of instrument names to include
+#' @param exclude A vector of names of attributes that should not be included in the returned data.frame
+#' @param attrs.of name of a FinancialInstrument instrument. Returned data.frame columns will be the attributes of instrument.
+#' @return data.frame
+#' @author Garrett See
+#' @seealso \code{\link{buildHierarchy}}, \code{\link{instrument}}
+#' @examples
+#'
+#' \dontrun{
+#' currency('USD')
+#' stock('GM','USD',exchange='NYSE')
+#' stock('XOM','USD',description='Exxon Mobil')
+#' instrument.table()
+#' #Usually, currencies will not have as many attribute levels
+#' #as other instruments, so you may want to exclude them from the table.
+#' it <- instrument.table(exclude="USD|GM", attrs.of = "XOM") #columns created based on XOM instrument
+#' #it <- instrument.table(exclude=c('USD','GM'), attrs.of = "XOM") #same thing
+#' it <- instrument.table(exclude='tick_size|description|exchange')
+#' }
+#' @export
+instrument.table <- function(symbols=NULL, exclude = NULL, attrs.of = NULL) {
+#TODO check for numeric/character
+    if (is.null(symbols)) symbols <- ls(pos=.instrument, all.names=TRUE) #ls_instruments()
+    if (is.null(attrs.of)) attrs.of <- ls(pos=.instrument, all.names=TRUE) #ls_instruments()
+   
+    attr.names <- NULL
+    for (symbol in attrs.of) {
+        instr <- try(getInstrument(symbol,silent=TRUE),silent=TRUE)            
+        if (!inherits(instr,'try-error') 
+                && inherits(instr, 'instrument') ) {
+            attr.names <- unique(c(attr.names, names(unlist(instr))))               
+        }
+    }
+    if (length(exclude) > 1) 
+        exclude <- paste(exclude, collapse='|')
+    if (length(exclude == 1)) #i.e. if (!is.null(exclude))
+        attr.names <- attr.names[-grep(exclude, attr.names, ignore.case=TRUE)]
+    out <- buildHierarchy(symbols,attr.names)
+  #FIXME: doesn't work if there is only 1 symbol.
+    data.frame(out[,-1], row.names=as.character(out[,1]))
+}
+
+

Added: pkg/FinancialInstrument/man/instrument.table.Rd
===================================================================
--- pkg/FinancialInstrument/man/instrument.table.Rd	                        (rev 0)
+++ pkg/FinancialInstrument/man/instrument.table.Rd	2011-07-10 20:16:02 UTC (rev 670)
@@ -0,0 +1,29 @@
+\name{instrument.table}
+\alias{instrument.table}
+\title{Create data...}
+\usage{instrument.table(symbols, exclude, attrs.of)}
+\description{Create data.frame with attributes of all instruments}
+\details{A wrapper for \code{\link{buildHierarchy}}, that defaults to returning all attributes.
+By default it looks for the instrument with the most attribute levels, and uses those attributes
+for columns.  If you would prefer to use the attribute levels of a given instrument to build the columns, 
+use \code{attrs.of}.
+
+if there are some attributes that you do not want to be included in the returned data.frame, 
+specify them with \code{exclude}.}
+\value{data.frame}
+\author{Garrett See}
+\seealso{\code{\link{buildHierarchy}}, \code{\link{instrument}}}
+\arguments{\item{symbols}{A vector of instrument names to include}
+\item{exclude}{A vector of names of attributes that should not be included in the returned data.frame}
+\item{attrs.of}{name of a FinancialInstrument instrument. Returned data.frame columns will be the attributes of instrument.}}
+\examples{\dontrun{
+currency('USD')
+stock('GM','USD',exchange='NYSE')
+stock('XOM','USD',description='Exxon Mobil')
+instrument.table()
+#Usually, currencies will not have as many attribute levels
+#as other instruments, so you may want to exclude them from the table.
+it <- instrument.table(exclude="USD|GM", attrs.of = "XOM") #columns created based on XOM instrument
+#it <- instrument.table(exclude=c('USD','GM'), attrs.of = "XOM") #same thing
+it <- instrument.table(exclude='tick_size|description|exchange')
+}}



More information about the Blotter-commits mailing list