[Blotter-commits] r1481 - in pkg/blotter: . R sandbox
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Tue Jul 2 17:43:37 CEST 2013
Author: peter_carl
Date: 2013-07-02 17:43:37 +0200 (Tue, 02 Jul 2013)
New Revision: 1481
Added:
pkg/blotter/R/addAcctTxn.R
pkg/blotter/sandbox/
pkg/blotter/sandbox/ex-AcctTxns.R
Modified:
pkg/blotter/NAMESPACE
pkg/blotter/R/initAcct.R
pkg/blotter/R/updateAcct.R
Log:
- introduced rudimentary account transaction functionality
- modified initAcct and updateAcct to handle account txns
- added addAcctTxn for passing data in
Modified: pkg/blotter/NAMESPACE
===================================================================
--- pkg/blotter/NAMESPACE 2013-06-25 02:45:10 UTC (rev 1480)
+++ pkg/blotter/NAMESPACE 2013-07-02 15:43:37 UTC (rev 1481)
@@ -2,6 +2,7 @@
export(addDiv)
export(addPortfInstr)
export(addTxn)
+export(addAcctTxn)
export(calcPortfWgt)
export(chart.ME)
export(chart.Posn)
Added: pkg/blotter/R/addAcctTxn.R
===================================================================
--- pkg/blotter/R/addAcctTxn.R (rev 0)
+++ pkg/blotter/R/addAcctTxn.R 2013-07-02 15:43:37 UTC (rev 1481)
@@ -0,0 +1,42 @@
+#' Add capital account transactions, such as capital additions and withdrawals or interest income (expense)
+#'
+#' For the specified Account, take in the date, amount, and type of transaction and append it to the correct list in the account object
+#'
+#' @param name Account name, as string
+#' @param TxnDate transaction date as ISO 8601, e.g., '2008-09-01' or '2010-01-05 09:54:23.12345'
+#' @param TxnType string indicating the type of account transaction, only "Addition", "Withdrawal", or "Interest" are currently supported
+#' @param amount As of now, the currency of the transaction MUST MATCH the currency of the Account. Patches welcome.
+#' @param \dots any other passthrough parameters
+#' @param verbose If TRUE (default) the function prints the elements of the transaction in a line to the screen, e.g., "2007-01-08 Withdrawal 15,012,235". Suppress using FALSE.
+#' @details
+#' Adds capital transactions to a rudimentary transactions table in the Account object. This may be useful when tracking the denominator of returns when there are changes to the account's capital or significant interest income.
+#' In the Account$summary table, there are several placeholder columns that mimic the CFTC's 13-column report. Columns of interest here are "Additions", "Withdrawals", and "Interest".
+#' Transactions added with this function will be added into the appropriate one of three slots in the Account object (Account$additions, Account$withdrawals, or Account$Interest), which contains an xts object of individual transactions with a date and amount. The \code{\link{updateAcct}} function will read the transactions from each list in turn, aggregate them by the specified date scope, and slot them into the \code{Account$summary} table as it's built. \code{\link{UpdateEndEq}} should then just work.
+#'
+#' @seealso \code{\link{initAcct}}, \code{\link{updateAcct}}, \code{\link{updateEndEq}}
+#' @author Peter Carl
+#' @export
+addAcctTxn <- function(Account, TxnDate, TxnType = c("Additions", "Withdrawals", "Interest"), Amount, ..., verbose=TRUE)
+{ # @author Peter Carl
+ aname <- Account
+
+ if(!is.timeBased(TxnDate) ){
+ TxnDate<-as.POSIXct(TxnDate)
+ }
+
+ Account<-get(paste("account",aname,sep='.'),envir=.blotter)
+
+ NewTxn = xts(t(c(Amount)), order.by=TxnDate)
+ # case:
+ switch(TxnType,
+ Additions = { Account$Additions<-rbind(Account$Additions, NewTxn) },
+ Withdrawals = { Account$Withdrawals<-rbind(Account$Withdrawals, NewTxn) },
+ Interest = { Account$Interest<-rbind(Account$Interest, NewTxn) },
+ stop("Not a valid transaction type. Select from 'Additions', 'Withdrawals', or 'Interest'.")
+ )
+
+ if(verbose)
+ print(paste(format(TxnDate, "%Y-%m-%d %H:%M:%S"), TxnType, Amount, sep=" "))
+
+ assign(paste("account",aname,sep='.'), Account, envir=.blotter)
+}
\ No newline at end of file
Modified: pkg/blotter/R/initAcct.R
===================================================================
--- pkg/blotter/R/initAcct.R 2013-06-25 02:45:10 UTC (rev 1480)
+++ pkg/blotter/R/initAcct.R 2013-07-02 15:43:37 UTC (rev 1481)
@@ -27,7 +27,7 @@
#' several different ways and is best left as a function.
#'
#' To get to the CFTC thirteen columns add:
-#' Gross.Realized, Commission, Net.Realized, Int.Income, Ch.Unrealized,
+#' Gross.Realized, Commission, Net.Realized, Interest, Ch.Unrealized,
#' Advisory.Fees, Wealth.Index
#' Again, no need to add Wealth.Index. Eventually, these additional
#' columns will be useful.
@@ -59,7 +59,16 @@
account$portfolios=vector("list",length=length(portfolios))
names(account$portfolios)=portfolios
account$summary = xts( as.matrix(t(c(0,0,0,0,0,0,0,0,0,0,initEq))), order.by=as.POSIXct(initDate,...=...), ...=... )
- colnames(account$summary) = c('Additions', 'Withdrawals', 'Realized.PL', 'Unrealized.PL', 'Int.Income', 'Gross.Trading.PL', 'Txn.Fees', 'Net.Trading.PL', 'Advisory.Fees', 'Net.Performance', 'End.Eq')
+ colnames(account$summary) = c('Additions', 'Withdrawals', 'Realized.PL', 'Unrealized.PL', 'Interest', 'Gross.Trading.PL', 'Txn.Fees', 'Net.Trading.PL', 'Advisory.Fees', 'Net.Performance', 'End.Eq')
+
+ # track capital additions, withdrawals, and interest
+ account$Additions = xts( as.matrix(t(c(0))), order.by=as.POSIXct(initDate,...=...), ...=... )
+ colnames(account$Additions) = c('Additions')
+ account$Withdrawals = xts( as.matrix(t(c(0))), order.by=as.POSIXct(initDate,...=...), ...=... )
+ colnames(account$Withdrawals) = c('Withdrawals')
+ account$Interest = xts( as.matrix(t(c(0))), order.by=as.POSIXct(initDate,...=...), ...=... )
+ colnames(account$Interest) = c('Interest')
+
for(portfolio in portfolios){
account$portfolios[[portfolio]] = .initSummary(initDate=initDate)
}
Modified: pkg/blotter/R/updateAcct.R
===================================================================
--- pkg/blotter/R/updateAcct.R 2013-06-25 02:45:10 UTC (rev 1480)
+++ pkg/blotter/R/updateAcct.R 2013-07-02 15:43:37 UTC (rev 1481)
@@ -87,7 +87,7 @@
obsDates = index(table)
# Now aggregate the portfolio information into the $summary slot
- Attributes = c('Additions', 'Withdrawals', 'Realized.PL', 'Unrealized.PL', 'Int.Income', 'Gross.Trading.PL', 'Txn.Fees', 'Net.Trading.PL', 'Advisory.Fees', 'Net.Performance', 'End.Eq')
+ Attributes = c('Additions', 'Withdrawals', 'Realized.PL', 'Unrealized.PL', 'Interest', 'Gross.Trading.PL', 'Txn.Fees', 'Net.Trading.PL', 'Advisory.Fees', 'Net.Performance', 'End.Eq')
for(Attribute in Attributes) {
switch(Attribute,
@@ -99,9 +99,15 @@
table = .getByPortf(Account, Attribute, Dates)
result = xts(rowSums(table,na.rm=TRUE),order.by=index(table))
},
- Additions = ,
- Withdrawals = ,
- Int.Income = ,
+ Additions = {
+ result = period.apply(Account$Additions[obsDates], endpoints(Account$Additions[obsDates], on=periodicity(table)$units), sum) # aggregates multiple account txns
+ },
+ Withdrawals = {
+ result = period.apply(Account$Withdrawals[obsDates], endpoints(Account$Withdrawals[obsDates], on=periodicity(table)$units), sum)
+ },
+ Interest = {
+ result = period.apply(Account$Interest[obsDates], endpoints(Account$Interest[obsDates], on=periodicity(table)$units), sum)
+ },
Advisory.Fees = ,
Net.Performance = ,
End.Eq = {
@@ -113,6 +119,7 @@
if(is.null(summary)) {summary=result}
else {summary=cbind(summary,result)}
}
+ summary[is.na(summary)] <- 0 # replace any NA's with zero
Account$summary <- rbind(Account$summary, summary)
# This function does not calculate End.Eq
Added: pkg/blotter/sandbox/ex-AcctTxns.R
===================================================================
--- pkg/blotter/sandbox/ex-AcctTxns.R (rev 0)
+++ pkg/blotter/sandbox/ex-AcctTxns.R 2013-07-02 15:43:37 UTC (rev 1481)
@@ -0,0 +1,56 @@
+# Example for testing new account txn functionality
+
+# load required packages
+require(blotter)
+# Try to clean up in case the demo was run previously
+try(rm("account.a","portfolio.p",pos=.blotter),silent=TRUE)
+
+
+# Make sure timezone is set correctly
+Sys.setenv(TZ="UTC")
+
+# Define a currency and a couple stocks
+require(FinancialInstrument)
+currency("USD")
+symbols = c("IBM","F")
+for(symbol in symbols){ # establish tradable instruments
+ stock(symbol, currency="USD", multiplier=1)
+}
+
+# Download price data
+require(quantmod)
+getSymbols(symbols, from='2007-01-01', to='2007-01-31', src='yahoo')
+
+# Add a portfolio with some transactions
+initPortf('p', symbols=symbols, currency="USD")
+
+addTxn(Portfolio = "p", Symbol = "IBM", TxnDate = '2007-01-03', TxnQty = 50,
+ TxnPrice = 96.5, TxnFees = -0.05*50)
+addTxn("p", "IBM", '2007-01-04', 50, 97.1, TxnFees = -0.05*50)
+addTxn("p", "F", '2007-01-03', -100, 7.60, TxnFees = pennyPerShare(-100))
+addTxn("p", "F", '2007-01-04', 50, 7.70, TxnFees = pennyPerShare(50))
+addTxn("p", "F", '2007-01-10', 50, 7.78, TxnFees = pennyPerShare(50))
+
+updatePortf(Portfolio="p",Dates='2007-01')
+
+initAcct(name="a", portfolios="p", initEq=10000, currency="USD")
+
+# Add transactions at the account level for capital additions, withdrawals, and interest income
+addAcctTxn("a", TxnDate=as.POSIXct("2007-01-05"), TxnType="Additions", Amount=5000)
+addAcctTxn("a", TxnDate=as.POSIXct("2007-01-05"), TxnType="Additions", Amount=1000)
+addAcctTxn("a", TxnDate=as.POSIXct("2007-01-05"), TxnType="Additions", Amount=600)
+addAcctTxn("a", TxnDate=as.POSIXct("2007-01-09"), TxnType="Additions", Amount=5000)
+addAcctTxn("a", TxnDate=as.POSIXct("2007-01-09"), TxnType="Additions", Amount=1000)
+# @TODO: Add a check for positive/negative values in each type
+addAcctTxn("a", TxnDate=as.POSIXct("2007-01-22"), TxnType="Withdrawals", Amount=-1600)
+addAcctTxn("a", TxnDate=as.POSIXct("2007-01-31"), TxnType="Withdrawals", Amount=-5000)
+addAcctTxn("a", TxnDate=as.POSIXct("2007-01-31"), TxnType="Withdrawals", Amount=-1000)
+addAcctTxn("a", TxnDate=as.POSIXct("2007-01-31"), TxnType="Interest", Amount=5.25)
+a=getAccount("a")
+# debug(updateAcct)
+updateAcct("a",'2007-01')
+updateEndEq("a",'2007-01')
+
+a=getAccount("a")
+a$summary
+
More information about the Blotter-commits
mailing list