addTxn: Add transactions to a portfolio.

Description Usage Arguments Details Note Author(s) See Also

View source: R/addTxn.R

Description

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.

Usage

1
2
3
4
5
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)

Arguments

Portfolio

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

Symbol

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

TxnDate

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

TxnQty

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

TxnPrice

Price at which the transaction was done

TxnFees

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

allowRebates

whether to allow positive (rebate) TxnFees, default FALSE

ConMult

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

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.

eps

value to add to force unique indices

TxnData

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

...

Any other passthrough parameters

Details

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.

Note

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

Author(s)

Peter Carl, Brian G. Peterson

See Also

addTxns, pennyPerShare, initPortf


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