[Blotter-commits] r1239 - in pkg/blotter: . R man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Mon Nov 5 22:24:09 CET 2012
Author: braverock
Date: 2012-11-05 22:24:09 +0100 (Mon, 05 Nov 2012)
New Revision: 1239
Modified:
pkg/blotter/DESCRIPTION
pkg/blotter/NAMESPACE
pkg/blotter/R/calcPortfWgt.R
pkg/blotter/R/getByPortf.R
pkg/blotter/R/perTradeStats.R
pkg/blotter/R/tradeStats.R
pkg/blotter/man/calcPortfWgt.Rd
pkg/blotter/man/tradeQuantiles.Rd
pkg/blotter/man/tradeStats.Rd
Log:
- fix and export calcPortfWgt
- minor doc updates
Modified: pkg/blotter/DESCRIPTION
===================================================================
--- pkg/blotter/DESCRIPTION 2012-11-04 14:50:22 UTC (rev 1238)
+++ pkg/blotter/DESCRIPTION 2012-11-05 21:24:09 UTC (rev 1239)
@@ -2,7 +2,7 @@
Type: Package
Title: Tools for transaction-oriented trading systems
development.
-Version: 0.8.12
+Version: 0.8.13
Date: $Date$
Author: Peter Carl, Brian G. Peterson
Maintainer: Brian G. Peterson <brian at braverock.com>
@@ -14,7 +14,7 @@
License: GPL
LazyLoad: yes
Depends:
- R (>= 2.11.1),
+ R (>= 2.15),
xts (>= 0.7-6.17),
zoo,
FinancialInstrument(>= 0.6.3)
@@ -23,41 +23,7 @@
Hmisc,
RUnit,
quantmod
-Contributors: Dirk Eddelbuettel, Jan Humme, Lance Levenson,
+Contributors: Dirk Eddelbuettel, Jan Humme, Lance Levenson,
Ben McCann, Jeff Ryan, Garrett See, Joshua Ulrich, Wolfgang Wu
URL: https://r-forge.r-project.org/projects/blotter/
-Copyright: (c) 2008-2011
-Collate:
- 'addPortfInstr.R'
- 'addTxn.R'
- 'calcPortfWgt.R'
- 'calcPosAvgCost.R'
- 'calcTxnAvgCost.R'
- 'calcTxnValue.R'
- 'chart.ME.R'
- 'chart.Posn.R'
- 'chart.Reconcile.R'
- 'chart.Spread.R'
- 'extractTests.R'
- 'getAccount.R'
- 'getByPortf.R'
- 'getBySymbol.R'
- 'getEndEq.R'
- 'getPortfAcct.R'
- 'getPortfolio.R'
- 'getPosAvgCost.R'
- 'getPosQty.R'
- 'getPos.R'
- 'getTxn.R'
- 'initAcct.R'
- 'initPortf.R'
- 'initPosPL.R'
- 'initSummary.R'
- 'initTxn.R'
- 'PortfReturns.R'
- 'tradeStats.R'
- 'updateAcct.R'
- 'updateEndEq.R'
- 'updatePortf.R'
- 'updatePosPL.R'
- 'perTradeStats.R'
+Copyright: (c) 2008-2012
Modified: pkg/blotter/NAMESPACE
===================================================================
--- pkg/blotter/NAMESPACE 2012-11-04 14:50:22 UTC (rev 1238)
+++ pkg/blotter/NAMESPACE 2012-11-05 21:24:09 UTC (rev 1239)
@@ -1,6 +1,7 @@
export(addDiv)
export(addPortfInstr)
export(addTxn)
+export(calcPortfWgt)
export(chart.ME)
export(chart.Posn)
export(chart.Reconcile)
Modified: pkg/blotter/R/calcPortfWgt.R
===================================================================
--- pkg/blotter/R/calcPortfWgt.R 2012-11-04 14:50:22 UTC (rev 1238)
+++ pkg/blotter/R/calcPortfWgt.R 2012-11-05 21:24:09 UTC (rev 1239)
@@ -1,25 +1,43 @@
#' Calculates the portfolio weights for positions within a given portfolio.
#'
#' Portfolio weights may be calculated differently depending on their use.
+#' By default, this function uses denominator of 'Gross.Value', the second most common
+#' option will likely be 'Net.Value'.
+#' For separating long and short weights, 'Long.Value' and 'Short.Value' may be
+#' needed as denominators.
#'
#' @return xts timeseries object with weights by date in rows and symbolname in columns
#' @param Portfolio a portfolio object structured with initPortf()
#' @param Symbols an instrument identifier for a symbol included in the portfolio
#' @param Dates dates to return the calculation over formatted as xts range
-#' @param denominator string describing the deniminator, see usage
+#' @param denominator string describing the deniminator, see Description
#' @param Account an Account object containing Portfolio summaries
-calcPortfWgt <- function(Portfolio, Symbols = NULL, Dates = NULL,
- denominator = c('Gross.Value', 'Net.Value', 'Long.Value', 'Short.Value'),
- Account = NULL)
-{ # @author Peter Carl
+#' @export
+calcPortfWgt <- function(Portfolio,
+ Symbols = NULL,
+ Dates = NULL,
+ denominator = c('Gross.Value', 'Net.Value', 'Long.Value', 'Short.Value'),
+ Account)
+{ # @author Peter Carl, Brian Peterson
- # FUNCTION
+ zerofill <- function (x)
+ { # kind of like PerformanceAnalytics, but not quite
+ for (column in 1:NCOL(x)) {
+ x[,column] <- ifelse(is.na(x[,column]),0, x[,column])
+ }
+ return(x)
+ }
+
+ pname<-Portfolio
+ Portfolio<-getPortfolio(pname) # TODO add Date handling
+
+ if(is.null(Symbols)) Symbols<-names(Portfolio$symbols)
+
+ pos.value = .getBySymbol(Portfolio = Portfolio, Dates = Dates, Attribute = "Pos.Value", Symbols = Symbols)
+ portf.value = .getByPortf(Account=getAccount(Account),Attribute = denominator[1], Date = Dates)
+ weights = zerofill(as.data.frame(lapply(pos.value, FUN = function(x,y){return(x/y)}, y=portf.value)))
-# 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)
-
-# return(weights)
+ return(weights)
}
###############################################################################
Modified: pkg/blotter/R/getByPortf.R
===================================================================
--- pkg/blotter/R/getByPortf.R 2012-11-04 14:50:22 UTC (rev 1238)
+++ pkg/blotter/R/getByPortf.R 2012-11-05 21:24:09 UTC (rev 1239)
@@ -27,7 +27,8 @@
table = NULL
portfolios=names(Account$portfolios)
for (portfolio in portfolios) {
- tmp_col= Account$portfolios[[portfolio]][Dates,Attribute,drop=FALSE]
+ if (!is.null(Dates))tmp_col= Account$portfolios[[portfolio]][Dates,Attribute,drop=FALSE]
+ else tmp_col= Account$portfolios[[portfolio]][,Attribute,drop=FALSE]
colnames(tmp_col)<-portfolio
if(is.null(table)) table = tmp_col
else table = cbind(table, tmp_col)
Modified: pkg/blotter/R/perTradeStats.R
===================================================================
--- pkg/blotter/R/perTradeStats.R 2012-11-04 14:50:22 UTC (rev 1238)
+++ pkg/blotter/R/perTradeStats.R 2012-11-05 21:24:09 UTC (rev 1239)
@@ -146,7 +146,7 @@
#' the maximum cumulative P&L achieved for each \code{scale} you request.
#' Tomasini&Jaekle recommend plotting MAE or MFE with respect to cumulative P&L
#' and choosing a stop or profit target in the 'stable region'. The reported
-#' max should help the user to locate the stable region, perhaps mechnaically.
+#' max should help the user to locate the stable region, perhaps mechanically.
#' There is room for improvement here, but this should give the user
#' information to work with in addition to the raw quantiles.
#' For example, it may make more sense to use the max of a loess or
Modified: pkg/blotter/R/tradeStats.R
===================================================================
--- pkg/blotter/R/tradeStats.R 2012-11-04 14:50:22 UTC (rev 1238)
+++ pkg/blotter/R/tradeStats.R 2012-11-05 21:24:09 UTC (rev 1239)
@@ -66,7 +66,7 @@
#' \item{Med.Daily.PL}{ median daily P&L }
#' \item{Std.Dev.Daily.PL}{ standard deviation of daliy P&L }
#' \item{Max.Drawdown}{ max drawdown }
-#' \item{Avg.WinLoss.Ratio}{ ration of mean winning over mean losing trade }
+#' \item{Avg.WinLoss.Ratio}{ ratio of mean winning over mean losing trade }
#' \item{Med.WinLoss.Ratio}{ ratio of median winning trade over mean losing trade }
#' \item{Max.Equity}{ maximum account equity }
#' \item{Min.Equity}{ minimum account equity }
Modified: pkg/blotter/man/calcPortfWgt.Rd
===================================================================
--- pkg/blotter/man/calcPortfWgt.Rd 2012-11-04 14:50:22 UTC (rev 1238)
+++ pkg/blotter/man/calcPortfWgt.Rd 2012-11-05 21:24:09 UTC (rev 1239)
@@ -4,7 +4,7 @@
\usage{
calcPortfWgt(Portfolio, Symbols = NULL, Dates = NULL,
denominator = c("Gross.Value", "Net.Value", "Long.Value", "Short.Value"),
- Account = NULL)
+ Account)
}
\arguments{
\item{Portfolio}{a portfolio object structured with
@@ -17,7 +17,7 @@
formatted as xts range}
\item{denominator}{string describing the deniminator, see
- usage}
+ Description}
\item{Account}{an Account object containing Portfolio
summaries}
@@ -28,6 +28,10 @@
}
\description{
Portfolio weights may be calculated differently depending
- on their use.
+ on their use. By default, this function uses denominator
+ of 'Gross.Value', the second most common option will
+ likely be 'Net.Value'. For separating long and short
+ weights, 'Long.Value' and 'Short.Value' may be needed as
+ denominators.
}
Modified: pkg/blotter/man/tradeQuantiles.Rd
===================================================================
--- pkg/blotter/man/tradeQuantiles.Rd 2012-11-04 14:50:22 UTC (rev 1238)
+++ pkg/blotter/man/tradeQuantiles.Rd 2012-11-05 21:24:09 UTC (rev 1239)
@@ -49,7 +49,7 @@
plotting MAE or MFE with respect to cumulative P&L and
choosing a stop or profit target in the 'stable region'.
The reported max should help the user to locate the
- stable region, perhaps mechnaically. There is room for
+ stable region, perhaps mechanically. There is room for
improvement here, but this should give the user
information to work with in addition to the raw
quantiles. For example, it may make more sense to use the
Modified: pkg/blotter/man/tradeStats.Rd
===================================================================
--- pkg/blotter/man/tradeStats.Rd 2012-11-04 14:50:22 UTC (rev 1238)
+++ pkg/blotter/man/tradeStats.Rd 2012-11-05 21:24:09 UTC (rev 1239)
@@ -41,7 +41,7 @@
\item{Avg.Daily.PL}{mean daily P&L } \item{Med.Daily.PL}{
median daily P&L } \item{Std.Dev.Daily.PL}{ standard
deviation of daliy P&L } \item{Max.Drawdown}{ max
- drawdown } \item{Avg.WinLoss.Ratio}{ ration of mean
+ drawdown } \item{Avg.WinLoss.Ratio}{ ratio of mean
winning over mean losing trade }
\item{Med.WinLoss.Ratio}{ ratio of median winning trade
over mean losing trade } \item{Max.Equity}{ maximum
More information about the Blotter-commits
mailing list