[Blotter-commits] r1502 - pkg/blotter/R

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Sep 13 19:13:11 CEST 2013


Author: braverock
Date: 2013-09-13 19:13:10 +0200 (Fri, 13 Sep 2013)
New Revision: 1502

Modified:
   pkg/blotter/R/initPortf.R
Log:
- change portfolio list to a hashed environent, thanks to JR and Josh Ulrich for pointers

Modified: pkg/blotter/R/initPortf.R
===================================================================
--- pkg/blotter/R/initPortf.R	2013-09-12 16:04:36 UTC (rev 1501)
+++ pkg/blotter/R/initPortf.R	2013-09-13 17:13:10 UTC (rev 1502)
@@ -60,30 +60,35 @@
 #' @export
 initPortf <- function(name="default", symbols, initPosQty = 0, initDate = '1950-01-01', currency='USD', ...)
 { # @author Peter Carl
-    if(exists(paste("portfolio",name,sep='.'), envir=.blotter,inherits=TRUE))
-        stop(paste("Portfolio",name,"already exists, use updatePortf() or addPortfInstr() to update it."))
-    
-    # FUNCTION
-    portfolio=list()
-	portfolio$symbols=vector("list",length=length(symbols))
-    names(portfolio$symbols)=symbols
-    if(length(initPosQty)==1)
-	initPosQty=rep(initPosQty, length(symbols))
-    if(length(initPosQty)!=length(symbols))
-	stop("The length of initPosQty is unequal to the number of symbols in the portfolio.")
-    for(instrument in symbols){
-    	i = match(instrument, symbols)
-        portfolio$symbols[[instrument]]$txn = .initTxn(initDate = initDate, initPosQty = initPosQty[i],...=...)
-        portfolio$symbols[[instrument]]$posPL = .initPosPL(initDate = initDate, initPosQty = initPosQty[i],...=...)
-        portfolio$symbols[[instrument]][[paste('posPL',currency,sep='.')]] = portfolio$symbols[[instrument]]$posPL
-    }
-	portfolio$summary<-.initSummary(initDate=initDate)
-    class(portfolio)<-c("blotter_portfolio", "portfolio")
-    attr(portfolio,'currency')<-currency
-	attr(portfolio,'initDate')<-initDate
-    #return(portfolio)
-    assign(paste("portfolio",as.character(name),sep='.'),portfolio,envir=.blotter)
-    return(name) # not sure this is a good idea
+  if(exists(paste("portfolio",name,sep='.'), envir=.blotter,inherits=TRUE))
+    stop(paste("Portfolio",name,"already exists, use updatePortf() or addPortfInstr() to update it."))
+  
+  # FUNCTION
+  
+  # portfolio=list()
+  # Initialize a hashed environment for this portfolio
+  # Thanks to Jeff Ryan and Josh Ulrich for pointers
+  portfolio = new.env(hash=TRUE)
+
+  portfolio$symbols=vector("list",length=length(symbols))
+  names(portfolio$symbols)=symbols
+  if(length(initPosQty)==1)
+    initPosQty=rep(initPosQty, length(symbols))
+  if(length(initPosQty)!=length(symbols))
+    stop("The length of initPosQty is unequal to the number of symbols in the portfolio.")
+  for(instrument in symbols){
+    i = match(instrument, symbols)
+    portfolio$symbols[[instrument]]$txn = .initTxn(initDate = initDate, initPosQty = initPosQty[i],...=...)
+    portfolio$symbols[[instrument]]$posPL = .initPosPL(initDate = initDate, initPosQty = initPosQty[i],...=...)
+    portfolio$symbols[[instrument]][[paste('posPL',currency,sep='.')]] = portfolio$symbols[[instrument]]$posPL
+  }
+  portfolio$summary<-.initSummary(initDate=initDate)
+  class(portfolio)<-c("blotter_portfolio", "portfolio")
+  attr(portfolio,'currency')<-currency
+  attr(portfolio,'initDate')<-initDate
+  #return(portfolio)
+  assign(paste("portfolio",as.character(name),sep='.'),portfolio,envir=.blotter)
+  return(name) # not sure this is a good idea
 }
 
 ###############################################################################



More information about the Blotter-commits mailing list