[Blotter-commits] r489 - in pkg: FinancialInstrument/R blotter/R
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Fri Dec 10 19:50:13 CET 2010
Author: braverock
Date: 2010-12-10 19:50:13 +0100 (Fri, 10 Dec 2010)
New Revision: 489
Modified:
pkg/FinancialInstrument/R/instrument.R
pkg/blotter/R/PortfReturns.R
pkg/blotter/R/addTxn.R
pkg/blotter/R/initAcct.R
Log:
- add bond_series instrument
- minor updates to roxygen comments
Modified: pkg/FinancialInstrument/R/instrument.R
===================================================================
--- pkg/FinancialInstrument/R/instrument.R 2010-12-10 18:16:12 UTC (rev 488)
+++ pkg/FinancialInstrument/R/instrument.R 2010-12-10 18:50:13 UTC (rev 489)
@@ -157,11 +157,13 @@
#' @param suffix_id string suffix that should be associated with the series, usually something like 'Z9' or 'Mar10' denoting expiration and year
#' @param first_traded string coercible to Date for first trading day
#' @param expires string coercible to Date for expiration date
+#' @param maturity string coercible to Date for maturity date of bond series
#' @param identifiers named list of any other identifiers that should also be stored for this instrument
#' @param ... any other passthru parameters
#' @aliases
#' option_series
#' future_series
+#' bond_series
#' @export
future_series <- function(primary_id , suffix_id, first_traded=NULL, expires=NULL, identifiers = NULL, ...){
contract<-try(getInstrument(primary_id))
@@ -289,7 +291,43 @@
bond_temp = instrument(primary_id=primary_id , currency=currency , multiplier=multiplier , tick_size=tick_size, identifiers = identifiers, ..., type="bond", assign_i=TRUE )
}
-
+#' @export
+bond_series <- function(primary_id , suffix_id, ..., first_traded=NULL, maturity=NULL, identifiers = NULL, payment_schedule=NULL){
+ contract<-try(getInstrument(primary_id))
+ if(!inherits(contract,"bond")) stop("bonds contract spec must be defined first")
+
+ # TODO add check for Date equivalent in first_traded and expires
+
+ ## with bond series we probably need to be more sophisticated,
+ ## and find the existing series from prior periods (probably years or months)
+ ## and then add the first_traded and expires to the time series by splicing
+ id<-paste(primary_id, suffix_id,sep="_")
+ temp_series<-try(getInstrument(id),silent=TRUE)
+ if(inherits(temp_series,"bond_series")) {
+ message("updating existing first_traded and maturity for ",id)
+ temp_series$first_traded<-c(temp_series$first_traded,first_traded)
+ temp_series$expires<-c(temp_series$expires,expires)
+ assign(id, temp_series, envir=as.environment(.instrument))
+ } else {
+ dargs<-list(...)
+ dargs$currency=NULL
+ dargs$multiplier=NULL
+ dargs$type=NULL
+ temp_series = instrument( primary_id = id,
+ suffix_id=suffix_id,
+ currency = contract$currency,
+ multiplier = contract$multiplier,
+ tick_size=contract$tick_size,
+ first_traded = first_traded,
+ maturity = maturity,
+ identifiers = identifiers,
+ type=c("bond_series", "bond"),
+ ...=dargs,
+ assign_i=TRUE
+ )
+ }
+}
+
#' primary accessor function for getting objects of type 'instrument'
#' @param x string identifier of instrument to retrieve
#' @param Dates date range to retrieve 'as of', may not currently be implemented
Modified: pkg/blotter/R/PortfReturns.R
===================================================================
--- pkg/blotter/R/PortfReturns.R 2010-12-10 18:16:12 UTC (rev 488)
+++ pkg/blotter/R/PortfReturns.R 2010-12-10 18:50:13 UTC (rev 489)
@@ -4,14 +4,6 @@
#' in the portfolio or portfolios that make up an account. These columns will be additive
#' to return on capital of each portfolio, or of the entire account.
#'
-#' TODO handle portfolio and account in different currencies (not hard, just not done)
-#'
-#' TODO explicitly handle portfolio weights
-#'
-#' TODO provide additional methods of calculating returns
-#'
-#' TODO support additions and withdrawals to available capital
-#'
#' This function exists because of R/Finance community requests by Mark Breman and Thomas Bolton
#' @param Account string name of the account to generate returns for
#' @param method for now, only 'contribution' is supported
@@ -19,6 +11,13 @@
#' @param Dates xts style ISO 8601 date subset to retrieve, default NULL (all dates)
#' @param Portfolios concatenated string vector for portfolio names to retrieve returns on, default NULL (all portfolios)
#' @export
+#' TODO handle portfolio and account in different currencies (not hard, just not done)
+#'
+#' TODO explicitly handle portfolio weights
+#'
+#' TODO provide additional methods of calculating returns
+#'
+#' TODO support additions and withdrawals to available capital
PortfReturns <- function (Account, method=c('contribution'),...,Dates=NULL,Portfolios=NULL)
{ # @author Brian Peterson
aname<-Account
Modified: pkg/blotter/R/addTxn.R
===================================================================
--- pkg/blotter/R/addTxn.R 2010-12-10 18:16:12 UTC (rev 488)
+++ pkg/blotter/R/addTxn.R 2010-12-10 18:50:13 UTC (rev 489)
@@ -90,8 +90,6 @@
}
#' Add multiple transactions to a portfolio
-#'
-#' TODO figure out if we can fully vectorize this function to make it faster
#' @param Portfolio A portfolio name that points to a portfolio object structured with initPortf()
#' @param Symbol An instrument identifier for a symbol included in the portfolio,e.g., IBM
#' @param TxnData An xts object containing all required txn fields
@@ -99,6 +97,7 @@
#' @param verbose If TRUE (default) the function prints the elements of the transaction in a line to the screen, e.g., "2007-01-08 IBM 50 @ 77.6". Suppress using FALSE.
#' @param ConMult Contract or instrument multiplier for the Symbol if it is not defined in an instrument specification
#' @seealso \code{\link{addTxn}}, \code{\link{initPortf}}
+#' TODO figure out if we can fully vectorize this function to make it faster
addTxns<- function(Portfolio, Symbol, TxnData , verbose=TRUE, ..., ConMult=NULL)
{
pname<-Portfolio
@@ -164,10 +163,6 @@
#'
#' Adding a cash dividend does not affect position quantity, like a split would.
#'
-#' # TODO add TxnTypes to $txn table
-#'
-#' # TODO add AsOfDate
-#'
#' @param Portfolio A portfolio name that points to a portfolio object structured with initPortf().
#' @param Symbol An instrument identifier for a symbol included in the portfolio,e.g., IBM.
#' @param TxnDate Transaction date as ISO 8601, e.g., '2008-09-01' or '2010-01-05 09:54:23.12345'.
@@ -177,6 +172,10 @@
#' @param verbose If TRUE (default) the function prints the elements of the transaction in a line to the screen, e.g., "2007-01-08 IBM 50 @ 77.6". Suppress using FALSE.
#' @param ConMult Contract or instrument multiplier for the Symbol if it is not defined in an instrument specification.
#' @export
+#' # TODO add TxnTypes to $txn table
+#'
+#' # TODO add AsOfDate
+#'
addDiv <- function(Portfolio, Symbol, TxnDate, DivPerShare, ..., TxnFees=0, ConMult=NULL, verbose=TRUE)
{ # @author Peter Carl
pname<-Portfolio
Modified: pkg/blotter/R/initAcct.R
===================================================================
--- pkg/blotter/R/initAcct.R 2010-12-10 18:16:12 UTC (rev 488)
+++ pkg/blotter/R/initAcct.R 2010-12-10 18:50:13 UTC (rev 489)
@@ -33,18 +33,19 @@
#' columns will be useful.
#' Gross.Realized will be calculated as (Net) Realized.PL + Txn.Fees
#'
+#' @param name Account name, as string
+#' @param portfolios character vector of strigs naming portfolios included in this account
+#' @param initDate A date prior to the first close price given, used to contain initial account equity and initial position
+#' @param currency ISO currency identifier used to locate the portfolio currency
+#' @param initEq initial account equity in the currency of the portfolio, as a floating point number.
+#' @export
#' TODO Add calcPeriodROR function
+#'
#' TODO Adddd functions addCapital, drawCapital, addFees
+#'
#' initDate and initEq can be used in addCapital to initalize the account?
#' Track cash at this level???
#' Calc gross PL and subtract fees? Or calc net PL and add fees.
-#'
-#' @param name
-#' @param portfolios
-#' @param initDate
-#' @param initEq
-#' @param currency
-#' @export
initAcct <- function(name='default', portfolios, initDate="1950-01-01", initEq=0, currency='USD')
{ # @author Peter Carl
More information about the Blotter-commits
mailing list