Description Usage Arguments Details Note Author(s) See Also
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.
1 2 3 4 5 |
Portfolio |
A portfolio name that points to a portfolio object structured with |
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 |
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
addTxns
, pennyPerShare
, initPortf
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.