[R-SIG-Finance] Period.Unrealized.PL always sums to 0 after a transaction, why? - Blotter/ Quantstrat

Brian G. Peterson brian at braverock.com
Tue Dec 30 11:27:18 CET 2014


On 12/30/2014 02:50 AM, Derek Wong wrote:
> I have been diving into the workings of blotter just to understand it
> better. I have been using the bbands.R demo that is in the quantstrat
> project. It is actually a number that will sum with all of the
> previous Period.Unrealized.PL to make it sum to 0. Shouldn't the
> Period.Unrealized.PL for a bar with a transaction to exit be 0,
> assuming you exit at the close price?  or in the bbands.R example the
> previous day 2007-03-20 close price is 94.50 the next day is 95.36.
> Shouldn't 86 be the result not -54? I am not saying the result is
> incorrect, I am just trying to figure out the logic behind the
> calculations.
>
> To get the portfolio PL sheet run bbands.R from r-forge then
>
> port <- getPortfolio(portfolio.st)
> View(port$symbols$IBM$posPL)
> sum(port$symbols$IBM$posPL$Period.Unrealized.PL)

Unrealized P&L is exactly what it says it is.  Once you realize that P&L 
(on a transaction), then the unrealized P&L flat to flat has to sum to 
zero. if it did not, then you'd double-count unrealized and realized P&L.

In the last period, the period with a transaction, you need to back out 
any residual unrealized P&L and move it to the realized column.

Put another way, portfolio value is the sum of realized P&L from trades, 
position average cost for open positions, and any any unrealized P&L 
from open position.

So, back to your example from the bbands demo.

on 2007-03-20 your cumulative Net.Trading.PL and Period.Unrealized.PL 
was 54, all of it from your unrealized gains.

on 2007-03-21 you close the position for 140 realized gain

The proceeds from that transaction, for period 2007-03-21, need to back 
out the 54 unrealized (since you've already accumulated it), for a gross 
profit in column Gross.Trading.PL (from the transaction realized P&L) on 
this period of 86.  (there's the 86 you're looking for in your question).

Part of this is a result of double entry accounting, where debits and 
credits need to add up.  Another part of this is the nature of portfolio 
accounting, where period gains and losses are attributed to your account 
value each day.  all the money you gained or lost each day was reflected 
in the cumulative totals of your account value, so that has to still 
make sense on the day you close the transaction, and you do that by 
backing out the accumulated unrealized gain when you realize it.

Regards,

Brian

-- 
Brian G. Peterson
http://braverock.com/brian/
Ph: 773-459-4973
IM: bgpbraverock



More information about the R-SIG-Finance mailing list