[Blotter-commits] r929 - in pkg/FinancialInstrument: . R man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Mon Feb 20 05:21:34 CET 2012
Author: gsee
Date: 2012-02-20 05:21:33 +0100 (Mon, 20 Feb 2012)
New Revision: 929
Added:
pkg/FinancialInstrument/man/update_instruments.instrument.Rd
Modified:
pkg/FinancialInstrument/DESCRIPTION
pkg/FinancialInstrument/NAMESPACE
pkg/FinancialInstrument/R/instrument.R
pkg/FinancialInstrument/R/update_instruments.yahoo.R
pkg/FinancialInstrument/man/is.currency.name.Rd
pkg/FinancialInstrument/man/is.instrument.name.Rd
pkg/FinancialInstrument/man/update_instruments.yahoo.Rd
Log:
- update_instruments.instrument FUN will use an instrument as a template to
update other instruments.
- is.instrument.name and is.currency.name accept/return vectors
- Version 0.11.2
Modified: pkg/FinancialInstrument/DESCRIPTION
===================================================================
--- pkg/FinancialInstrument/DESCRIPTION 2012-02-19 23:05:13 UTC (rev 928)
+++ pkg/FinancialInstrument/DESCRIPTION 2012-02-20 04:21:33 UTC (rev 929)
@@ -11,7 +11,7 @@
meta-data and relationships. Provides support for
multi-asset class and multi-currency portfolios. Still
in heavy development.
-Version: 0.11.1
+Version: 0.11.2
URL: https://r-forge.r-project.org/projects/blotter/
Date: $Date$
Depends:
Modified: pkg/FinancialInstrument/NAMESPACE
===================================================================
--- pkg/FinancialInstrument/NAMESPACE 2012-02-19 23:05:13 UTC (rev 928)
+++ pkg/FinancialInstrument/NAMESPACE 2012-02-20 04:21:33 UTC (rev 929)
@@ -109,6 +109,7 @@
export(synthetic.ratio)
export(synthetic)
export(to_secBATV)
+export(update_instruments.instrument)
export(update_instruments.TTR)
export(update_instruments.yahoo)
export(volep)
Modified: pkg/FinancialInstrument/R/instrument.R
===================================================================
--- pkg/FinancialInstrument/R/instrument.R 2012-02-19 23:05:13 UTC (rev 928)
+++ pkg/FinancialInstrument/R/instrument.R 2012-02-20 04:21:33 UTC (rev 929)
@@ -28,13 +28,14 @@
}
-#' test to see if a string is the name of a \code{\link{instrument}}
-#' @param x object
+#' check each element of a character vector to see if it is either the
+#' primary_id or an identifier of an \code{\link{instrument}}
+#' @param x character vector
+#' @return logical vector
#' @export
is.instrument.name <- function(x) {
if (!is.character(x)) return(FALSE)
- x <- suppressWarnings(getInstrument(x, silent=TRUE))
- inherits(x, 'instrument')
+ sapply(lapply(x, getInstrument, silent=TRUE), inherits, "instrument")
}
@@ -47,13 +48,14 @@
}
-#' test to see if a string is the name of a \code{\link{currency}}
-#' @param x character string to test.
+#' check each element of a character vector to see if it is either the
+#' primary_id or an identifier of a \code{\link{currency}}
+#' @param x character vector
#' @export
is.currency.name <- function( x ) {
if (!is.character(x)) return(FALSE)
- x <- suppressWarnings(getInstrument(x, type='currency', silent=TRUE))
- inherits( x, "currency" )
+ sapply(lapply(x, getInstrument, type='currency', silent=TRUE), inherits,
+ "currency")
}
Modified: pkg/FinancialInstrument/R/update_instruments.yahoo.R
===================================================================
--- pkg/FinancialInstrument/R/update_instruments.yahoo.R 2012-02-19 23:05:13 UTC (rev 928)
+++ pkg/FinancialInstrument/R/update_instruments.yahoo.R 2012-02-20 04:21:33 UTC (rev 929)
@@ -32,7 +32,8 @@
#' @param verbose be verbose?
#' @return called for side-effect
#' @author Garrett See
-#' @seealso \code{\link[TTR]{stockSymbols}}, \code{\link{stock}}
+#' @seealso \code{\link{update_instruments.instrument}},
+#' \code{\link[TTR]{stockSymbols}}, \code{\link{stock}}
#' @references Yahoo! Finance \url{finance.yahoo.com} YahooQuote
#' \url{http://dirk.eddelbuettel.com/code/yahooquote.html}
#' gummy-stuff.org \url{www.gummy-stuff.org/Yahoo-data.htm}
@@ -168,4 +169,101 @@
}
+#' Update instruments with metadata from another instrument.
+#'
+#' Update instruments with metadata from another instrument.
+#'
+#' By default, only attributes that have a value of \code{""} will be given a
+#' new value.
+#'
+#' If \code{create.new} is \code{TRUE}, then if there are attributes in
+#' \code{source_id} that are not in the \code{Symbols}' instrument, those
+#' attributes will be copied to the updated instruments unless they are in
+#' \code{ignore}.
+#'
+#' @param Symbols charcter vector of primary_ids or other instrument identifiers.
+#' of instruments to be updated.
+#' @param source_id The primary_id (or other identifier) of an instrument, or
+#' an instrument. The \code{source_id} instrument will be used to update the
+#' metadata of \code{Symbols}' instruments.
+#' @param create.new If FALSE (Default), only attributes that exist but have
+#' empty values will be updated. If TRUE, new attributes will be created if
+#' \code{source_id} has them, but the \code{Symbols} do not.
+#' @param ignore vector of names of instrument attributes that should not be
+#' copied to the updated instruments.
+#' @param assign_i TRUE/FALSE. If TRUE, the updated instruments will be assigned
+#' back into the instrument environment. If FALSE, a list of updated
+#' instruments will be returned
+#' @return if \code{isTRUE(assign_i)} a vector of primary_ids of the instruments
+#' that were upated. Otherwise, a list of updated instrument objects.
+#' @author Garrett See
+#' @seealso \code{\link{update_instruments.yahoo}},
+#' \code{\link{all.equal.instrument}}
+#' @note one way to overwrite attributes of one instrument with those of another
+#' is to first set equal to \code{""} those attributes that you want to
+#' overwrite, then use \code{update_instruments.instrument} to copy the
+#' attributes.
+#' @examples
+#' \dontrun{
+#' #rm_instruments()
+#' currency("USD")
+#' synthetic("SPX", "USD", identifiers=list(yahoo="GSPC"),
+#' tick_size=0.01,
+#' liquidHours="T08:30:00/T15:00:00",
+#' extraField='something else',
+#' assign_i=TRUE)
+#' stock("SPY", "USD", liquidHours="", assign_i=TRUE)
+#' all.equal(getInstrument("SPX"), getInstrument("SPY"))
+#' getInstrument("SPY")
+#' ## update SPY metadata based on the metadata of SPX
+#' ## Only attributes that == "" are updated by default
+#' update_instruments.instrument("SPY", "SPX", assign_i=FALSE) #liquidHours
+#' update_instruments.instrument("SPY", "SPX", create.new=TRUE,
+#' ignore=c("identifiers", "type"),
+#' assign_i=FALSE)
+#' # Although you probably do NOT want to, this will
+#' # copy everything new -- including identifiers and type!
+#' update_instruments.instrument("SPY", "SPX", create.new=TRUE, ignore=NULL,
+#' assign_i=FALSE)
+#' }
+#' @export
+update_instruments.instrument <- function(Symbols, source_id, create.new=FALSE,
+ ignore="identifiers", assign_i=TRUE) {
+ r <- if (is.instrument(source_id)) { source_id } else getInstrument(source_id)
+ if (!is.instrument(r)) {
+ stop('source_id is neither an instrument nor the name of an instrument')
+ }
+
+ out <- lapply(Symbols, function(s) {
+ si <- getInstrument(s)
+ if (!is.instrument(si)) {
+ warning(paste('could not find instrument"', s, '"Skipping...'))
+ next
+ }
+ all.empty <- do.call(c, lapply(si, function(x) all(x == "")))
+ all.empty <- all.empty[!names(all.empty) %in% ignore]
+
+ names.empty <- names(all.empty[all.empty])
+ for (n in names.empty) {
+ if (!is.null(r[[n]])) {
+ si[[n]] <- r[[n]]
+ }
+ }
+ if (isTRUE(create.new)) {
+ nr <- names(r)
+ nr <- nr[!nr %in% ignore]
+ nsi <- names(si)
+ new.attr <- nr[!nr %in% nsi]
+ for (n in new.attr) {
+ si[[n]] <- r[[n]]
+ }
+ }
+ si
+ })
+ if (isTRUE(assign_i)) {
+ invisible(lapply(out, function(x) assign(x$primary_id, x,
+ pos=FinancialInstrument:::.instrument)))
+ } else return(out)
+ sapply(out, "[[", "primary_id")
+}
Modified: pkg/FinancialInstrument/man/is.currency.name.Rd
===================================================================
--- pkg/FinancialInstrument/man/is.currency.name.Rd 2012-02-19 23:05:13 UTC (rev 928)
+++ pkg/FinancialInstrument/man/is.currency.name.Rd 2012-02-20 04:21:33 UTC (rev 929)
@@ -1,14 +1,16 @@
\name{is.currency.name}
\alias{is.currency.name}
-\title{test to see if a string is the name of a \code{\link{currency}}}
+\title{check each element of a character vector to see if it is either the
+primary_id or an identifier of a \code{\link{currency}}}
\usage{
is.currency.name(x)
}
\arguments{
- \item{x}{character string to test.}
+ \item{x}{character vector}
}
\description{
- test to see if a string is the name of a
+ check each element of a character vector to see if it is
+ either the primary_id or an identifier of a
\code{\link{currency}}
}
Modified: pkg/FinancialInstrument/man/is.instrument.name.Rd
===================================================================
--- pkg/FinancialInstrument/man/is.instrument.name.Rd 2012-02-19 23:05:13 UTC (rev 928)
+++ pkg/FinancialInstrument/man/is.instrument.name.Rd 2012-02-20 04:21:33 UTC (rev 929)
@@ -1,14 +1,19 @@
\name{is.instrument.name}
\alias{is.instrument.name}
-\title{test to see if a string is the name of a \code{\link{instrument}}}
+\title{check each element of a character vector to see if it is either the
+primary_id or an identifier of an \code{\link{instrument}}}
\usage{
is.instrument.name(x)
}
\arguments{
- \item{x}{object}
+ \item{x}{character vector}
}
+\value{
+ logical vector
+}
\description{
- test to see if a string is the name of a
+ check each element of a character vector to see if it is
+ either the primary_id or an identifier of an
\code{\link{instrument}}
}
Added: pkg/FinancialInstrument/man/update_instruments.instrument.Rd
===================================================================
--- pkg/FinancialInstrument/man/update_instruments.instrument.Rd (rev 0)
+++ pkg/FinancialInstrument/man/update_instruments.instrument.Rd 2012-02-20 04:21:33 UTC (rev 929)
@@ -0,0 +1,87 @@
+\name{update_instruments.instrument}
+\alias{update_instruments.instrument}
+\title{Update instruments with metadata from another instrument.}
+\usage{
+ update_instruments.instrument(Symbols, source_id,
+ create.new = FALSE, ignore = "identifiers",
+ assign_i = TRUE)
+}
+\arguments{
+ \item{Symbols}{charcter vector of primary_ids or other
+ instrument identifiers. of instruments to be updated.}
+
+ \item{source_id}{The primary_id (or other identifier) of
+ an instrument, or an instrument. The \code{source_id}
+ instrument will be used to update the metadata of
+ \code{Symbols}' instruments.}
+
+ \item{create.new}{If FALSE (Default), only attributes
+ that exist but have empty values will be updated. If
+ TRUE, new attributes will be created if \code{source_id}
+ has them, but the \code{Symbols} do not.}
+
+ \item{ignore}{vector of names of instrument attributes
+ that should not be copied to the updated instruments.}
+
+ \item{assign_i}{TRUE/FALSE. If TRUE, the updated
+ instruments will be assigned back into the instrument
+ environment. If FALSE, a list of updated instruments
+ will be returned}
+}
+\value{
+ if \code{isTRUE(assign_i)} a vector of primary_ids of the
+ instruments that were upated. Otherwise, a list of
+ updated instrument objects.
+}
+\description{
+ Update instruments with metadata from another instrument.
+}
+\details{
+ By default, only attributes that have a value of
+ \code{""} will be given a new value.
+
+ If \code{create.new} is \code{TRUE}, then if there are
+ attributes in \code{source_id} that are not in the
+ \code{Symbols}' instrument, those attributes will be
+ copied to the updated instruments unless they are in
+ \code{ignore}.
+}
+\note{
+ one way to overwrite attributes of one instrument with
+ those of another is to first set equal to \code{""} those
+ attributes that you want to overwrite, then use
+ \code{update_instruments.instrument} to copy the
+ attributes.
+}
+\examples{
+\dontrun{
+#rm_instruments()
+currency("USD")
+synthetic("SPX", "USD", identifiers=list(yahoo="GSPC"),
+ tick_size=0.01,
+ liquidHours="T08:30:00/T15:00:00",
+ extraField='something else',
+ assign_i=TRUE)
+stock("SPY", "USD", liquidHours="", assign_i=TRUE)
+all.equal(getInstrument("SPX"), getInstrument("SPY"))
+getInstrument("SPY")
+## update SPY metadata based on the metadata of SPX
+## Only attributes that == "" are updated by default
+update_instruments.instrument("SPY", "SPX", assign_i=FALSE) #liquidHours
+update_instruments.instrument("SPY", "SPX", create.new=TRUE,
+ ignore=c("identifiers", "type"),
+ assign_i=FALSE)
+# Although you probably do NOT want to, this will
+# copy everything new -- including identifiers and type!
+update_instruments.instrument("SPY", "SPX", create.new=TRUE, ignore=NULL,
+ assign_i=FALSE)
+}
+}
+\author{
+ Garrett See
+}
+\seealso{
+ \code{\link{update_instruments.yahoo}},
+ \code{\link{all.equal.instrument}}
+}
+
Modified: pkg/FinancialInstrument/man/update_instruments.yahoo.Rd
===================================================================
--- pkg/FinancialInstrument/man/update_instruments.yahoo.Rd 2012-02-19 23:05:13 UTC (rev 928)
+++ pkg/FinancialInstrument/man/update_instruments.yahoo.Rd 2012-02-20 04:21:33 UTC (rev 929)
@@ -55,6 +55,7 @@
gummy-stuff.org \url{www.gummy-stuff.org/Yahoo-data.htm}
}
\seealso{
+ \code{\link{update_instruments.instrument}},
\code{\link[TTR]{stockSymbols}}, \code{\link{stock}}
}
More information about the Blotter-commits
mailing list