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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Sep 30 10:51:53 CEST 2011


Author: gsee
Date: 2011-09-30 10:51:52 +0200 (Fri, 30 Sep 2011)
New Revision: 807

Modified:
   pkg/FinancialInstrument/DESCRIPTION
   pkg/FinancialInstrument/R/instrument.R
   pkg/FinancialInstrument/man/instrument.Rd
   pkg/FinancialInstrument/sandbox/ISO.currencies.wiki.R
Log:
 - currency function had arguments that weren't being used/were being overwritten. (currency and multiplier)
 - if you try to define an already defined currency, it will be updated instead of completely overwritten.
 - when instrument.auto defines something with a yahoo index ticker (e.g. ^GSPC), that ticker will be added as an identifier
 - add define_currencies.oanda to sandbox, and improve define_currencies.wiki.


Modified: pkg/FinancialInstrument/DESCRIPTION
===================================================================
--- pkg/FinancialInstrument/DESCRIPTION	2011-09-29 00:09:05 UTC (rev 806)
+++ pkg/FinancialInstrument/DESCRIPTION	2011-09-30 08:51:52 UTC (rev 807)
@@ -11,7 +11,7 @@
     meta-data and relationships. Provides support for
     multi-asset class and multi-currency portfolios.  
     Still in heavy development.
-Version: 0.7.2
+Version: 0.7.3
 URL: https://r-forge.r-project.org/projects/blotter/
 Date: $Date$
 Depends:

Modified: pkg/FinancialInstrument/R/instrument.R
===================================================================
--- pkg/FinancialInstrument/R/instrument.R	2011-09-29 00:09:05 UTC (rev 806)
+++ pkg/FinancialInstrument/R/instrument.R	2011-09-30 08:51:52 UTC (rev 807)
@@ -524,19 +524,35 @@
 
 #' @export
 #' @rdname instrument
-currency <- function(primary_id , currency=NULL , multiplier=1 , identifiers = NULL, ...){
-  if (length(primary_id) > 1) return(unname(sapply(primary_id, currency, identifiers=identifiers, ...=...)))
-  currency_temp <- list(primary_id = primary_id,
-          currency = primary_id,
-          multiplier = 1,
-          tick_size= .01,
-          identifiers = identifiers,
-          type = "currency"
-  )
-  currency_temp <- c(currency_temp,list(...))   
-  class(currency_temp)<-c("currency","instrument")
-  assign(primary_id, currency_temp, envir=as.environment(.instrument) )
-  primary_id
+currency <- function(primary_id, identifiers = NULL, ...){
+    if (length(primary_id) > 1) return(unname(sapply(primary_id, currency, identifiers=identifiers, ...=...)))
+    ccy <- try(getInstrument(primary_id,type='currency',silent=TRUE))
+    if (is.instrument(ccy)) {
+        if (!is.null(identifiers)) {
+            if (!is.list(identifiers)) identifiers <- list(identifiers)
+            for (nm in names(ccy$identifiers)[names(ccy$identifiers) %in% names(identifiers)]) {
+                ccy$identifiers[[nm]] <- identifiers[[nm]]
+            }
+            identifiers <- identifiers[names(identifiers)[!names(identifiers) %in% names(ccy$identifiers)]]
+            ccy$identifiers <- c(identifiers, ccy$identifiers)
+        }
+    } else ccy <- list(primary_id = primary_id,
+                        currency = primary_id,
+                        multiplier = 1,
+                        tick_size= .01,
+                        identifiers = identifiers,
+                        type = "currency")
+    dargs <- list(...)
+    if (!is.null(dargs)) {
+        for (nm in names(ccy)[names(ccy) %in% names(dargs)]) {
+            ccy[[nm]] <- dargs[[nm]]
+        }
+        dargs <- dargs[names(dargs)[!names(dargs) %in% names(ccy)]]
+        ccy <- c(ccy,dargs)
+    }        
+    class(ccy)<-c("currency","instrument")
+    assign(primary_id, ccy, pos=as.environment(.instrument) )
+    primary_id
 }
 
 #' class test for object supposedly of type 'currency'
@@ -762,7 +778,8 @@
     }
     if (any(pid$type == 'synthetic')) {
         if (!is.na(pid$format) && pid$format == 'yahooIndex') {
-            return(synthetic(gsub("\\^","",primary_id), currency=currency, src=list(src='yahoo',name=primary_id), defined_by='auto', ...))
+            return(synthetic(gsub("\\^","",primary_id), currency=currency, identifiers=list(yahoo=primary_id), 
+                            src=list(src='yahoo',name=primary_id), defined_by='auto', ...))
         } else return(synthetic(members=strsplit(primary_id,"\\.")[[1]], currency=currency, defined.by='auto', ...) )
     } 
     ss <- strsplit(primary_id," ")[[1]]  #take out spaces (OSI uses spaces, but makenames would turn them into dots)

