[Blotter-commits] r524 - pkg/blotter/R
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Tue Jan 4 17:16:14 CET 2011
Author: braverock
Date: 2011-01-04 17:16:13 +0100 (Tue, 04 Jan 2011)
New Revision: 524
Modified:
pkg/blotter/R/addTxn.R
pkg/blotter/R/calcPosAvgCost.R
pkg/blotter/R/calcTxnAvgCost.R
pkg/blotter/R/calcTxnValue.R
Log:
- move several non-exported calc* functions to .calc*
- adjust addTxn to call .calc fns
- remove abs() in calcPosAvgCost to account for negative prices
Modified: pkg/blotter/R/addTxn.R
===================================================================
--- pkg/blotter/R/addTxn.R 2011-01-04 12:40:10 UTC (rev 523)
+++ pkg/blotter/R/addTxn.R 2011-01-04 16:16:13 UTC (rev 524)
@@ -57,8 +57,8 @@
if(TxnFees>0) warning('Positive Transaction Fees should only be used in the case of broker/exchange rebates for TxnFees ',TxnFees,'. See Documentation.')
# Calculate the value and average cost of the transaction
- TxnValue = calcTxnValue(TxnQty, TxnPrice, 0, ConMult) # Gross of Fees
- TxnAvgCost = calcTxnAvgCost(TxnValue, TxnQty, ConMult)
+ TxnValue = .calcTxnValue(TxnQty, TxnPrice, 0, ConMult) # Gross of Fees
+ TxnAvgCost = .calcTxnAvgCost(TxnValue, TxnQty, ConMult)
# Calculate the change in position
PrevPosQty = getPosQty(pname, Symbol, TxnDate)
@@ -66,7 +66,7 @@
# Calculate the resulting position's average cost
PrevPosAvgCost = .getPosAvgCost(pname, Symbol, TxnDate)
- PosAvgCost = calcPosAvgCost(PrevPosQty, PrevPosAvgCost, TxnValue, PosQty, ConMult)
+ PosAvgCost = .calcPosAvgCost(PrevPosQty, PrevPosAvgCost, TxnValue, PosQty, ConMult)
# Calculate any realized profit or loss (net of fees) from the transaction
@@ -134,11 +134,11 @@
TxnPrice <- as.numeric(TxnData[row,'Price'])
TxnFee <- 0 #TODO FIXME support transaction fees in addTxns
#TxnFee <- ifelse( is.function(TxnFees), TxnFees(TxnQty, TxnPrice), TxnFees)
- TxnValue <- calcTxnValue(TxnQty, TxnPrice, TxnFee, ConMult)
- TxnAvgCost <- calcTxnAvgCost(TxnValue, TxnQty, ConMult)
+ TxnValue <- .calcTxnValue(TxnQty, TxnPrice, TxnFee, ConMult)
+ TxnAvgCost <- .calcTxnAvgCost(TxnValue, TxnQty, ConMult)
#PrevPosQty <- getPosQty(pname, Symbol, index(TxnData[row,]))
PosQty <- PrevPosQty+TxnQty
- PosAvgCost <- calcPosAvgCost(PrevPosQty, PrevPosAvgCost, 0, PosQty, ConMult) # lag this over the data?
+ PosAvgCost <- .calcPosAvgCost(PrevPosQty, PrevPosAvgCost, 0, PosQty, ConMult) # lag this over the data?
GrossTxnRealizedPL = TxnQty * ConMult * (PrevPosAvgCost - TxnAvgCost)
NetTxnRealizedPL = GrossTxnRealizedPL - TxnFee
PrevPosQty <- PosQty
Modified: pkg/blotter/R/calcPosAvgCost.R
===================================================================
--- pkg/blotter/R/calcPosAvgCost.R 2011-01-04 12:40:10 UTC (rev 523)
+++ pkg/blotter/R/calcPosAvgCost.R 2011-01-04 16:16:13 UTC (rev 524)
@@ -6,12 +6,13 @@
#' @param TxnValue total value of the transaction, including fees
#' @param PosQty total units (shares) of the resulting position
#' @param ConMult multiplier from instrument data
-calcPosAvgCost <- function(PrevPosQty, PrevPosAvgCost, TxnValue, PosQty, ConMult=1)
+.calcPosAvgCost <- function(PrevPosQty, PrevPosAvgCost, TxnValue, PosQty, ConMult=1)
{ # @author Peter Carl
if(PosQty == 0)
PosAvgCost = 0
else {
- PosAvgCost = abs((PrevPosQty * PrevPosAvgCost * ConMult + TxnValue)/(PosQty*ConMult))
+ # PosAvgCost = abs((PrevPosQty * PrevPosAvgCost * ConMult + TxnValue)/(PosQty*ConMult))
+ PosAvgCost = (PrevPosQty * PrevPosAvgCost * ConMult + TxnValue)/(PosQty*ConMult)
}
return(PosAvgCost)
}
Modified: pkg/blotter/R/calcTxnAvgCost.R
===================================================================
--- pkg/blotter/R/calcTxnAvgCost.R 2011-01-04 12:40:10 UTC (rev 523)
+++ pkg/blotter/R/calcTxnAvgCost.R 2011-01-04 16:16:13 UTC (rev 524)
@@ -3,7 +3,7 @@
#' @param TxnQty total units (shares) of the transaction
#' @param ConMult multiplier from instrument data
#' @return TxnAvgCost: unit normalized (per share) cost implied by the transaction
-calcTxnAvgCost <- function(TxnValue, TxnQty, ConMult=1)
+.calcTxnAvgCost <- function(TxnValue, TxnQty, ConMult=1)
{ # @author Peter Carl
TxnAvgCost = TxnValue/(TxnQty*ConMult)
return(TxnAvgCost)
Modified: pkg/blotter/R/calcTxnValue.R
===================================================================
--- pkg/blotter/R/calcTxnValue.R 2011-01-04 12:40:10 UTC (rev 523)
+++ pkg/blotter/R/calcTxnValue.R 2011-01-04 16:16:13 UTC (rev 524)
@@ -4,7 +4,7 @@
#' @param TxnFees fees associated with the transaction, e.g. commissions
#' @param ConMult multiplier from instrument data
#' @return TxnValue: total dollar value of the transaction, including fees
-calcTxnValue <- function(TxnQty, TxnPrice, TxnFees, ConMult=1)
+.calcTxnValue <- function(TxnQty, TxnPrice, TxnFees, ConMult=1)
{ # @author Peter Carl
TxnValue = TxnQty * TxnPrice * ConMult - TxnFees
return(TxnValue)
More information about the Blotter-commits
mailing list