[Blotter-commits] r163 - pkg/blotter/R
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Fri Jan 15 22:25:41 CET 2010
Author: peter_carl
Date: 2010-01-15 22:25:41 +0100 (Fri, 15 Jan 2010)
New Revision: 163
Modified:
pkg/blotter/R/addTxn.R
pkg/blotter/R/calcPosAvgCost.R
pkg/blotter/R/calcTxnAvgCost.R
Log:
- added ConMult to calculations for correct scaling
Modified: pkg/blotter/R/addTxn.R
===================================================================
--- pkg/blotter/R/addTxn.R 2010-01-15 20:26:45 UTC (rev 162)
+++ pkg/blotter/R/addTxn.R 2010-01-15 21:25:41 UTC (rev 163)
@@ -36,7 +36,7 @@
txnfees <- ifelse( is.function(TxnFees), TxnFees(TxnQty, TxnPrice), TxnFees)
# Calculate the value and average cost of the transaction
TxnValue = calcTxnValue(TxnQty, TxnPrice, txnfees, ConMult)
- TxnAvgCost = calcTxnAvgCost(TxnValue, TxnQty)
+ TxnAvgCost = calcTxnAvgCost(TxnValue, TxnQty, ConMult)
# Calculate the change in position
PrevPosQty = getPosQty(pname, Symbol, TxnDate)
@@ -44,7 +44,7 @@
# Calculate the resulting position's average cost
PrevPosAvgCost = getPosAvgCost(pname, Symbol, TxnDate)
- PosAvgCost = calcPosAvgCost(PrevPosQty, PrevPosAvgCost, TxnValue, PosQty)
+ 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)
@@ -75,7 +75,7 @@
if(is.null(ConMult)){
tmp_instr<-try(getInstrument(Symbol))
if(inherits(tmp_instr,"try-error")){
- warning(paste("Instrument",Symbol," not found, using contract multiplier of 1"))
+ warning(paste("Instrument",Symbiol," not found, using contract multiplier of 1"))
ConMult<-1
} else {
ConMult<-tmp_instr$multiplier
@@ -93,13 +93,13 @@
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)
+ TxnAvgCost <- calcTxnAvgCost(TxnValue, TxnQty, ConMult)
#PrevPosQty <- getPosQty(pname, Symbol, index(TxnData[row,]))
PosQty <- PrevPosQty+TxnQty
- PosAvgCost <- calcPosAvgCost(PrevPosQty, PrevPosAvgCost, TxnValue, PosQty) # lag this over the data?
- RealizedPL = calcRealizedPL(TxnQty, TxnAvgCost, PrevPosAvgCost, PosQty, PrevPosQty, ConMult)
+ PosAvgCost <- calcPosAvgCost(PrevPosQty, PrevPosAvgCost, TxnValue, PosQty, ConMult) # lag this over the data?
PrevPosQty <- PosQty
PrevPosAvgCost <- PosAvgCost
+ RealizedPL = calcRealizedPL(TxnQty, TxnAvgCost, PrevPosAvgCost, PosQty, PrevPosQty, ConMult)
NewTxn = xts(t(c(TxnQty,
TxnPrice,
Modified: pkg/blotter/R/calcPosAvgCost.R
===================================================================
--- pkg/blotter/R/calcPosAvgCost.R 2010-01-15 20:26:45 UTC (rev 162)
+++ pkg/blotter/R/calcPosAvgCost.R 2010-01-15 21:25:41 UTC (rev 163)
@@ -18,7 +18,7 @@
if(PosQty == 0)
PosAvgCost = 0
else {
- PosAvgCost = (PrevPosQty * PrevPosAvgCost + TxnValue)/PosQty
+ PosAvgCost = (PrevPosQty * PrevPosAvgCost + TxnValue)/(PosQty*ConMult)
}
return(PosAvgCost)
}
Modified: pkg/blotter/R/calcTxnAvgCost.R
===================================================================
--- pkg/blotter/R/calcTxnAvgCost.R 2010-01-15 20:26:45 UTC (rev 162)
+++ pkg/blotter/R/calcTxnAvgCost.R 2010-01-15 21:25:41 UTC (rev 163)
@@ -1,5 +1,5 @@
`calcTxnAvgCost` <-
-function(TxnValue, TxnQty)
+function(TxnValue, TxnQty, ConMult)
{ # @author Peter Carl
# DESCRIPTION:
@@ -14,7 +14,7 @@
# Outputs
# TxnAvgCost: unit normalized (per share) cost implied by the transaction
- TxnAvgCost = TxnValue/TxnQty
+ TxnAvgCost = TxnValue/(TxnQty*ConMult)
return(TxnAvgCost)
}
More information about the Blotter-commits
mailing list