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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Jul 9 16:59:51 CEST 2012


Author: gsee
Date: 2012-07-09 16:59:51 +0200 (Mon, 09 Jul 2012)
New Revision: 1102

Added:
   pkg/FinancialInstrument/R/CompareInstrumentFiles.R
   pkg/FinancialInstrument/man/CompareInstrumentFiles.Rd
Modified:
   pkg/FinancialInstrument/DESCRIPTION
   pkg/FinancialInstrument/NAMESPACE
Log:
 add CompareInstrumentFiles fun to see the diffs between 2 .instrument environments.

Modified: pkg/FinancialInstrument/DESCRIPTION
===================================================================
--- pkg/FinancialInstrument/DESCRIPTION	2012-07-06 13:46:05 UTC (rev 1101)
+++ pkg/FinancialInstrument/DESCRIPTION	2012-07-09 14:59:51 UTC (rev 1102)
@@ -55,3 +55,4 @@
     'Notionalize.R'
     'update_instruments.iShares.R'
     'update_instruments.morningstar.R'
+    'CompareInstrumentFiles.R'

Modified: pkg/FinancialInstrument/NAMESPACE
===================================================================
--- pkg/FinancialInstrument/NAMESPACE	2012-07-06 13:46:05 UTC (rev 1101)
+++ pkg/FinancialInstrument/NAMESPACE	2012-07-09 14:59:51 UTC (rev 1102)
@@ -10,6 +10,7 @@
 export(build_spread_symbols)
 export(butterfly)
 export(C2M)
+export(CompareInstrumentFiles)
 export(currency)
 export(Denotionalize)
 export(exchange_rate)

Added: pkg/FinancialInstrument/R/CompareInstrumentFiles.R
===================================================================
--- pkg/FinancialInstrument/R/CompareInstrumentFiles.R	                        (rev 0)
+++ pkg/FinancialInstrument/R/CompareInstrumentFiles.R	2012-07-09 14:59:51 UTC (rev 1102)
@@ -0,0 +1,87 @@
+#' Compare Instrument Files
+#'
+#' Compare the .instrument environments of two files
+#'
+#' This will load 2 instrument files (created by \code{\link{saveInstruments}})
+#' and find the differences between them.  It will produce messages 
+#' indicating the number of instruments that were added, the number of 
+#' instruments that were removed, and the number of instruments that are 
+#' different
+#'
+#' @param file1 file containing an instrument environment
+#' @param file2 another file containing an instrument environment
+#' @param ... arguments to pass to \code{\link{all.equal.instrument}}
+#' @return A list that contains the names of all instruments that were added,
+#'   the names of all instruments that were removed, and the changes to all
+#'   instruments that were updated (per \code{\link{all.equal.instrument}}).
+#' @author Garrett See
+#' @seealso \code{\link{saveInstruments}}, \code{\link{all.equal.instrument}}
+#' @examples
+#backup current .instrument environment
+#' bak <- as.list(FinancialInstrument:::.instrument, all.names=TRUE) 
+#' old.wd <- getwd()
+#' tmpdir <- tempdir()
+#' setwd(tmpdir)
+#' rm_instruments(keep=FALSE)
+#' # create some instruments and save
+#' stock(c("SPY", "DIA", "GLD"), currency("USD"))
+#' saveInstruments("MyInstruments1")
+#' # make some changes
+#' rm_stocks("GLD")
+#' stock("QQQ", "USD")
+#' instrument_attr("SPY", "description", "S&P ETF")
+#' saveInstruments("MyInstruments2")
+#' CompareInstrumentFiles("MyInstruments1", "MyInstruments2")
+#' #Clean up
+#' setwd(old.wd)
+#' reloadInstruments(bak)
+#' @export
+CompareInstrumentFiles <- function(file1, file2, ...) {
+    stopifnot(require("FinancialInstrument"))
+    #backup current instrument environment
+    bak <- as.list(FinancialInstrument:::.instrument, all.names=TRUE)
+    # load files to be compared
+    reloadInstruments(file1)
+    orig <- as.list(FinancialInstrument:::.instrument, all.names=TRUE)
+    reloadInstruments(file2)
+    new <-  as.list(FinancialInstrument:::.instrument, all.names=TRUE)
+    #restore user's instrument environment
+    reloadInstruments(bak)
+    new.instruments <- names(new)[!names(new) %in% names(orig)]
+    removed.instruments <- names(orig)[!names(orig) %in% names(new)]
+    lni <- length(new.instruments)
+    if (lni == 1L) { # grammar
+        message(paste("1 new instrument."))
+    } else {
+        message(paste(lni, "new instruments."))
+    }
+    lri <- length(removed.instruments)
+    if (lri == 1L) { #grammar
+        message(paste("1 instrument removed."))
+    } else {
+        message(paste(lri, "instruments removed."))
+    }
+    # now look at changes of those that both have in common
+    to.comp <- names(new)[names(new) %in% names(orig)]
+    diffs <- lapply(to.comp, function(x) {
+        ae <- all.equal(orig[[x]], new[[x]], ...)
+        if (!isTRUE(ae)) ae
+    }) #took about 11 seconds for me on fast computer with 7500 instruments
+    names(diffs) <- to.comp
+    diffs <- diffs[!vapply(diffs, is.null, logical(1))]
+    liu <- length(diffs)
+    if (liu == 1L) { #grammar
+        message(paste("1 instrument updated."))
+    } else {
+        message(paste(liu, "instruments updated."))
+    }
+    out <- c(list(new.instruments=new.instruments, 
+                  removed.instruments=removed.instruments),
+             diffs)
+    out <- Filter(function(x) length(x) > 0L, out)
+    if (length(out) > 0L) {
+        out
+    }
+}
+
+

