[R-SIG-Finance] Accounting in blotter query
Brian G. Peterson
brian at braverock.com
Fri Oct 5 12:01:31 CEST 2012
On 10/04/2012 11:38 PM, Worik Stanton wrote:
> On 05/10/12 13:50, Brian G. Peterson wrote:
>> On 10/04/2012 07:39 PM, Worik Stanton wrote:
>>> 2012-10-04 00:00:00 1 210.39 0.00
>>> 2012-10-04 00:00:00 -3 -631.17 127.26
>>> So I would expect the equity in the account to be the initial equity
>>> plus (127.26 - 9.67) == 1,000,000 + 117.59 == 1,000,117.59
>>> But getEndEq(account, end(IBM)) == 999813.3
>>> what am I missing here?
>> you have non-unique timestamps. The xts documentation, and the
>> blotter documentation, make it clear that non-unique timestamps will
>> cause undefined and incorrect behavior, that's why the
>> make.unique.index command exists.
> Thank you. My mistake and I fixed that.
> Now I have the following list of transactions:
> Txn.Qty Txn.Value Net.Txn.Realized.PL
> 2007-01-02 13:00:00 0 0.00 0.00
> 2007-01-03 00:00:00 1 97.27 0.00
> 2008-02-28 00:00:00 1 115.24 0.00
> 2009-04-23 00:00:00 -2 -202.84 -9.67
> 2010-06-17 00:00:00 1 130.98 0.00
> 2011-08-10 00:00:00 1 162.54 0.00
> 2012-10-04 00:00:00 -2 -420.78 127.26
>> Ret <- (getEndEq(Account=account, Date=end(IBM))-initEq)/initEq
>> cat('Return: ', Ret,'\n')
> Return: -0.00018095
>> getEndEq(account, end(IBM))
>  999819.1
>> initEq + sum(XX[,6])
>  1000118
>  117.59
> The trading made a 117.59 profit, but the equity at the end is less than
> at the start, as reported by getEndEq
> I am still missing something.
updatePortf(Portfolio = portfolio, Dates = CurrentDate)
updateAcct(account, Dates = CurrentDate)
updateEndEq(account, Dates = CurrentDate)
should be outside your loop. You never call them after making the last
closing trade, so the ending equity that you are requesting shows
(correctly) a loss as of the date of the last time you updated it.
updatePortf(Portfolio = portfolio)
after your loop will return the correct ending equity.
These update* functions don't need to be called every time, or even
every transaction. You're doing more processing than you need to do.
That is true in a general sense as well. You need to loop over
*Transactions*, because these are path dependent, and need to be entered
in order to get meaningful P&L. Your script loops over every index of
IBM, and will be very slow.
We make use of vectorization and path-independent processes wherever
possible. Using loops when you don't need to only leads to bad performance.
More information about the R-SIG-Finance