[Blotter-commits] r214 - in pkg/blotter: R man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Thu Jan 28 23:19:28 CET 2010
Author: braverock
Date: 2010-01-28 23:19:27 +0100 (Thu, 28 Jan 2010)
New Revision: 214
Modified:
pkg/blotter/R/calcAcctAttr.R
pkg/blotter/R/calcPortfAttr.R
pkg/blotter/R/calcPortfSummary.R
pkg/blotter/R/calcPortfWgt.R
pkg/blotter/R/getByPortf.R
pkg/blotter/R/getBySymbol.R
pkg/blotter/R/updateAcct.R
pkg/blotter/man/initPortf.Rd
pkg/blotter/man/updatePosPL.Rd
Log:
- further additions to handling date subsets and ranges, mostly related to updateAcct and functions called by it.
Modified: pkg/blotter/R/calcAcctAttr.R
===================================================================
--- pkg/blotter/R/calcAcctAttr.R 2010-01-28 16:03:54 UTC (rev 213)
+++ pkg/blotter/R/calcAcctAttr.R 2010-01-28 22:19:27 UTC (rev 214)
@@ -1,5 +1,4 @@
-`calcAcctAttr` <-
-function(Account, Attribute, Date=NULL)
+calcAcctAttr <- function(Account, Attribute, Dates=NULL)
{ # @author Peter Carl
# DESCRIPTION
@@ -12,13 +11,13 @@
# 'Realized.PL', 'Unrealized.PL', or 'Trading.PL'
portfolios = names(Account)[-1]
- if(is.null(Date)) # if no date is specified, get all available dates
- Date = time(Account[[2]])
- else
- Date = time(Account[[2]][Date])
- table = xts(NULL, order.by=Date) ## Reference time index
- table = .getByPortf(Account, Attribute, Date)
- result = xts(apply(table, FUN='sum', MARGIN=1), Date)
+ if(is.null(Dates)) # if no date is specified, get all available dates
+ Dates = time(Account[[2]])
+# else
+# Dates = time(Account[[2]][Dates])
+
+ table = .getByPortf(Account, Attribute, Dates)
+ result = xts(rowSums(table),order.by=index(table),na.rm=TRUE)
colnames(result) = Attribute
return(result)
}
Modified: pkg/blotter/R/calcPortfAttr.R
===================================================================
--- pkg/blotter/R/calcPortfAttr.R 2010-01-28 16:03:54 UTC (rev 213)
+++ pkg/blotter/R/calcPortfAttr.R 2010-01-28 22:19:27 UTC (rev 214)
@@ -1,54 +1,53 @@
-calcPortfAttr <- function(Portfolio, Attribute, Date=NULL, Symbols = NULL)
+calcPortfAttr <- function(Portfolio, Attribute, Dates=NULL, Symbols = NULL)
{
if(!inherits(Portfolio,"portfolio")) stop("Portfolio passed is not a portfolio object.")
symbols = names(Portfolio)
- if(is.null(Date)|is.na(Date)) # if no date is specified, get all available dates
- Date = time(Portfolio[[1]]$posPL)
- else Date = time(Portfolio[[1]]$posPL[Date])
+ if(is.null(Dates)|is.na(Dates)) # if no date is specified, get all available dates
+ Dates = time(Portfolio[[1]]$posPL)
+# else Dates = time(Portfolio[[1]]$posPL[Dates])
- table = xts(NULL, order.by=Date) ## Reference time index
-
switch(Attribute,
Trading.PL = {
- table = .getBySymbol(Portfolio = Portfolio, Attribute = 'Trading.PL', Date = Date, Symbols = Symbols)
- result = xts(apply(table, FUN='sum', MARGIN=1), Date)
+ table = .getBySymbol(Portfolio = Portfolio, Attribute = 'Trading.PL', Dates = Dates, Symbols = Symbols)
+# result = xts(apply(table, FUN='sum', MARGIN=1), Dates)
+ result = xts(rowSums(table), na.rm=TRUE ,order.by=index(table))
colnames(result) = 'Trading.PL'
},
Txn.Fees = {
- table = .getBySymbol(Portfolio = Portfolio, Attribute = 'Txn.Fees', Date = Date, Symbols = Symbols)
- result = xts(rowSums(table), Date)
+ table = .getBySymbol(Portfolio = Portfolio, Attribute = 'Txn.Fees', Dates = Dates, Symbols = Symbols)
+ result = xts(rowSums(table), na.rm=TRUE ,order.by=index(table))
colnames(result) = 'Txn.Fees'
},
Realized.PL = {
- table = .getBySymbol(Portfolio = Portfolio, Attribute = 'Realized.PL', Date = Date, Symbols = Symbols)
- result = xts(rowSums(table), Date)
+ table = .getBySymbol(Portfolio = Portfolio, Attribute = 'Realized.PL', Dates = Dates, Symbols = Symbols)
+ result = xts(rowSums(table), na.rm=TRUE ,order.by=index(table))
colnames(result) = 'Realized.PL'
},
Unrealized.PL = {
- table = .getBySymbol(Portfolio = Portfolio, Attribute = 'Unrealized.PL', Date = Date, Symbols = Symbols)
- result = xts(rowSums(table), Date)
+ table = .getBySymbol(Portfolio = Portfolio, Attribute = 'Unrealized.PL', Dates = Dates, Symbols = Symbols)
+ result = xts(rowSums(table), na.rm=TRUE ,order.by=index(table))
colnames(result) = 'Unrealized.PL'
},
Net.Value = {
- table = .getBySymbol(Portfolio = Portfolio, Attribute = 'Pos.Value', Date = Date, Symbols = Symbols)
- result = xts(rowSums(table), Date)
+ table = .getBySymbol(Portfolio = Portfolio, Attribute = 'Pos.Value', Dates = Dates, Symbols = Symbols)
+ result = xts(rowSums(table), na.rm=TRUE ,order.by=index(table))
colnames(result) = 'Net.Value'
},
Gross.Value = {
- table = .getBySymbol(Portfolio = Portfolio, Attribute = 'Pos.Value', Date = Date, Symbols = Symbols)
- result = xts(rowSums(abs(table)), Date)
+ table = .getBySymbol(Portfolio = Portfolio, Attribute = 'Pos.Value', Dates = Dates, Symbols = Symbols)
+ result = xts(rowSums(abs(table)), na.rm=TRUE ,order.by=index(table))
colnames(result) = 'Gross.Value'
},
Long.Value = {
- table = .getBySymbol(Portfolio = Portfolio, Attribute = 'Pos.Value', Date = Date, Symbols = Symbols)
- table = apply(table,MARGIN=c(1,2),FUN=max,0)
- result = xts(rowSums(table), Date)
+ table = .getBySymbol(Portfolio = Portfolio, Attribute = 'Pos.Value', Dates = Dates, Symbols = Symbols)
+ tmat = apply(table,MARGIN=c(1,2),FUN=max,0)# comes out a matrix
+ result = xts(rowSums(tmat), na.rm=TRUE ,order.by=index(table))
colnames(result) = 'Long.Value'
},
Short.Value = {
- table = .getBySymbol(Portfolio = Portfolio, Attribute = 'Pos.Value', Date = Date, Symbols = Symbols)
- table = apply(table,MARGIN=c(1,2),FUN=min,0) # comes out a matrix
- result = xts(rowSums(table), Date)
+ table = .getBySymbol(Portfolio = Portfolio, Attribute = 'Pos.Value', Dates = Dates, Symbols = Symbols)
+ tmat = apply(table,MARGIN=c(1,2),FUN=min,0) # comes out a matrix
+ result = xts(rowSums(tmat), na.rm=TRUE ,order.by=index(table))
colnames(result) = 'Short.Value'
}
)
Modified: pkg/blotter/R/calcPortfSummary.R
===================================================================
--- pkg/blotter/R/calcPortfSummary.R 2010-01-28 16:03:54 UTC (rev 213)
+++ pkg/blotter/R/calcPortfSummary.R 2010-01-28 22:19:27 UTC (rev 214)
@@ -1,23 +1,23 @@
-calcPortfSummary <- function(Portfolio, Date=NULL)
+calcPortfSummary <- function(Portfolio, Dates=NULL)
{ # @ author Peter Carl
if(!inherits(Portfolio,"portfolio")) stop("Portfolio passed is not a portfolio object.")
# DESCRIPTION
# Create portfolio summary with the following columns
# Long.Value, Short.Value, Net.Value, Trading.PL
- if(is.null(Date) | is.na(Date)) # if no date is specified, get all available dates
- Date = time(Portfolio[[1]]$posPL )
- else Date = time(Portfolio[[1]]$posPL[Date])
+ if(is.null(Dates) | is.na(Dates)) # if no date is specified, get all available dates
+ Dates = time(Portfolio[[1]]$posPL )
+# else Dates = time(Portfolio[[1]]$posPL[Dates])
- TradingPL = calcPortfAttr(Portfolio, 'Trading.PL', Date)
- RealizedPL = calcPortfAttr(Portfolio, 'Realized.PL', Date)
- UnrealizedPL = calcPortfAttr(Portfolio, 'Unrealized.PL', Date)
+ TradingPL = calcPortfAttr(Portfolio, 'Trading.PL', Dates)
+ RealizedPL = calcPortfAttr(Portfolio, 'Realized.PL', Dates)
+ UnrealizedPL = calcPortfAttr(Portfolio, 'Unrealized.PL', Dates)
# UnrealizedPL = TradingPL - RealizedPL
- TxnFees = calcPortfAttr(Portfolio, 'Txn.Fees', Date)
- NetValue = calcPortfAttr(Portfolio, 'Net.Value', Date)
- GrossValue = calcPortfAttr(Portfolio, 'Gross.Value', Date)
- LongValue = calcPortfAttr(Portfolio, 'Long.Value', Date)
- ShortValue = calcPortfAttr(Portfolio, 'Short.Value', Date)
+ TxnFees = calcPortfAttr(Portfolio, 'Txn.Fees', Dates)
+ NetValue = calcPortfAttr(Portfolio, 'Net.Value', Dates)
+ GrossValue = calcPortfAttr(Portfolio, 'Gross.Value', Dates)
+ LongValue = calcPortfAttr(Portfolio, 'Long.Value', Dates)
+ ShortValue = calcPortfAttr(Portfolio, 'Short.Value', Dates)
summary=merge(LongValue, ShortValue, NetValue, GrossValue, TxnFees, RealizedPL, UnrealizedPL, TradingPL)
# colnames(summary)=c('Long.Value', 'Short.Value', 'Net.Value', 'Gross.Value', 'Txn.Fees','Realized.PL', 'Unrealized.PL', 'Trading.PL')
Modified: pkg/blotter/R/calcPortfWgt.R
===================================================================
--- pkg/blotter/R/calcPortfWgt.R 2010-01-28 16:03:54 UTC (rev 213)
+++ pkg/blotter/R/calcPortfWgt.R 2010-01-28 22:19:27 UTC (rev 214)
@@ -16,7 +16,7 @@
# FUNCTION
- pos.value = .getBySymbol(Portfolio = Portfolio, Date = Dates, Attribute = "Pos.Value", Symbols = Symbols)
+ pos.value = .getBySymbol(Portfolio = Portfolio, Dates = Dates, Attribute = "Pos.Value", Symbols = Symbols)
portf.value = calcPortfAttr(Portfolio = Portfolio, Date = Dates, Attribute = denominator[1])
weights = apply(pos.value, MARGIN = 2, FUN = function(x,y){return(x/y)}, y=portf.value)
Modified: pkg/blotter/R/getByPortf.R
===================================================================
--- pkg/blotter/R/getByPortf.R 2010-01-28 16:03:54 UTC (rev 213)
+++ pkg/blotter/R/getByPortf.R 2010-01-28 22:19:27 UTC (rev 214)
@@ -1,4 +1,4 @@
-.getByPortf <- function(Account, Attribute, Date=NULL)
+.getByPortf <- function(Account, Attribute, Dates=NULL)
{ # @author Peter Carl
# DESCRIPTION:
@@ -15,16 +15,18 @@
# regular xts object of values by portfolio
# FUNCTION
- if(is.null(Date)) # if no date is specified, get all available dates
- Date = time(Account[[2]])
- else
- Date = time(Account[[2]][Date])
- table = xts(NULL, order.by=Date)
- ## Need a reference time index
+ if(is.null(Dates)) # if no date is specified, get all available dates
+ Dates = time(Account[[2]])
+
+ table = NULL
+ i=1
portfolios=names(Account)[-1]
- for (i in 1:length(portfolios))
- table = merge(table, Account[[i+1]][Date,Attribute,drop=FALSE])
- colnames(table) = portfolios
+ for (portfolio in portfolios) {
+ tmp_col= Account[[portfolio]][Dates,Attribute,drop=FALSE]
+ colnames(tmp_col)<-portfolio
+ if(is.null(table)) table = tmp_col
+ else table = cbind(table, tmp_col)
+ }
return(table)
}
Modified: pkg/blotter/R/getBySymbol.R
===================================================================
--- pkg/blotter/R/getBySymbol.R 2010-01-28 16:03:54 UTC (rev 213)
+++ pkg/blotter/R/getBySymbol.R 2010-01-28 22:19:27 UTC (rev 214)
@@ -1,4 +1,4 @@
-.getBySymbol <- function(Portfolio, Attribute, Date=NULL, Symbols = NULL)
+.getBySymbol <- function(Portfolio, Attribute, Dates=NULL, Symbols = NULL)
{ # @author Peter Carl
# DESCRIPTION:
@@ -16,12 +16,11 @@
# regular xts object of values by symbol
# FUNCTION
- if(is.null(Date)) # if no date is specified, get all available dates
- Date = time(Portfolio[[1]]$posPL)
- else
- Date = time(Portfolio[[1]]$posPL[Date])
+ if(is.null(Dates)) # if no date is specified, get all available dates
+ Dates = time(Portfolio[[1]]$posPL)
+ # else Dates = time(Portfolio[[1]]$posPL[Dates])
- table = xts(NULL, order.by=Date)
+ table = NULL
## Need a reference time index
if(is.null(Symbols))
symbols=names(Portfolio)
@@ -29,9 +28,12 @@
symbols = Symbols
for (symbol in symbols) {
- table = merge(table, Portfolio[[symbol]]$posPL[Date,Attribute,drop=FALSE])
+ tmp_col = Portfolio[[symbol]]$posPL[Dates,Attribute,drop=FALSE]
+ if(is.null(table)) table = tmp_col
+ else table = merge(table, tmp_col)
}
colnames(table) = symbols
+ class(table)<-class(xts())
return(table)
## TODO: append summary information in last columns based on Attribute requested
Modified: pkg/blotter/R/updateAcct.R
===================================================================
--- pkg/blotter/R/updateAcct.R 2010-01-28 16:03:54 UTC (rev 213)
+++ pkg/blotter/R/updateAcct.R 2010-01-28 22:19:27 UTC (rev 214)
@@ -14,10 +14,7 @@
updateAcct <- function(name='default', Dates=NULL)
{ # @author Peter Carl
- Account<-try(get(paste("account",name,sep='.'), envir=.blotter))
- if(inherits(Account,"try-error"))
- stop(paste("Account",name," not found, use initAcct() to create a new account"))
-
+ Account<-getAccount(name)
# FUNCTION
Portfolios = names(Account)[-1]
@@ -28,27 +25,49 @@
Dates = time(Portfolio[[1]]$posPL ) # if no date is specified, get all available dates
# if(!is.timeBased(Dates) ){
# Dates is an xts range, turn it into a list of Dates
- else Dates = time(Portfolio[[1]]$posPL[Dates])
+# else Dates = time(Portfolio[[1]]$posPL[Dates])
# }
+
+ #TODO FIXME do two loops, one over portfolios to update each portfolio
+ # and one over accounts, to aggregate, probably via matrix addition
+
# For each date, calculate realized and unrealized P&L
for(d in 1:length(Dates)){ # d is a date slot counter
# I shouldn't have to do this but I lose the class for the element when I do
# for(date in Dates)
+
+
# Append the portfolio summary data to the portfolio slot
- for(i in 1:length(Portfolios)){
- Portfolio = getPortfolio(Portfolios[i])
- row = calcPortfSummary(Portfolio, Dates[d])
- Account[[i+1]] = rbind(Account[[i+1]],row)
+ for(pname in Portfolios){
+ Portfolio = getPortfolio(pname)
+
+ CurrentDate = Dates[d]
+ PrevDate = time(Portfolio[[1]]$posPL[Portfolio[[1]]$posPL[CurrentDate,which.i=TRUE]-1 ] ) # which.i is new in [.xts
+ if (length(PrevDate)==0) next() #no price data, keep looking
+ PrevDateWidth = xts:::.parseISO8601(PrevDate)
+ PrevDateLast = PrevDateWidth$last.time
+ CurrentSpan = paste(PrevDateLast, CurrentDate, sep="::")
+
+ rows = calcPortfSummary(Portfolio, CurrentSpan)
+ rows = na.omit(rows)
+ Account[[pname]] = rbind(Account[[pname]],rows)
}
-
+ if(is.null(CurrentSpan)) next()
# Now aggregate the portfolio information into the TOTAL slot
- TxnFees = as.numeric(calcAcctAttr(Account, Attribute = 'Txn.Fees', Date = Dates[d]))
- RealizedPL = as.numeric(calcAcctAttr(Account, 'Realized.PL', Dates[d]))
- UnrealizedPL = as.numeric(calcAcctAttr(Account, 'Unrealized.PL', Dates[d]))
- TradingPL = as.numeric(calcAcctAttr(Account, 'Trading.PL', Dates[d]))
- row = xts(t(c(0, 0, TxnFees, RealizedPL, UnrealizedPL, 0, TradingPL, 0, 0, 0)), order.by=Dates[d])
- colnames(row) = c('Additions', 'Withdrawals', 'Txn.Fees', 'Realized.PL', 'Unrealized.PL', 'Int.Income', 'Trading.PL', 'Advisory.Fees', 'Net.Performance', 'End.Eq')
- Account[['TOTAL']] <- rbind(Account[['TOTAL']], row)
+ TxnFees = calcAcctAttr(Account, Attribute = 'Txn.Fees', CurrentSpan)
+ Additions = xts(rep(0,length(index(TxnFees))),order.by=index(TxnFees))
+ Withdrawals = Additions
+ IntIncome = Additions
+ AdvisoryFees = Additions
+ NetPerformance = Additions
+ EndEq = Additions
+ RealizedPL = calcAcctAttr(Account, 'Realized.PL', CurrentSpan)
+ UnrealizedPL = calcAcctAttr(Account, 'Unrealized.PL', CurrentSpan)
+ TradingPL = calcAcctAttr(Account, 'Trading.PL', CurrentSpan)
+ rows = cbind(Additions, Withdrawals, TxnFees, RealizedPL, UnrealizedPL, IntIncome, TradingPL, AdvisoryFees,NetPerformance, EndEq)
+ browser()
+ colnames(rows) = c('Additions', 'Withdrawals', 'Txn.Fees', 'Realized.PL', 'Unrealized.PL', 'Int.Income', 'Trading.PL', 'Advisory.Fees', 'Net.Performance', 'End.Eq')
+ Account[['TOTAL']] <- rbind(Account[['TOTAL']], rows)
# This function does not calculate End.Eq
}
assign(paste("account",name,sep='.'),Account, envir=.blotter)
Modified: pkg/blotter/man/initPortf.Rd
===================================================================
--- pkg/blotter/man/initPortf.Rd 2010-01-28 16:03:54 UTC (rev 213)
+++ pkg/blotter/man/initPortf.Rd 2010-01-28 22:19:27 UTC (rev 214)
@@ -8,11 +8,12 @@
Initializes a Portfolio object, which contains transactions and position profit-and-loss calculations for a list of symbols.
}
\usage{
-initPortf(symbols, initPosQty = 0, initDate = "1950-01-01")
+initPortf(name='default', symbols, initPosQty = 0, initDate = "1950-01-01")
initTxn(initDate = "1950-01-01", initPosQty = 0)
initPosPL(initDate = "1950-01-01", initPosQty = 0)
}
\arguments{
+ \item{name}{ a unique string identifier for a portfolio }
\item{symbols}{ a list of identfiers of the instruments to be contained in the Portfolio. The name of any associated price objects (xts prices, usually OHLC) should match these }
\item{initDate}{ date (ISO8601) prior to the first close price given, used to contain initial position }
\item{initPosQty}{ a vector of initial positions for each symbol, default is zero }
Modified: pkg/blotter/man/updatePosPL.Rd
===================================================================
--- pkg/blotter/man/updatePosPL.Rd 2010-01-28 16:03:54 UTC (rev 213)
+++ pkg/blotter/man/updatePosPL.Rd 2010-01-28 22:19:27 UTC (rev 214)
@@ -10,7 +10,7 @@
updatePortf(Portfolio, StartDate, EndDate)
}
\arguments{
- \item{Portfolio}{ A portfolio object structured with \code{\link{initPortf}}. }
+ \item{Portfolio}{ the string identifier for a portoflio created by \code{\link{initPortf}} }
\item{Symbol}{ An instrument identifier for a symbol included in the portfolio. }
\item{Dates}{ xts date subset or ISO 8601 Dates from which to calculate profit or loss. }
\item{Prices}{ Close prices in an xts object with a columnname containing a substring "Close". }
More information about the Blotter-commits
mailing list