[Blotter-commits] r66 - pkg/R
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Thu Mar 12 03:21:49 CET 2009
Author: edd
Date: 2009-03-12 03:21:49 +0100 (Thu, 12 Mar 2009)
New Revision: 66
Modified:
pkg/R/addTxn.R
Log:
add option to supply a function of Qty and Prc to set transaction costs
Modified: pkg/R/addTxn.R
===================================================================
--- pkg/R/addTxn.R 2009-03-11 14:22:19 UTC (rev 65)
+++ pkg/R/addTxn.R 2009-03-12 02:21:49 UTC (rev 66)
@@ -15,40 +15,50 @@
# TxnPrice: price at which the transaction was done
# TxnFees: fees associated with the transaction, e.g. commissions. Fees are
# indicated as positive values and will be subtracted from the transaction.
+ # TxnFees can either be a fixed amount, or a function of two arguments
+ # Qty and Price in which case the function is evaluated to determine the
+ # fee amount.
# Outputs:
# Portfolio: hands back the entire portfolio object with the additional
# transaction in the correct slot: Portfolio[[Symbol]]$txn
# FUNCTION
+ # Compute transaction fees if a function was supplied
+ txncost <- ifelse( is.function(TxnFees), TxnFees(TxnQty, TxnPrice), TxnFees)
# Calculate the value and average cost of the transaction
- TxnValue = calcTxnValue(TxnQty, TxnPrice, TxnFees)
+ TxnValue = calcTxnValue(TxnQty, TxnPrice, txncost)
TxnAvgCost = calcTxnAvgCost(TxnValue, TxnQty)
# Calculate the change in position
PrevPosQty = getPosQty(Portfolio, Symbol, TxnDate)
- PosQty = PrevPosQty + TxnQty
+ PosQty = PrevPosQty + TxnQty
# Calculate the resulting position's average cost
PrevPosAvgCost = getPosAvgCost(Portfolio, Symbol, TxnDate)
- PosAvgCost = calcPosAvgCost(PrevPosQty, PrevPosAvgCost, TxnValue, PosQty)
+ PosAvgCost = calcPosAvgCost(PrevPosQty, PrevPosAvgCost, TxnValue, PosQty)
# Calculate any realized profit or loss (net of fees) from the transaction
RealizedPL = calcRealizedPL(TxnQty, TxnAvgCost, PrevPosAvgCost, PosQty, PrevPosQty)
# Store the transaction and calculations
- NewTxn = xts(t(c(TxnQty, TxnPrice, TxnFees, TxnValue, TxnAvgCost, PosQty, PosAvgCost, RealizedPL)), order.by=as.POSIXct(TxnDate))
+ NewTxn = xts(t(c(TxnQty, TxnPrice, txncost, TxnValue, TxnAvgCost, PosQty, PosAvgCost, RealizedPL)), 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')
- Portfolio[[Symbol]]$txn <- rbind(Portfolio[[Symbol]]$txn, NewTxn)
+ Portfolio[[Symbol]]$txn <- rbind(Portfolio[[Symbol]]$txn, NewTxn)
if(verbose)
print(paste(Symbol, TxnDate, TxnQty, "@",TxnPrice, sep=" "))
return(Portfolio)
}
+## example cost function
+pennyPerShare <- function(Qty, Price) {
+ return(Qty * 0.01)
+}
+
###############################################################################
# Blotter: Tools for transaction-oriented trading systems development
-# for R (see http://r-project.org/)
+# for R (see http://r-project.org/)
# Copyright (c) 2008 Peter Carl and Brian G. Peterson
#
# This library is distributed under the terms of the GNU Public License (GPL)
More information about the Blotter-commits
mailing list