[Blotter-commits] r967 - in pkg/FinancialInstrument: . R
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Thu Mar 8 03:39:23 CET 2012
Author: gsee
Date: 2012-03-08 03:39:22 +0100 (Thu, 08 Mar 2012)
New Revision: 967
Added:
pkg/FinancialInstrument/R/find.instrument.R
Modified:
pkg/FinancialInstrument/DESCRIPTION
pkg/FinancialInstrument/NAMESPACE
Log:
add find.instrument function that will search for text in instruments and return the
primary_ids of the instruments containing the search string
Modified: pkg/FinancialInstrument/DESCRIPTION
===================================================================
--- pkg/FinancialInstrument/DESCRIPTION 2012-03-07 20:40:36 UTC (rev 966)
+++ pkg/FinancialInstrument/DESCRIPTION 2012-03-08 02:39:22 UTC (rev 967)
@@ -11,7 +11,7 @@
meta-data and relationships. Provides support for
multi-asset class and multi-currency portfolios. Still
in heavy development.
-Version: 0.12.5
+Version: 0.12.6
URL: https://r-forge.r-project.org/projects/blotter/
Date: $Date$
Depends:
@@ -48,3 +48,4 @@
'Tick2Sec.R'
'all.equal.instrument.R'
'expires.R'
+ 'find.instrument.R'
Modified: pkg/FinancialInstrument/NAMESPACE
===================================================================
--- pkg/FinancialInstrument/NAMESPACE 2012-03-07 20:40:36 UTC (rev 966)
+++ pkg/FinancialInstrument/NAMESPACE 2012-03-08 02:39:22 UTC (rev 967)
@@ -12,6 +12,7 @@
export(currency)
export(exchange_rate)
export(expires)
+export(find.instrument)
export(fn_SpreadBuilder)
export(format_id)
export(formatSpreadPrice)
Added: pkg/FinancialInstrument/R/find.instrument.R
===================================================================
--- pkg/FinancialInstrument/R/find.instrument.R (rev 0)
+++ pkg/FinancialInstrument/R/find.instrument.R 2012-03-08 02:39:22 UTC (rev 967)
@@ -0,0 +1,65 @@
+###############################################################################
+# R (http://r-project.org/) Instrument Class Model
+#
+# Copyright (c) 2009-2012
+# Peter Carl, Dirk Eddelbuettel, Jeffrey Ryan,
+# Joshua Ulrich, Brian G. Peterson, and Garrett See
+#
+# This library is distributed under the terms of the GNU Public License (GPL)
+# for full details see the file COPYING
+#
+# $Id$
+#
+###############################################################################
+
+
+#' Find the primary_ids of instruments that contain certain
+#' strings
+#'
+#' Uses regular expressions matching to find \code{\link{instrument}}s
+#'
+#' @param text string to search for
+#' @param where if \dQuote{anywhere} all levels/attributes of the instruments
+#' will be searched. Otherwise, \code{where} can be used to specify in which
+#' levels/attributes to look. (e.g. \code{c("name", "description")} would only
+#' look for \code{text} in those 2 places.
+#' @param Symbols the character ids of instruments to be searched. All are
+#' are searched by default.
+#' @param ignore.case passed to \code{\link{grep}}; if \code{FALSE}, the pattern
+#' matching is case sensitive and if \code{TRUE}, case is ignored during
+#' matching.
+#' @param ... other arguments to pass throught to \code{\link{grep}}
+#' @return character vector of primary_ids of instruments that contain the
+#' sought after \code{text}.
+#' @author Garrett See
+#' @seealso \code{\link{buildHierarchy}}, \code{\link{instrument.table}},
+#' \code{\link{regex}}
+#' @examples
+#' \dontrun{
+#' instruments.bak <- as.list(FinancialInstrument:::.instrument)
+#' rm_instruments(keep.currencies=FALSE)
+#' currency("USD")
+#' stock("SPY", "USD", description="S&P 500 ETF")
+#' stock("DIA", "USD", description="DJIA ETF")
+#' stock(c("AA", "AXP", "BA", "BAC", "CAT"), "USD", members.of='DJIA')
+#' stock("BMW", currency("EUR"))
+#' find.instrument("ETF")
+#' find.instrument("DJIA")
+#' find.instrument("DJIA", "members.of")
+#' find.instrument("USD")
+#' find.instrument("EUR")
+#' find.instrument("EUR", Symbols=ls_stocks())
+#' find.instrument("USD", "type")
+#' ## Cleanup. restore previous instrument environment
+#' rm_instruments(); rm_currencies()
+#' loadInstruments(instruments.bak)
+#' }
+#' @export
+find.instrument <- function(text, where='anywhere', Symbols = ls_instruments(),
+ ignore.case=TRUE, ...) {
+ tbl <- if (where == "anywhere") {
+ instrument.table(Symbols)
+ } else buildHierarchy(Symbols, where)
+ tbl[unique(unname(unlist(apply(tbl, 2, function(x)
+ grep(pattern=text, x=x, ignore.case=ignore.case, ...))))), 1]
+}
Property changes on: pkg/FinancialInstrument/R/find.instrument.R
___________________________________________________________________
Added: svn:keywords
+ Revision Id Date Author
More information about the Blotter-commits
mailing list