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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Aug 16 17:30:39 CEST 2010


Author: braverock
Date: 2010-08-16 17:30:39 +0200 (Mon, 16 Aug 2010)
New Revision: 372

Modified:
   pkg/blotter/R/updatePosPL.R
Log:
- apply first part of patch from Wolfgang Wu to streamline portfolio PL processing

Modified: pkg/blotter/R/updatePosPL.R
===================================================================
--- pkg/blotter/R/updatePosPL.R	2010-08-12 20:18:09 UTC (rev 371)
+++ pkg/blotter/R/updatePosPL.R	2010-08-16 15:30:39 UTC (rev 372)
@@ -97,8 +97,8 @@
         PriorPrevDateLast = PriorPrevDateWidth$last.time
         CurrentSpan = paste(PrevDateLast, CurrentDate, sep="::")
         PrevSpan = paste(PriorPrevDateLast, PrevDate, sep="::")
-        if(length(PrevDate)==0)
-             PrevDate = NA
+
+		if(length(PrevDate)==0) PrevDate = NA
         
         #TODO write a single getTxn and use the values instead of these lines
         TxnValue = getTxnValue(pname, Symbol, CurrentSpan)
@@ -106,27 +106,25 @@
         PosQty = getPosQty(pname, Symbol, as.character(CurrentDate))
         
         ClosePrice = as.numeric(last(getPrice(Prices[CurrentDate,]))) #not necessary
-        PosValue = calcPosValue(PosQty, ClosePrice, ConMult)
-
-        if(is.na(PrevDate))
+        #PosValue = calcPosValue(PosQty, ClosePrice, ConMult)
+		PosValue <- PosQty * ClosePrice * ConMult			
+	
+        if(is.na(PrevDate)){
             PrevPosQty = 0
-        else
+            PrevPosValue = 0
+        } else {
             PrevPosQty = getPosQty(pname, Symbol, as.character(PrevDate)) 
+            PrevPosValue <- as.numeric(Portfolio[[Symbol]]$posPL[PrevDate, 'Pos.Value'])
+        }    
 
-        if(PrevPosQty==0)
-            PrevClosePrice = 0
-        else
-            PrevClosePrice = as.numeric(getPrice(Prices)[as.character(PrevDate)])
+        ifelse(PrevPosQty==0, PrevClosePrice <- 0 , PrevClosePrice <- as.numeric(getPrice(Prices)[as.character(PrevDate)]))
 
-        PrevPosValue = calcPosValue(PrevPosQty, PrevClosePrice, ConMult) ### @TODO: PrevConMult?
-        GrossTradingPL = PosValue - PrevPosValue - TxnValue
-        NetTradingPL = GrossTradingPL + TxnFees # Fees are assumed to have negative values
+		GrossTradingPL = PosValue - PrevPosValue - TxnValue
+		NetTradingPL = GrossTradingPL + TxnFees # Fees are assumed to have negative values
+		PosAvgCost = as.numeric(last(Portfolio[[Symbol]]$txn[paste('::', CurrentDate, sep=""),'Pos.Avg.Cost']))			
+		UnrealizedPL = PosQty*(ClosePrice-PosAvgCost)*ConMult			
+		RealizedPL = round(GrossTradingPL - UnrealizedPL,2)
 
-        UnrealizedPL = PosQty*(ClosePrice-getPosAvgCost(Portfolio=pname, Symbol, CurrentDate))*ConMult
-
-        RealizedPL = round(GrossTradingPL - UnrealizedPL,2)
-        #$unrealized_gl    = $end_return['calc_position'] * ($end_return['last_price'] - $end_return['average_cost']);
-
         NewPeriod = as.xts(t(c(PosQty, ConMult, CcyMult, PosValue, TxnValue, RealizedPL, UnrealizedPL, GrossTradingPL, TxnFees, NetTradingPL)), order.by=CurrentDate) #, format=tformat
         colnames(NewPeriod) =  c('Pos.Qty', 'Con.Mult', 'Ccy.Mult', 'Pos.Value', 'Txn.Value',  'Realized.PL', 'Unrealized.PL','Gross.Trading.PL', 'Txn.Fees', 'Net.Trading.PL')
         Portfolio[[Symbol]]$posPL <- rbind(Portfolio[[Symbol]]$posPL, NewPeriod) 
@@ -174,4 +172,4 @@
 #
 # $Id$
 #
-###############################################################################
+###############################################################################
\ No newline at end of file



More information about the Blotter-commits mailing list