[Blotter-commits] r1391 - in pkg/FinancialInstrument: . R inst/tests man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sun Feb 10 21:19:02 CET 2013


Author: gsee
Date: 2013-02-10 21:19:02 +0100 (Sun, 10 Feb 2013)
New Revision: 1391

Added:
   pkg/FinancialInstrument/inst/tests/test-redenominate.R
Modified:
   pkg/FinancialInstrument/DESCRIPTION
   pkg/FinancialInstrument/NEWS
   pkg/FinancialInstrument/R/redenominate.R
   pkg/FinancialInstrument/man/redenominate.Rd
Log:
 - redenominate will use old_base if provided; instrument's currency otherwise 
   (instead of always using instrument's currency)
 - updated docs and NEWS
 - added tests


Modified: pkg/FinancialInstrument/DESCRIPTION
===================================================================
--- pkg/FinancialInstrument/DESCRIPTION	2013-02-07 17:26:39 UTC (rev 1390)
+++ pkg/FinancialInstrument/DESCRIPTION	2013-02-10 20:19:02 UTC (rev 1391)
@@ -9,9 +9,9 @@
 Contributors: Dirk Eddelbuettel, Alexis Petit, Jeffrey Ryan, Joshua Ulrich
 Description: Infrastructure for defining meta-data and
     relationships for financial instruments.
-Version: 1.1.4
+Version: 1.1.5
 URL: https://r-forge.r-project.org/projects/blotter/
-Date: 2013-02-02
+Date: 2013-02-10
 Depends:
     R (>= 2.12.0),
     quantmod(>= 0.3-17),

Modified: pkg/FinancialInstrument/NEWS
===================================================================
--- pkg/FinancialInstrument/NEWS	2013-02-07 17:26:39 UTC (rev 1390)
+++ pkg/FinancialInstrument/NEWS	2013-02-10 20:19:02 UTC (rev 1391)
@@ -11,6 +11,10 @@
   getSymbols.FI now always appends the Symbol to the file path and 
   setSymbolLookup.FI does not append it to the base_dir. Test added.
 
+* In redenominate, if the object was a defined instrument, the currency in the
+  instrument object would be used even if the user passed a different currency
+  in the old_base argument.  
+
 TESTS
 -----
 

Modified: pkg/FinancialInstrument/R/redenominate.R
===================================================================
--- pkg/FinancialInstrument/R/redenominate.R	2013-02-07 17:26:39 UTC (rev 1390)
+++ pkg/FinancialInstrument/R/redenominate.R	2013-02-10 20:19:02 UTC (rev 1391)
@@ -280,9 +280,10 @@
 #'
 #' Redenominate (change the base of) an instrument
 #'
-#' If \code{x} is the name of an instrument, old_base is not required 
-#' and will become whatever is in the currency slot of the instrument.  
-#' Otherwise, old_base must be provided.
+#' If \code{old_base} is not provided, \code{x} must be the name of an 
+#' instrument (or an object with the name of a defined instrument) so that the
+#' currency attribute of the instrument can be used.  Otherwise, \code{old_base}
+#' must be provided.
 #'
 #' If you want to convert to JPY something that is denominated in EUR,
 #' you must have data for the EURJPY (or JPYEUR) exchange rate. If you don't have
@@ -327,7 +328,7 @@
             if (is.null(old_base)) stop(paste("If old_base is not provided, ", Symbol, ' must be defined.', sep=""))
             mult <- 1        
         } else {
-            old_base <- instr$currency
+            if (is.null(old_base)) old_base <- instr$currency
             mult <- as.numeric(instr$multiplier)    
         }
         if (is.character(x)) x <- get(Symbol,pos=env)

Added: pkg/FinancialInstrument/inst/tests/test-redenominate.R
===================================================================
--- pkg/FinancialInstrument/inst/tests/test-redenominate.R	                        (rev 0)
+++ pkg/FinancialInstrument/inst/tests/test-redenominate.R	2013-02-10 20:19:02 UTC (rev 1391)
@@ -0,0 +1,45 @@
+context("redenominate")
+
+.dat <- new.env()
+
+currency(c("USD", "CCY"))
+exchange_rate("USDCCY")
+stock("xus", "USD")
+stock("xcy", "CCY")
+
+with(.dat, {
+  xus <- xts(1:5, as.Date("2010-01-01")-5:1) 
+  xcy <- xts(c(40, 45), as.Date("2010-01-01")-2:1)
+  USDCCY <- xts(c(10, 11, 9, 10, 12), as.Date("2010-01-01")-5:1)
+})
+
+test_that("convert TO USD", {
+  expect_equal(as.numeric(redenominate("xcy", "USD", env=.dat)), c(4.00, 3.75))
+})
+test_that("convert FROM USD", {
+  expect_equal(as.numeric(redenominate("xus", "CCY", env=.dat)), c(10, 22, 27, 40, 60))
+})
+test_that("xts instead of name", {
+  expect_equal(as.numeric(redenominate(.dat$xcy, "USD", "CCY", env=.dat)), c(4.00, 3.75))
+})
+test_that("uses currency from instrument if old_base is missing", {
+  expect_equal(as.numeric(redenominate("xus", "CCY", env=.dat)), c(10, 22, 27, 40, 60))
+})
+test_that("provided old_base overrides instrument", {
+  expect_equal(as.numeric(redenominate("xus", "CCY", "USD", env=.dat)), c(10, 22, 27, 40, 60))
+})
+test_that("no instrument defined", {
+  rm_instruments("xus")
+  expect_equal(as.numeric(redenominate("xus", "CCY", "USD", env=.dat)), c(10, 22, 27, 40, 60))
+  expect_error(redenominate("xus", "CCY", env=.dat), "old_base is not provided")
+})
+test_that("inverts FX if necessary", {
+  with.usdccy <- redenominate("xus", "CCY", "USD", env=.dat)
+  with(.dat, {
+    CCYUSD <- 1 / USDCCY
+    rm(USDCCY)
+  })
+  expect_equal(with.usdccy, redenominate("xus", "CCY", "USD", env=.dat))
+})
+
+# TODO: test mixing intraday and daily data
\ No newline at end of file

Modified: pkg/FinancialInstrument/man/redenominate.Rd
===================================================================
--- pkg/FinancialInstrument/man/redenominate.Rd	2013-02-07 17:26:39 UTC (rev 1390)
+++ pkg/FinancialInstrument/man/redenominate.Rd	2013-02-10 20:19:02 UTC (rev 1391)
@@ -31,9 +31,11 @@
   Redenominate (change the base of) an instrument
 }
 \details{
-  If \code{x} is the name of an instrument, old_base is not
-  required and will become whatever is in the currency slot
-  of the instrument. Otherwise, old_base must be provided.
+  If \code{old_base} is not provided, \code{x} must be the
+  name of an instrument (or an object with the name of a
+  defined instrument) so that the currency attribute of the
+  instrument can be used.  Otherwise, \code{old_base} must
+  be provided.
 
   If you want to convert to JPY something that is
   denominated in EUR, you must have data for the EURJPY (or



More information about the Blotter-commits mailing list