Added: pkg/FinancialInstrument/man/CompareInstrumentFiles.Rd
===================================================================
--- pkg/FinancialInstrument/man/CompareInstrumentFiles.Rd	                        (rev 0)
+++ pkg/FinancialInstrument/man/CompareInstrumentFiles.Rd	2012-07-09 14:59:51 UTC (rev 1102)
@@ -0,0 +1,59 @@
+\name{CompareInstrumentFiles}
+\alias{CompareInstrumentFiles}
+\title{Compare Instrument Files}
+\usage{
+  CompareInstrumentFiles(file1, file2, ...)
+}
+\arguments{
+  \item{file1}{file containing an instrument environment}
+
+  \item{file2}{another file containing an instrument
+  environment}
+
+  \item{...}{arguments to pass to
+  \code{\link{all.equal.instrument}}}
+}
+\value{
+  A list that contains the names of all instruments that
+  were added, the names of all instruments that were
+  removed, and the changes to all instruments that were
+  updated (per \code{\link{all.equal.instrument}}).
+}
+\description{
+  Compare the .instrument environments of two files
+}
+\details{
+  This will load 2 instrument files (created by
+  \code{\link{saveInstruments}}) and find the differences
+  between them.  It will produce messages indicating the
+  number of instruments that were added, the number of
+  instruments that were removed, and the number of
+  instruments that are different
+}
+\examples{
+bak <- as.list(FinancialInstrument:::.instrument, all.names=TRUE)
+old.wd <- getwd()
+tmpdir <- tempdir()
+setwd(tmpdir)
+rm_instruments(keep=FALSE)
+# create some instruments and save
+stock(c("SPY", "DIA", "GLD"), currency("USD"))
+saveInstruments("MyInstruments1")
+# make some changes
+rm_stocks("GLD")
+stock("QQQ", "USD")
+instrument_attr("SPY", "description", "S&P ETF")
+saveInstruments("MyInstruments2")
+CompareInstrumentFiles("MyInstruments1", "MyInstruments2")
+#Clean up
+setwd(old.wd)
+reloadInstruments(bak)
+}
+\author{
+  Garrett See
+}
+\seealso{
+  \code{\link{saveInstruments}},
+  \code{\link{all.equal.instrument}}
+}
+



More information about the Blotter-commits mailing list