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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Jan 4 19:32:46 CET 2013


Author: gsee
Date: 2013-01-04 19:32:45 +0100 (Fri, 04 Jan 2013)
New Revision: 1342

Modified:
   pkg/FinancialInstrument/R/buildSpread.R
   pkg/FinancialInstrument/man/buildSpread.Rd
Log:
 * buildSpread gets data from environment specified by the "env" arg instead of hardcoding .GlobalEnv (Might want to consider deprecating this function and instead users can just make a price frame and matrix multiply by the weights which would be safer and much less code... even for the user)
* At the very least the default value for "env" will change in the near future.

Modified: pkg/FinancialInstrument/R/buildSpread.R
===================================================================
--- pkg/FinancialInstrument/R/buildSpread.R	2013-01-04 16:51:11 UTC (rev 1341)
+++ pkg/FinancialInstrument/R/buildSpread.R	2013-01-04 18:32:45 UTC (rev 1342)
@@ -23,7 +23,8 @@
 #' @param prefer Price column to use to build structure.
 #' @param auto.assign Assign the spread? If FALSE, the xts object will be 
 #'   returned.
-#' @param env Environment in which to assign spread data.
+#' @param env Environment holding data for members as well as where spread data
+#'   will be assigned.
 #' @return If \code{auto.assign} is FALSE, a univariate xts object. 
 #'   Otherwise, the xts object will be assigned to \code{spread_id} and the 
 #'   \code{spread_id} will be returned.
@@ -47,8 +48,8 @@
 #' }
 #' @rdname buildSpread
 #' @export
-buildSpread <- function(spread_id, Dates = NULL, onelot=TRUE, prefer = NULL, auto.assign=TRUE, env=.GlobalEnv) #overwrite=FALSE
-{
+buildSpread <- function(spread_id, Dates = NULL, onelot=TRUE, prefer = NULL, 
+                        auto.assign=TRUE, env=.GlobalEnv) {
     has.Mid <- quantmod:::has.Mid #FIXME: this should be exported from quatmod
     
     spread_instr <- try(getInstrument(spread_id))
@@ -71,24 +72,24 @@
       to <- times$last.time
     }
     spreadseries <- NULL
-    for (i in 1:length(spread_instr$members)) {
+    for (i in seq_len(length(spread_instr$members))) {
         instr <- try(getInstrument(as.character(spread_instr$members[i])))
         if (inherits(instr, "try-error") || !is.instrument(instr)) 
             stop(paste("Instrument", spread_instr$members[i], " not found, please create it first."))
         instr_currency <- instr$currency
         instr_mult <- as.numeric(instr$multiplier)
         instr_ratio <- spread_instr$memberratio[i]
-        instr_prices <- try(get(as.character(spread_instr$members[i]),envir=.GlobalEnv),silent=TRUE)
-        # If we were able to find instr_prices in .GlobalEnv, check to make sure there is data between from and to.
-        #if we couldn't find it in .GlobalEnv or there's no data between from and to, getSymbols
+        instr_prices <- try(get(as.character(spread_instr$members[i]),envir=env),silent=TRUE)
+        # If we were able to find instr_prices in env, check to make sure there is data between from and to.
+        #if we couldn't find it in env or there's no data between from and to, getSymbols
         if (inherits(instr_prices, "try-error") || length(instr_prices) < 2 || (!is.null(Dates) && length(instr_prices[Dates]) == 0)) {
             if (is.null(Dates)) {
-                warning(paste(spread_instr$members[i],"not found in .GlobalEnv, and no Dates supplied. Trying getSymbols defaults.") )
+                warning(paste(spread_instr$members[i],"not found in 'env', and no Dates supplied. Trying getSymbols defaults.") )
                 instr_prices <- getSymbols(as.character(spread_instr$members[i]),auto.assign=FALSE)
                 from <- first(index(instr_prices))
                 to <- last(index(instr_prices))
             } else {
-                warning(paste('Requested data for', spread_instr$members[i], 'not found in .GlobalEnv. Trying getSymbols.'))
+                warning(paste("Requested data for", spread_instr$members[i], "not found in 'env'. Trying getSymbols."))
                 instr_prices <- getSymbols(as.character(spread_instr$members[i]), from = from, to = to, auto.assign=FALSE)
             }
         }

Modified: pkg/FinancialInstrument/man/buildSpread.Rd
===================================================================
--- pkg/FinancialInstrument/man/buildSpread.Rd	2013-01-04 16:51:11 UTC (rev 1341)
+++ pkg/FinancialInstrument/man/buildSpread.Rd	2013-01-04 18:32:45 UTC (rev 1342)
@@ -28,7 +28,8 @@
   \item{auto.assign}{Assign the spread? If FALSE, the xts
   object will be returned.}
 
-  \item{env}{Environment in which to assign spread data.}
+  \item{env}{Environment holding data for members as well
+  as where spread data will be assigned.}
 }
 \value{
   If \code{auto.assign} is FALSE, a univariate xts object.



More information about the Blotter-commits mailing list