[Blotter-commits] r137 - in pkg/blotter: . R
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Tue Jan 12 15:19:21 CET 2010
Author: braverock
Date: 2010-01-12 15:19:20 +0100 (Tue, 12 Jan 2010)
New Revision: 137
Modified:
pkg/blotter/NAMESPACE
pkg/blotter/R/addTxn.R
pkg/blotter/R/getPos.R
pkg/blotter/R/initPosPL.R
Log:
- initial support for adding multiple transactions at once fn addTxns
Modified: pkg/blotter/NAMESPACE
===================================================================
--- pkg/blotter/NAMESPACE 2010-01-12 04:11:11 UTC (rev 136)
+++ pkg/blotter/NAMESPACE 2010-01-12 14:19:20 UTC (rev 137)
@@ -1,4 +1,5 @@
export(addTxn)
+export(addTxns)
export(chart.Posn)
export(getEndEq)
export(getPosQty)
Modified: pkg/blotter/R/addTxn.R
===================================================================
--- pkg/blotter/R/addTxn.R 2010-01-12 04:11:11 UTC (rev 136)
+++ pkg/blotter/R/addTxn.R 2010-01-12 14:19:20 UTC (rev 137)
@@ -22,9 +22,9 @@
# FUNCTION
# Compute transaction fees if a function was supplied
- txncost <- ifelse( is.function(TxnFees), TxnFees(TxnQty, TxnPrice), TxnFees)
+ txnfees <- ifelse( is.function(TxnFees), TxnFees(TxnQty, TxnPrice), TxnFees)
# Calculate the value and average cost of the transaction
- TxnValue = calcTxnValue(TxnQty, TxnPrice, txncost)
+ TxnValue = calcTxnValue(TxnQty, TxnPrice, txnfees)
TxnAvgCost = calcTxnAvgCost(TxnValue, TxnQty)
# Calculate the change in position
@@ -39,9 +39,9 @@
RealizedPL = calcRealizedPL(TxnQty, TxnAvgCost, PrevPosAvgCost, PosQty, PrevPosQty)
# Store the transaction and calculations
- 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)
+ NewTxn = xts(t(c(TxnQty, TxnPrice, txnfees, 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')
+ rbind(Portfolio[[Symbol]]$txn, NewTxn)
if(verbose)
print(paste(TxnDate, Symbol, TxnQty, "@",TxnPrice, sep=" "))
@@ -54,6 +54,49 @@
return(TxnQty * -0.01)
}
+#' @export
+addTxns<- function(Portfolio, Symbol, TxnData , verbose=TRUE, ... )
+{
+ pname<-Portfolio
+ Portfolio<-get(paste("portfolio",pname,sep='.'),envir=.blotter)
+
+ #NewTxns=xts()
+ for (row in 1:nrow(TxnData)) {
+ #TODO create vectorized versions of all these functions so we don't have to loop
+ TxnQty <- TxnData[row,'Quantity']
+ TxnPrice <- TxnData[row,'Price']
+ TxnFee <- 0 #TODO FIXME support transaction fees in addTxns
+ TxnValue <- calcTxnValue(TxnQty, TxnPrice, TxnFee)
+ TxnAvgCost <- calcTxnAvgCost(TxnValue, TxnQty)
+ PrevPosQty <- getPosQty(pname, Symbol, index(TxnData[row,]))
+ PosQty <- PrevPosQty+TxnQty
+ PrevPosAvgCost <- getPosAvgCost(pname, Symbol, index(TxnData[row,]))
+ PosAvgCost <- calcPosAvgCost(PrevPosQty, PrevPosAvgCost, TxnValue, PosQty)
+ RealizedPL = calcRealizedPL(TxnQty, TxnAvgCost, PrevPosAvgCost, PosQty, PrevPosQty)
+
+ NewTxn = xts(t(c(TxnQty,
+ TxnPrice,
+ TxnFee,
+ TxnValue,
+ TxnAvgCost,
+ PosQty,
+ PosAvgCost,
+ RealizedPL)),
+ 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', 'Realized.PL')
+ } else {
+ NewTxns<-rbind(NewTxns, NewTxn)
+ }
+ }
+ Portfolio[[Symbol]]$txn<-rbind(Portfolio[[Symbol]]$txn,NewTxns)
+
+ if(verbose) print(NewTxns)
+
+ assign(paste("portfolio",pname,sep='.'),Portfolio,envir=.blotter)
+}
+
###############################################################################
# Blotter: Tools for transaction-oriented trading systems development
# for R (see http://r-project.org/)
Modified: pkg/blotter/R/getPos.R
===================================================================
--- pkg/blotter/R/getPos.R 2010-01-12 04:11:11 UTC (rev 136)
+++ pkg/blotter/R/getPos.R 2010-01-12 14:19:20 UTC (rev 137)
@@ -1,5 +1,4 @@
-`getPos` <-
-function(Portfolio, Symbol, Date)
+getPos <- function(Portfolio, Symbol, Date)
{ # @author Peter Carl
pname<-Portfolio
Portfolio<-get(paste("portfolio",pname,sep='.'),envir=.blotter)
@@ -23,7 +22,7 @@
toDate = paste('::', Date, sep="")
# It may not make sense to return realized P&L with the position information, so only position and
# position average cost are returned.
- Pos = tail(PosData[toDate,c('Pos.Qty','Pos.Avg.Cost')], n=1)
+ Pos = last(PosData[toDate,c('Pos.Qty','Pos.Avg.Cost')])
return(Pos)
}
Modified: pkg/blotter/R/initPosPL.R
===================================================================
--- pkg/blotter/R/initPosPL.R 2010-01-12 04:11:11 UTC (rev 136)
+++ pkg/blotter/R/initPosPL.R 2010-01-12 14:19:20 UTC (rev 137)
@@ -1,7 +1,10 @@
#' initializes position P&L for a portfolio instrument
-#' @param initDate
-#' @param initPosQty
-initPosPL <- function(initDate="1950-01-01", initPosQty=0)
+#' @param initDate date prior to the first close price given, used to contain initial account equity and initial position
+#' @param \dots any other passthrough parameters
+#' @param initPosQty initial position, default is zero
+#' @param initConMult initial contract multiplier, default is one(1)
+#' @param initCcyMult initial currency multiplier, default is one(1)
+initPosPL <- function(initDate="1950-01-01", ..., initPosQty=0, initConMult=1, initCcyMult=1) #TODO add other init values to function as well for cost basis
{ # @author Peter Carl
# DESCRIPTION
@@ -9,16 +12,16 @@
# transactions and close prices. The data series will be a regular time series.
# Inputs
- # initDate: date prior to the first close price given, used to contain
- # initial account equity and initial position
- # initPosQty: initial position, default is zero
+ # initDate:
+ #
+ # initPosQty:
# Outputs
# Constructs multi-column xts object used to store derived position information
# FUNCTION
- posPL <- xts( as.matrix(t(c(initPosQty,0,0,0,0,0,0,0))), order.by=as.POSIXct(initDate) )
- colnames(posPL) <- c('Pos.Qty', 'Con.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))), 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')
class(posPL)<- c("posPL",class(posPL))
return(posPL)
}
More information about the Blotter-commits
mailing list