Modified: pkg/FinancialInstrument/man/instrument.Rd
===================================================================
--- pkg/FinancialInstrument/man/instrument.Rd	2011-09-29 00:09:05 UTC (rev 806)
+++ pkg/FinancialInstrument/man/instrument.Rd	2011-09-30 08:51:52 UTC (rev 807)
@@ -24,8 +24,7 @@
   option(primary_id, currency, multiplier, tick_size =
   NULL, identifiers = NULL, ..., underlying_id = NULL)
 
-  currency(primary_id, currency = NULL, multiplier = 1,
-  identifiers = NULL, ...)
+  currency(primary_id, identifiers = NULL, ...)
 
   bond(primary_id, currency, multiplier, tick_size = NULL,
   identifiers = NULL, ...)

Modified: pkg/FinancialInstrument/sandbox/ISO.currencies.wiki.R
===================================================================
--- pkg/FinancialInstrument/sandbox/ISO.currencies.wiki.R	2011-09-29 00:09:05 UTC (rev 806)
+++ pkg/FinancialInstrument/sandbox/ISO.currencies.wiki.R	2011-09-30 08:51:52 UTC (rev 807)
@@ -1,3 +1,30 @@
+#' Define currencies using the tables found on oanda's website.
+#'
+#' If you do not provide \code{Symbols} all oanda curriencies will be defined.
+#' If you do provide \code{Symbols} only the Symbols you provided will be defined. 
+#' @param Symbols
+#' @param silent
+#' @return the names of the currecies that were defined.  Called for side-effect
+#' @references \url{http://www.oanda.com/help/currency-iso-code}
+#' @author Garrett See
+#' @examples
+#' \dontrun{
+#' define_currencies.oanda(c("EUR","GBP","JPY"))
+#' define_currencies.oanda()
+#' }
+define_currencies.oanda <- function(Symbols, silent=FALSE) {
+    if (!("package:XML" %in% search() || require("XML",quietly=TRUE))) 
+        stop("Please install the XML package before using this function.")
+    x <- readHTMLTable("http://www.oanda.com/help/currency-iso-code")
+    x <- lapply(x, function(xx) xx[-1,])
+    #all.syms <- unname(do.call(c,lapply(x, function(xx) as.character(xx[,1]))))
+    df <- do.call(rbind, lapply(x, function(xx) cbind(as.character(xx[,1]), as.character(xx[,2]))))
+    if(missing(Symbols)) Symbols <- df[,1]
+    df <- df[df[,1] %in% Symbols,]
+    apply(df,1,function(X) currency(X[1], description=X[2],defined.by='oanda'))
+}
+
+
 #http://en.wikipedia.org/wiki/ISO_4217#Active_codes
 
 #' Define currency instruments using the tables found on the ISO_4217 wikipedia page
@@ -27,7 +54,8 @@
         ccy <- rbind(ccy, x[[3]]) #add non-ISO... things like Bitcoin
         ccy <- ccy[ccy$Code %in% Symbols,]
     } else Symbols <- NULL
-    out <- unname(apply(ccy, 1, function(xx) currency(xx[1], description=xx[4], country=xx[5], defined.by='wiki')))
+    out <- unname(apply(ccy, 1, function(xx) currency(xx[1], identifiers=list(Num=xx[2]), digits.after.dec=xx[3], 
+                                                        description=xx[4], country=xx[5], defined.by='wiki')))
     if (!is.null(Symbols) && !identical(character(0), Symbols[!Symbols %in% ccy$Code])) {
         if (!silent) warning(paste("The following are historical,",
                 "and are no longer active:", Symbols[!Symbols %in% ccy$Code]))
@@ -62,3 +90,4 @@
 #The table below shows the ISO currency codes of former currencies and their common names 
 #(which do not always match the ISO 4217 name)."
 #historic.ccy <- x[[4]]
+



More information about the Blotter-commits mailing list