braverock / blotter

blotter provides transaction infrastructure for defining transactions, portfolios and accounts for trading systems and simulation. Provides portfolio support for multi-asset class and multi-currency portfolios. Actively maintained and developed.
114 stars 50 forks source link

[R-Forge #1115] blotter:::addDiv uses wrong Quantity in calculation? #27

Open joshuaulrich opened 8 years ago

joshuaulrich commented 8 years ago

Submitted by: Mark Breman Assigned to: Garrett See R-Forge link

Hi,

I think the addDiv implementation is not correct in all cases as it has only one date param and no quantity param.

Here is a scenario where it's producing the wrong result:

start script

try(rm('account.IB','portfolio.faber',pos=.blotter),silent=TRUE) try(rm('PLW','USD',pos=.instrument),silent=TRUE)

initDate='2010-01-01' currency('USD') stock(primary_id='PLW', currency='USD', multiplier=1) getSymbols('PLW', from=initDate)

portfolio='faber' account='IB'

initPortf(name=portfolio, symbols='PLW', initDate=initDate) initAcct(name=account, portfolios=portfolio, initDate=initDate, initEq=50000)

verbose=TRUE

addTxn(Portfolio=portfolio, Symbol='PLW', TxnDate='2010-02-04', TxnQty=12, TxnPrice=27.12, TxnFees=-1.0) addTxn(Portfolio=portfolio, Symbol='PLW', TxnDate='2010-02-24', TxnQty=24, TxnPrice=27.12, TxnFees=0.0)

actually the cumdate='2010-02-11'

blotter:::addDiv(Portfolio=portfolio, Symbol='PLW', TxnDate='2010-02-26', DivPerShare=.0969) addTxn(Portfolio=portfolio, Symbol='PLW', TxnDate='2010-05-20', TxnQty=-36, TxnPrice=28.14, TxnFees=-1.0)

updatePortf(Portfolio=portfolio, Dates='2010::') updateAcct(name=account, Dates='2010::') updateEndEq(Account=account)

getTxns(Portfolio=portfolio, Symbol='PLW', Date='2010::')

end script

In the Net.Txn.Realized.PL results the #shares used is 36 (0.0969*36 = 3.4884), while the #shares should have been 12 because that was the amount of shares I held at the date before the ex-dividend date (I call this the cum-dividend date).

For cash dividends I think there should be two date params to addDiv: cumdate and paydate (or altenativly a #shares and a paydate).

Kind regards,

-Mark-

Followups:

Date: 2011-08-19 14:42 Sender: Brian Peterson Garrett,Can you take a look at this? I know you've poked around at addDiv - Brian

Date: 2010-10-11 13:28 Sender: Mark Breman You could also consider generating two transactions if someone adds a cash dividend:1) transaction for determining position held (period preceding exDivDate)2) payout of dividend transactionThe time in between those two transactions you have a sort-of 'unrealized dividend'.This way you can do with just one date column in the slot, and things can be reconstructed later (bit more difficult though). -Mark-

Date: 2010-10-11 13:14 Sender: Mark Breman I think to use the position at the end of the exDivDate is not correct. Better would be to use the position held preceding the exDivDate, see http://en.wikipedia.org/wiki/Ex-dividend_date'We might need to add another column in addition to the Txn.Typecolumn to hold extra info to be able to reconstruct things later.'This is another reason why I declared a different slot for corporate actions in the pnl-package.The column headers for transactions (typically buy/sell transactions) just don't match with what's required for corporate actions, although they do have some headers in common. It's also confusing if you look at the transactions slot and the buy/sell transactions are mixed with dividends and possibly more corporate actions in the future.Kind regards,-Mark-

Date: 2010-10-11 12:34 Sender: Brian Peterson My inclination for this is to have exDivDate and payDate. We'll use the position at the end of the exDivDate to calculate number of shares to pay the dividend on.We might need to add another column in addition to the Txn.Type column to hold extra info to be able to reconstruct things later.Thoughts? - Brian