[Blotter-commits] r790 - pkg/FinancialInstrument/sandbox

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sun Sep 25 20:26:39 CEST 2011


Author: gsee
Date: 2011-09-25 20:26:39 +0200 (Sun, 25 Sep 2011)
New Revision: 790

Added:
   pkg/FinancialInstrument/sandbox/ISO.currencies.wiki.R
Log:
 sandbox: define_currencies using wikipedia

Added: pkg/FinancialInstrument/sandbox/ISO.currencies.wiki.R
===================================================================
--- pkg/FinancialInstrument/sandbox/ISO.currencies.wiki.R	                        (rev 0)
+++ pkg/FinancialInstrument/sandbox/ISO.currencies.wiki.R	2011-09-25 18:26:39 UTC (rev 790)
@@ -0,0 +1,61 @@
+#http://en.wikipedia.org/wiki/ISO_4217#Active_codes
+
+#' Define currency instruments using the tables found on the ISO_4217 wikipedia page
+#'
+#' If you do not provide \code{Symbols} all active ISO 4127 curriencies will be defined.
+#' If you do provide \code{Symbols} only the Symbols you provided will be defined. Also,
+#' if you provide some \code{Symbols} that are not active ISO currencies, it will try to 
+#' find them in the "Without currency code" and "Historic currency codes" tables.
+#' @param Symbols
+#' @param silent
+#' @return the names of the currecies that were defined.  
+#' If \code{Symbols} was provided, they will be in the order they were found (ISO, non-ISO, historic). 
+#' Called for side-effect
+#' @references \url{http://en.wikipedia.org/wiki/ISO_4217}
+#' @examples
+#' \dontrun{
+#' define_currencies.wiki(c("USD","EUR","ADP","ETB","GBP","BTC"))
+#' define_currencies.wiki()
+#' }
+define_currencies.wiki <- 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://en.wikipedia.org/wiki/ISO_4217")
+    #"The following is a list of active codes of official ISO 4217 currency names."        
+    ccy <- x[[2]] #active ISO 4217 currencies
+    if (!missing(Symbols)) {
+        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])))
+    if (!is.null(Symbols) && !identical(character(0), Symbols[!Symbols %in% ccy$Code])) {
+        if (!silent) warning(paste("The following are historical,",
+                "and are not longer active:", Symbols[!Symbols %in% ccy$Code]))
+        hccy <- x[[4]]
+        hccy <- hccy[hccy$Code %in% Symbols,]
+        out <- c(out, unname(apply(hccy, 1, function(xx) {
+                    currency(xx[1], description=xx[4], used.from=xx[5], used.until=xx[6], replaced.by=xx[7])
+                 })))
+    }
+    out
+}
+
+#rm_currencies()
+#define_currencies.wiki(c("USD","EUR","ADP","ETB","GBP","BTC"))
+#define_currencies.wiki()
+
+#_______________________________________________________________________________________
+
+#x <- readHTMLTable("http://en.wikipedia.org/wiki/ISO_4217")
+
+#"A number of territories are not included in ISO 4217, because their currencies are: 
+#(a) not per se an independent currency but a variant of another currency, 
+#(b) a legal tender only issued as commemorative banknotes or coinage, or 
+#(c) a currency of an unrecognized or partially recognized state. These currencies are:"
+#nonISO.ccy <- x[[3]]
+
+#"A number of currencies were official ISO 4217 currency codes and currency names 
+#until their replacement by the euro or other currencies. 
+#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