addTxn: Add transactions to a portfolio.

Description Usage Arguments Details Note Author(s) See Also

View source: R/addTxn.R


When a trade or adjustment is made to the Portfolio, the addTxn function calculates the value and average cost of the transaction, the change in position, the resulting positions average cost, and any realized profit or loss (net of fees) from the transaction. Then it stores the transaction and calculations in the Portfolio object.


addTxn(Portfolio, Symbol, TxnDate, TxnQty, TxnPrice, ..., TxnFees = 0,
  allowRebates = FALSE, ConMult = NULL, verbose = TRUE, eps = 1e-06)

addTxns(Portfolio, Symbol, TxnData, verbose = FALSE, ..., ConMult = NULL,
  allowRebates = FALSE, eps = 1e-06)



A portfolio name that points to a portfolio object structured with initPortf()


An instrument identifier for a symbol included in the portfolio, e.g., "IBM"


Transaction date as ISO 8601, e.g., '2008-09-01' or '2010-01-05 09:54:23.12345'


Total units (such as shares or contracts) transacted. Positive values indicate a 'buy'; negative values indicate a 'sell'


Price at which the transaction was done


Fees associated with the transaction, e.g. commissions., See Details


whether to allow positive (rebate) TxnFees, default FALSE


Contract/instrument multiplier for the Symbol if it is not defined in an instrument specification


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.


value to add to force unique indices


An xts object containing all required txn fields (for addTxns)


Any other passthrough parameters


Fees are indicated as negative values and will be subtracted from the transaction value. TxnFees can either be a fixed numeric amount, or a function (or charavcter name of a function) in which case the function is evaluated to determine the fee amount.

The pennyPerShare function provides a simple example of a transaction cost function.

Transactions which would cross the position through zero will be split into two transactions, one to flatten the position, and another to initiate a new position on the opposite side of the market. The new (split) transaction will have its timestamp incremented by eps to preserve ordering.

This transaction splitting vastly simplifies realized P&L calculations elsewhere in the code. Such splitting also mirrors many execution platforms and brokerage requirements in particular asset classes where the side of a trade needs to be specified with the order.

The addTxns function allows you to add multiple transactions to the portfolio, which is much faster than adding them one at a time. The TxnData object must have "TxnQty" and "TxnPrice" columns, while the "TxnFees" column is optional.

If TxnFees is the name of a function, the function will be called with TxnFees(TxnQty, TxnPrice, Symbol) so a user supplied fee function must at the very least take dots to avoid an error. We have chosen not to use named arguments to reduce issues from user-supplied fee functions.


The addTxn function will eventually also handle other transaction types, such as adjustments for corporate actions or expire/assign for options. See addDiv


Peter Carl, Brian G. Peterson

See Also

addTxns, pennyPerShare, initPortf

cloudcell/blotter documentation built on May 13, 2019, 8:01 p.m.