[Blotter-commits] r306 - pkg/blotter/R
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Fri Mar 26 22:19:52 CET 2010
Author: peter_carl
Date: 2010-03-26 22:19:51 +0100 (Fri, 26 Mar 2010)
New Revision: 306
Modified:
pkg/blotter/R/addTxn.R
pkg/blotter/R/initPosPL.R
pkg/blotter/R/initTxn.R
pkg/blotter/R/updatePosPL.R
Log:
- modified to clarify Gross and Net P&L calcs
Modified: pkg/blotter/R/addTxn.R
===================================================================
--- pkg/blotter/R/addTxn.R 2010-03-26 14:28:57 UTC (rev 305)
+++ pkg/blotter/R/addTxn.R 2010-03-26 21:19:51 UTC (rev 306)
@@ -35,7 +35,7 @@
# Compute transaction fees if a function was supplied
txnfees <- ifelse( is.function(TxnFees), TxnFees(TxnQty, TxnPrice), TxnFees)
# Calculate the value and average cost of the transaction
- TxnValue = calcTxnValue(TxnQty, TxnPrice, txnfees, ConMult)
+ TxnValue = calcTxnValue(TxnQty, TxnPrice, 0, ConMult) # Gross of Fees
TxnAvgCost = calcTxnAvgCost(TxnValue, TxnQty, ConMult)
# Calculate the change in position
@@ -47,11 +47,12 @@
PosAvgCost = calcPosAvgCost(PrevPosQty, PrevPosAvgCost, TxnValue, PosQty, ConMult)
# Calculate any realized profit or loss (net of fees) from the transaction
- RealizedPL = calcRealizedPL(TxnQty, TxnAvgCost, PrevPosAvgCost, PosQty, PrevPosQty, ConMult)
+ GrossTxnRealizedPL = calcRealizedPL(TxnQty, TxnAvgCost, PrevPosAvgCost, PosQty, PrevPosQty, ConMult)
+ NetTxnRealizedPL = GrossTxnRealizedPL + txnfees
# Store the transaction and calculations
- NewTxn = xts(t(c(TxnQty, TxnPrice, txnfees, TxnValue, TxnAvgCost, PosQty, PosAvgCost, RealizedPL, ConMult)), order.by=as.POSIXct(TxnDate))
- #colnames(NewTxn) = c('Txn.Qty', 'Txn.Price', 'Txn.Fees', 'Txn.Value', 'Txn.Avg.Cost', 'Pos.Qty', 'Pos.Avg.Cost', 'Realized.PL')
+ 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')
Portfolio[[Symbol]]$txn<-rbind(Portfolio[[Symbol]]$txn, NewTxn)
if(verbose)
@@ -96,25 +97,27 @@
TxnAvgCost <- calcTxnAvgCost(TxnValue, TxnQty, ConMult)
#PrevPosQty <- getPosQty(pname, Symbol, index(TxnData[row,]))
PosQty <- PrevPosQty+TxnQty
- PosAvgCost <- calcPosAvgCost(PrevPosQty, PrevPosAvgCost, TxnValue, PosQty, ConMult) # lag this over the data?
- RealizedPL = calcRealizedPL(TxnQty, TxnAvgCost, PrevPosAvgCost, PosQty, PrevPosQty, ConMult)
+ PosAvgCost <- calcPosAvgCost(PrevPosQty, PrevPosAvgCost, 0, PosQty, ConMult) # lag this over the data?
+ GrossTxnRealizedPL = calcRealizedPL(TxnQty, TxnAvgCost, PrevPosAvgCost, PosQty, PrevPosQty, ConMult)
+ NetTxnRealizedPL = GrossTxnRealizedPL - TxnFee
PrevPosQty <- PosQty
PrevPosAvgCost <- PosAvgCost
NewTxn = xts(t(c(TxnQty,
TxnPrice,
- TxnFee,
TxnValue,
TxnAvgCost,
PosQty,
PosAvgCost,
- RealizedPL,
+ GrossTxnRealizedPL,
+ TxnFee,
+ NetTxnRealizedPL,
ConMult)),
order.by=index(TxnData[row,]))
if(row==1){
NewTxns <- NewTxn
- colnames(NewTxns) = c('Txn.Qty', 'Txn.Price', 'Txn.Fees', 'Txn.Value', 'Txn.Avg.Cost', 'Pos.Qty', 'Pos.Avg.Cost', 'Txn.Realized.PL', 'Con.Mult')
+ 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')
} else {
NewTxns<-rbind(NewTxns, NewTxn)
}
Modified: pkg/blotter/R/initPosPL.R
===================================================================
--- pkg/blotter/R/initPosPL.R 2010-03-26 14:28:57 UTC (rev 305)
+++ pkg/blotter/R/initPosPL.R 2010-03-26 21:19:51 UTC (rev 306)
@@ -9,19 +9,16 @@
# DESCRIPTION
# Constructs the data container used to store calculated P&L values from
- # transactions and close prices. The data series will be a regular time series.
+ # transactions and close prices.
# Inputs
- # initDate:
- #
- # initPosQty:
# Outputs
# Constructs multi-column xts object used to store derived position information
# FUNCTION
- posPL <- xts( as.matrix(t(c(initPosQty,initConMult,initCcyMult,0,0,0,0,0,0))), order.by=as.POSIXct(initDate) )
- colnames(posPL) <- c('Pos.Qty', 'Con.Mult', 'Ccy.Mult', 'Pos.Value', 'Txn.Value', 'Txn.Fees', 'Realized.PL', 'Unrealized.PL','Trading.PL')
+ posPL <- xts( as.matrix(t(c(initPosQty,initConMult,initCcyMult,0,0,0,0,0,0,0))), order.by=as.POSIXct(initDate) )
+ colnames(posPL) <- c('Pos.Qty', 'Con.Mult', 'Ccy.Mult', 'Pos.Value', 'Txn.Value', 'Realized.PL', 'Unrealized.PL','Gross.Trading.PL', 'Txn.Fees', 'Net.Trading.PL')
class(posPL)<- c("posPL",class(posPL))
return(posPL)
}
Modified: pkg/blotter/R/initTxn.R
===================================================================
--- pkg/blotter/R/initTxn.R 2010-03-26 14:28:57 UTC (rev 305)
+++ pkg/blotter/R/initTxn.R 2010-03-26 21:19:51 UTC (rev 306)
@@ -16,8 +16,8 @@
# FUNCTION
## TODO: Add 'Txn.Type' column
## TODO: DIVIDEND Txn.Type creates a realized gain
- txn <- xts( as.matrix(t(c(0,0,0,0,0,initPosQty,0,0,0))), order.by=as.POSIXct(initDate) )
- colnames(txn) <- c('Txn.Qty', 'Txn.Price', 'Txn.Fees', 'Txn.Value', 'Txn.Avg.Cost', 'Pos.Qty', 'Pos.Avg.Cost', 'Txn.Realized.PL','Con.Mult')
+ txn <- xts( as.matrix(t(c(0,0,0,0,initPosQty,0,0,0,0,0))), order.by=as.POSIXct(initDate) )
+ colnames(txn) <- 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')
class(txn)<-c("transactions",class(txn))
return(txn)
}
Modified: pkg/blotter/R/updatePosPL.R
===================================================================
--- pkg/blotter/R/updatePosPL.R 2010-03-26 14:28:57 UTC (rev 305)
+++ pkg/blotter/R/updatePosPL.R 2010-03-26 21:19:51 UTC (rev 306)
@@ -88,15 +88,16 @@
PrevClosePrice = as.numeric(Cl(Prices)[as.character(PrevDate)])
PrevPosValue = calcPosValue(PrevPosQty, PrevClosePrice, ConMult) ### @TODO: PrevConMult?
- TradingPL = PosValue - PrevPosValue - TxnValue
+ GrossTradingPL = PosValue - PrevPosValue - TxnValue
+ NetTradingPL = GrossTradingPL + TxnFees # Fees are assumed to have negative values
UnrealizedPL = PosQty*(ClosePrice-getPosAvgCost(Portfolio=pname, Symbol, CurrentDate))*ConMult
- RealizedPL = round(TradingPL - UnrealizedPL,2)
+ 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, TxnFees, RealizedPL, UnrealizedPL, TradingPL)), order.by=as.POSIXct(CurrentDate)) #, format=tformat
- colnames(NewPeriod) = c('Pos.Qty', 'Con.Mult', 'Ccy.Mult', 'Pos.Value', 'Txn.Value', 'Txn.Fees', 'Realized.PL', 'Unrealized.PL', 'Trading.PL')
+ NewPeriod = as.xts(t(c(PosQty, ConMult, CcyMult, PosValue, TxnValue, RealizedPL, UnrealizedPL, GrossTradingPL, TxnFees, NetTradingPL)), order.by=as.POSIXct(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)
}
# return(Portfolio)
More information about the Blotter-commits
mailing list