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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Oct 11 02:07:12 CEST 2010


Author: braverock
Date: 2010-10-11 02:07:10 +0200 (Mon, 11 Oct 2010)
New Revision: 412

Added:
   pkg/blotter/R/PortfReturns.R
Modified:
   pkg/blotter/R/addTxn.R
   pkg/blotter/R/initAcct.R
Log:
- initial revision of PortfReturns fn, not yet exported, provides per-instrument contribution to return on equity


Added: pkg/blotter/R/PortfReturns.R
===================================================================
--- pkg/blotter/R/PortfReturns.R	                        (rev 0)
+++ pkg/blotter/R/PortfReturns.R	2010-10-11 00:07:10 UTC (rev 412)
@@ -0,0 +1,37 @@
+PortfReturns <- function (Account, method=c('contribution'),...,Dates=NULL,Portfolios=NULL) 
+{ # @author Brian Peterson
+	aname<-Account
+	if(!grepl("account\\.",aname)) Account<-try(get(paste("account",aname,sep='.'),envir=.blotter))
+	else Account<-try(get(aname,envir=.blotter))
+	if(inherits(Account,"try-error"))
+		stop(paste("Account ",aname," not found, use initAcct() to create a new account"))
+	if(!inherits(Account,"account")) stop("Account ",aname," passed is not the name of an account object.")
+	
+	Portfolios = names(Account$portfolios)
+	table=NULL
+	for(pname in Portfolios){
+		#Portfolio<-
+		ptable = .getBySymbol(Portfolio = pname, Attribute = "Net.Trading.PL", Dates = Dates,...)
+		
+		#TODO check portfolio and account currencies and convert if necessary
+		
+		if(!is.null(attr(Account,'initEq'))){
+			ptable = ptable/as.numeric(attr(Account,'initEq'))
+		}
+		if(is.null(table)) table=ptable
+		else table=cbind(table,ptable)
+	}
+}
+
+
+###############################################################################
+# Blotter: Tools for transaction-oriented trading systems development
+# for R (see http://r-project.org/)
+# Copyright (c) 2008-2010 Peter Carl and Brian G. Peterson
+#
+# This library is distributed under the terms of the GNU Public License (GPL)
+# for full details see the file COPYING
+#
+# $Id$
+#
+###############################################################################


Property changes on: pkg/blotter/R/PortfReturns.R
___________________________________________________________________
Added: svn:mime-type
   + text/plain
Added: svn:keywords
   + Revision Id Date Author

Modified: pkg/blotter/R/addTxn.R
===================================================================
--- pkg/blotter/R/addTxn.R	2010-10-10 12:05:53 UTC (rev 411)
+++ pkg/blotter/R/addTxn.R	2010-10-11 00:07:10 UTC (rev 412)
@@ -58,10 +58,18 @@
     PrevPosAvgCost = getPosAvgCost(pname, Symbol, TxnDate)
     PosAvgCost = calcPosAvgCost(PrevPosQty, PrevPosAvgCost, TxnValue, PosQty, ConMult)
 
+	
     # Calculate any realized profit or loss (net of fees) from the transaction
-    GrossTxnRealizedPL = calcRealizedPL(TxnQty, TxnAvgCost, PrevPosAvgCost, PosQty, PrevPosQty, ConMult)
-    NetTxnRealizedPL = GrossTxnRealizedPL + txnfees
+    GrossTxnRealizedPL = TxnQty * ConMult * (PrevPosAvgCost - TxnAvgCost)
 
+	# if the previous position is zero, RealizedPL = 0
+	# if previous position is positive and position is larger, RealizedPL =0
+	# if previous position is negative and position is smaller, RealizedPL =0
+	if(abs(PrevPosQty) < abs(PosQty) | (PrevPosQty = 0))
+		GrossTxnRealizedPL = 0
+	
+	NetTxnRealizedPL = GrossTxnRealizedPL + txnfees
+
     # Store the transaction and calculations
     NewTxn = xts(t(c(TxnQty, TxnPrice, TxnValue, TxnAvgCost, PosQty, PosAvgCost, GrossTxnRealizedPL, txnfees, NetTxnRealizedPL, ConMult)), order.by=as.POSIXct(TxnDate))
     #colnames(NewTxns) = c('Txn.Qty', 'Txn.Price', 'Txn.Value', 'Txn.Avg.Cost', 'Pos.Qty', 'Pos.Avg.Cost', 'Gross.Txn.Realized.PL', 'Txn.Fees', 'Net.Txn.Realized.PL', 'Con.Mult')

Modified: pkg/blotter/R/initAcct.R
===================================================================
--- pkg/blotter/R/initAcct.R	2010-10-10 12:05:53 UTC (rev 411)
+++ pkg/blotter/R/initAcct.R	2010-10-11 00:07:10 UTC (rev 412)
@@ -61,9 +61,10 @@
         account$portfolios[[portfolio]] = initSummary(initDate=initDate)
     }
     # return(account)
-    class(account)<-c("portfolio_account","account")
     attr(account,'currency')<-currency
-    assign(paste("account",as.character(name),sep='.'),account,envir=.blotter)  
+	attr(account,'initEq')<-initEq
+	class(account)<-c("portfolio_account","account")
+	assign(paste("account",as.character(name),sep='.'),account,envir=.blotter)  
     return(name) # not sure this is a good idea
 }
 



More information about the Blotter-commits mailing list