[Blotter-commits] r491 - pkg/FinancialInstrument/R

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Dec 10 23:57:53 CET 2010


Author: peter_carl
Date: 2010-12-10 23:57:53 +0100 (Fri, 10 Dec 2010)
New Revision: 491

Added:
   pkg/FinancialInstrument/R/buildHierarchy.R
Log:
- first draft of function


Added: pkg/FinancialInstrument/R/buildHierarchy.R
===================================================================
--- pkg/FinancialInstrument/R/buildHierarchy.R	                        (rev 0)
+++ pkg/FinancialInstrument/R/buildHierarchy.R	2010-12-10 22:57:53 UTC (rev 491)
@@ -0,0 +1,38 @@
+#' Construct a heirarchy of instruments useful for aggregation
+#'
+#' All 'currency' instruments must be defined before instruments of other types may be defined.
+#'
+#' In \dots you may pass any other arbitrary instrument fields that will be used to create 'custom' fields.
+#'
+#' @param primary_ids A list of assets to be included in the hierarchy list
+#' @param levels A list of instrument attributes in top-down order
+#' @param ... any other passthru parameters
+#' @author Peter Carl
+#' @return Constructs a data.frame that contains the list of assets in the first column and the category or factor for grouping at each level in the following columns
+#' @seealso
+# TODO add a link to PortfolioAnalytics attribution functions, when they exist
+#' @export
+buildHeirarchy <- function(primary_ids, levels, ...) {
+    out = data.frame(NA)
+    for(primary_id in primary_ids) {
+        tmp_instr = try(getInstrument(primary_id))
+        # TODO finish error checking
+        # for each instrument, create a vector of factors at each level
+        attrs = NA
+        for(level in levels){
+            attr = unname(tmp_instr[eval(level)])
+            if(!is.na(attrs))
+                attrs = cbind(attrs, attr)
+            else
+                attrs = attr
+        }
+        if(!is.na(out))
+            out = rbind(out, attrs)
+        else
+            out = attrs
+    }
+    out = cbind(primary_ids, out)
+    colnames(out) = c("primary_id", levels)
+    out
+
+}


Property changes on: pkg/FinancialInstrument/R/buildHierarchy.R
___________________________________________________________________
Added: svn:keywords
   + Revision Id Date Author



More information about the Blotter-commits mailing list