From noreply at r-forge.r-project.org Tue Jul 2 17:43:37 2013 From: noreply at r-forge.r-project.org (noreply at r-forge.r-project.org) Date: Tue, 2 Jul 2013 17:43:37 +0200 (CEST) Subject: [Blotter-commits] r1481 - in pkg/blotter: . R sandbox Message-ID: <20130702154337.E5469183263@r-forge.r-project.org> 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 + From noreply at r-forge.r-project.org Tue Jul 2 17:45:19 2013 From: noreply at r-forge.r-project.org (noreply at r-forge.r-project.org) Date: Tue, 2 Jul 2013 17:45:19 +0200 (CEST) Subject: [Blotter-commits] r1482 - pkg/blotter Message-ID: <20130702154519.CE459184AAF@r-forge.r-project.org> Author: peter_carl Date: 2013-07-02 17:45:19 +0200 (Tue, 02 Jul 2013) New Revision: 1482 Modified: pkg/blotter/DESCRIPTION Log: - added addAcctTxn to collate Modified: pkg/blotter/DESCRIPTION =================================================================== --- pkg/blotter/DESCRIPTION 2013-07-02 15:43:37 UTC (rev 1481) +++ pkg/blotter/DESCRIPTION 2013-07-02 15:45:19 UTC (rev 1482) @@ -32,6 +32,7 @@ 'PortfReturns.R' 'addPortfInstr.R' 'addTxn.R' + 'addAcctTxn.R' 'calcPortfWgt.R' 'calcPosAvgCost.R' 'calcTxnAvgCost.R' From noreply at r-forge.r-project.org Mon Jul 22 03:27:15 2013 From: noreply at r-forge.r-project.org (noreply at r-forge.r-project.org) Date: Mon, 22 Jul 2013 03:27:15 +0200 (CEST) Subject: [Blotter-commits] r1483 - pkg/blotter/demo Message-ID: <20130722012716.16EB9183C75@r-forge.r-project.org> Author: bodanker Date: 2013-07-22 03:27:14 +0200 (Mon, 22 Jul 2013) New Revision: 1483 Modified: pkg/blotter/demo/longtrend.R pkg/blotter/demo/turtles.R Log: - update demos to run with new quantmod/xts code Modified: pkg/blotter/demo/longtrend.R =================================================================== --- pkg/blotter/demo/longtrend.R 2013-07-02 15:45:19 UTC (rev 1482) +++ pkg/blotter/demo/longtrend.R 2013-07-22 01:27:14 UTC (rev 1483) @@ -47,6 +47,8 @@ require(TTR) require(blotter) +Sys.setenv(TZ="UTC") + # Try to clean up in case the demo was run previously try(rm("account.longtrend","portfolio.longtrend",pos=.blotter),silent=TRUE) try(rm("ltaccount","ltportfolio","ClosePrice","CurrentDate","equity","GSPC","i","initDate","initEq","Posn","UnitSize","verbose"),silent=TRUE) @@ -61,7 +63,7 @@ currency("USD") stock("GSPC",currency="USD",multiplier=1) getSymbols('^GSPC', src='yahoo', index.class=c("POSIXt","POSIXct"),from='1998-01-01') -GSPC=to.monthly(GSPC, indexAt='endof') +GSPC=to.monthly(GSPC, indexAt='endof', drop.time=FALSE) # Set up indicators with TTR print("Setting up indicators") Modified: pkg/blotter/demo/turtles.R =================================================================== --- pkg/blotter/demo/turtles.R 2013-07-02 15:45:19 UTC (rev 1482) +++ pkg/blotter/demo/turtles.R 2013-07-22 01:27:14 UTC (rev 1483) @@ -6,6 +6,8 @@ require(TTR) require(blotter) +Sys.setenv(TZ="UTC") + # Try to clean up in case the demo was run previously try(rm("account.turtles","portfolio.turtles",pos=.blotter),silent=TRUE) try(rm("portfolio","account","N","symbol","symbols","ClosePrice","CurrentDate","equity","Units","maxUnits","size","Stop","equity","TxnPrice","initDate","initEq","Posn","verbose"),silent=TRUE) From noreply at r-forge.r-project.org Mon Jul 22 23:21:13 2013 From: noreply at r-forge.r-project.org (noreply at r-forge.r-project.org) Date: Mon, 22 Jul 2013 23:21:13 +0200 (CEST) Subject: [Blotter-commits] r1484 - pkg/blotter/R Message-ID: <20130722212113.4288318443C@r-forge.r-project.org> Author: peter_carl Date: 2013-07-22 23:21:12 +0200 (Mon, 22 Jul 2013) New Revision: 1484 Modified: pkg/blotter/R/updateAcct.R Log: - fixed condition where periodicity cannot be determined in account summary of attributes Modified: pkg/blotter/R/updateAcct.R =================================================================== --- pkg/blotter/R/updateAcct.R 2013-07-22 01:27:14 UTC (rev 1483) +++ pkg/blotter/R/updateAcct.R 2013-07-22 21:21:12 UTC (rev 1484) @@ -85,6 +85,10 @@ table = .getByPortf(Account, 'Net.Trading.PL', Dates) obsLength = length(index(table)) obsDates = index(table) + if(obsLength > 1) # can't estimate periodicity of one observation + on=periodicity(table)$units + else + on="none" # Now aggregate the portfolio information into the $summary slot Attributes = c('Additions', 'Withdrawals', 'Realized.PL', 'Unrealized.PL', 'Interest', 'Gross.Trading.PL', 'Txn.Fees', 'Net.Trading.PL', 'Advisory.Fees', 'Net.Performance', 'End.Eq') @@ -100,13 +104,22 @@ result = xts(rowSums(table,na.rm=TRUE),order.by=index(table)) }, Additions = { - result = period.apply(Account$Additions[obsDates], endpoints(Account$Additions[obsDates], on=periodicity(table)$units), sum) # aggregates multiple account txns + result = if(on=="none") + as.xts(sum(Account$Additions[paste("::",obsDates, sep="")]), order.by=index(table)) + else + period.apply(Account$Additions[obsDates], endpoints(Account$Additions[obsDates], on=on), sum) # aggregates multiple account txns }, Withdrawals = { - result = period.apply(Account$Withdrawals[obsDates], endpoints(Account$Withdrawals[obsDates], on=periodicity(table)$units), sum) + result = if(on=="none") + as.xts(sum(Account$Withdrawals[paste("::",obsDates, sep="")]), order.by=index(table)) + else + 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) + result = if(on=="none") + as.xts(sum(Account$Interest[paste("::",obsDates, sep="")]),, order.by=index(table)) + else + period.apply(Account$Interest[obsDates], endpoints(Account$Interest[obsDates], on=periodicity(table)$units), sum) }, Advisory.Fees = , Net.Performance = , From noreply at r-forge.r-project.org Tue Jul 23 19:49:22 2013 From: noreply at r-forge.r-project.org (noreply at r-forge.r-project.org) Date: Tue, 23 Jul 2013 19:49:22 +0200 (CEST) Subject: [Blotter-commits] r1485 - pkg/blotter/R Message-ID: <20130723174923.04234185450@r-forge.r-project.org> Author: bodanker Date: 2013-07-23 19:49:22 +0200 (Tue, 23 Jul 2013) New Revision: 1485 Modified: pkg/blotter/R/chart.Posn.R Log: - only use first element of Symbol Modified: pkg/blotter/R/chart.Posn.R =================================================================== --- pkg/blotter/R/chart.Posn.R 2013-07-22 21:21:12 UTC (rev 1484) +++ pkg/blotter/R/chart.Posn.R 2013-07-23 17:49:22 UTC (rev 1485) @@ -14,6 +14,7 @@ pname<-Portfolio Portfolio<-getPortfolio(pname) if (missing(Symbol)) Symbol <- names(Portfolio$symbols)[[1]] + else Symbol <- Symbol[1] # FUNCTION require(quantmod)