[R-SIG-Finance] bug in updatePortf
kidA
jbherman at gmail.com
Sun Feb 5 15:19:00 CET 2012
summary: *think* i've found a bug in blotter's updatePortf which only arises
in the context of transactions priced from ticks that are not in the Symbols
pricing (OHLC daily for instance)
As you can see below, the posPL tables are correct. They list the
transactions and the Close prices and provide PL measurement for periods
between days UNLESS there is a transaction in which case the PL is broken up
into intraday segments.
THE PROBLEM is in calculating the p$summary table the following call:
table = .getBySymbol(Portfolio = Portfolio, Attribute = "Pos.Value", Dates
= Dates, Symbols = Symbols)
returns a table defined by:
for (symbol in symbols) {
tmp_col =
Portfolio$symbols[[symbol]][[namePosPL]][Dates,Attribute,drop=FALSE]
if(is.null(table)) table = tmp_col
else table = merge(table, tmp_col)
}
which uses whatever "Dates" you called updatePortf with or if nothing is
specified grabs price and transaction time stamps from the first symbol's
posPL list (which is an admitted bug):
if(is.null(Dates)) Dates <- time(Portfolio$symbols[[1]]$posPL) #not quite
right, only using first symbol...
So the only way it can work is with a "Dates" argument that includes every
transaction time in addition to whatever regular bar (5min, daily) you'd
like. If any transaction times are not included in the Dates argument to
updatePortf the intraday piece of PnL marked in symbol$posPL will not be
included in portfolio$summary (as seen below: specifically compare the rows
in p$symbols$`AAPL-US`$posPL with a time component in date index 2008-09-30
15:03:12.137 or 2008-10-01 16:00:00.000 to the rows in p$summary. The
p$summary table has a Net.Trading.PL entry corresponding to every line in
A-US and AAPL-US $posPL tables EXCEPT these two lines. *In summary:
-15558.27 and -10342.47 of Net.Trading.PL don't make it into the summary
calculations because they don't join to the time index as based upon the
price and transaction times of the arbitrarily first symbols's posPL table*
Also, given a hi-freq portfolio with many trades in many securities marking
the entire portfolio at every transaction time (which the above implies)
could become quite burdensome. It's one thing to mark any given symbol on
all it's transactions. But I would suggest it's more sensible to then
aggregate the symbol PnL to a regularized time signature defined as a
portfolio attribute.
I'm new to R and blotter so please let me know if I misunderstood something.
thoughts?
best, J
reproducible example:
initDate = '1950-01-01'
startDate = '2008-01-09'
endDate = '2010-10-28'
symbols<-c("A-US", "AAPL-US") # note: i'm loading proprietary Closing price
data from RData files
initPortf("p", symbols=symbols, initDate=initDate, currency="USD")
> extractTxns("p")
[1] "addTxn(Portfolio ='p', Symbol ='A-US', TxnDate = '2008-09-30
11:27:38.586', TxnQty =34400, TxnPrice =29.05, TxnFees =-1498.98, ConMult
=1)"
[2] "addTxn(Portfolio ='p', Symbol ='A-US', TxnDate = '2008-10-06 16:00:00',
TxnQty =-34400, TxnPrice =26.7802, TxnFees =-1381.858, ConMult =1)"
[3] "addTxn(Portfolio ='p', Symbol ='AAPL-US', TxnDate = '2008-09-30
15:03:12.137', TxnQty =-8900, TxnPrice =112.08, TxnFees =-1496.268, ConMult
=1)"
[4] "addTxn(Portfolio ='p', Symbol ='AAPL-US', TxnDate = '2008-10-01
16:00:00', TxnQty =8900, TxnPrice =110.1169, TxnFees =-1470.061, ConMult
=1)"
updatePortf(Portfolio="p")
p <- getPortfolio("p") # make a local copy of the portfolio object
p$summary['2008-09-28/2008-10-8']
p$symbols$`A-US`$txn
p$symbols$`AAPL-US`$txn
p$symbols$`A-US`$posPL['2008-09-28/2008-10-8']
p$symbols$`AAPL-US`$posPL['2008-09-28/2008-10-8']
> updatePortf(Portfolio="p")
[1] "p"
>
> p <- getPortfolio("p") # make a local copy of the portfolio object
>
> p$summary['2008-09-28/2008-10-8']
Long.Value Short.Value Net.Value Gross.Value
Period.Realized.PL Period.Unrealized.PL Gross.Trading.PL Txn.Fees
Net.Trading.PL
2008-09-29 00:00:00.000 0 0 0 0
0.00 0 0.00 0.000 0.000
2008-09-30 00:00:00.000 0 0 0 0
0.00 0 0.00 0.000 0.000
2008-09-30 11:27:38.586 1020304 0 1020304 1020304
0.00 20984 20984.00 -1498.980 19485.020
2008-10-01 00:00:00.000 1020648 -971168 49480 1991816
0.00 40750 40750.00 0.000 40750.000
2008-10-02 00:00:00.000 968016 0 968016 968016
0.00 -52632 -52632.00 0.000 -52632.000
2008-10-03 00:00:00.000 972832 0 972832 972832
0.00 4816 4816.00 0.000 4816.000
2008-10-06 00:00:00.000 928456 0 928456 928456
0.00 -44376 -44376.00 0.000 -44376.000
2008-10-06 16:00:00.000 0 0 0 0
-78081.12 70864 -7217.12 -1381.858 -8598.978
2008-10-07 00:00:00.000 0 0 0 0
0.00 0 0.00 0.000 0.000
2008-10-08 00:00:00.000 0 0 0 0
0.00 0 0.00 0.000 0.000
>
> p$symbols$`A-US`$txn
Txn.Qty Txn.Price Txn.Value Txn.Avg.Cost Pos.Qty
Pos.Avg.Cost Gross.Txn.Realized.PL Txn.Fees Net.Txn.Realized.PL Con.Mult
1950-01-01 00:00:00.000 0 0.0000 0.0 0.0000 0
0.00 0.00 0.000 0.00 0
2008-09-30 11:27:38.586 34400 29.0500 999320.0 29.0500 34400
29.05 0.00 -1498.980 -1498.98 1
2008-10-06 16:00:00.000 -34400 26.7802 -921238.9 26.7802 0
0.00 -78081.12 -1381.858 -79462.98 1
> p$symbols$`AAPL-US`$txn
Txn.Qty Txn.Price Txn.Value Txn.Avg.Cost Pos.Qty
Pos.Avg.Cost Gross.Txn.Realized.PL Txn.Fees Net.Txn.Realized.PL Con.Mult
1950-01-01 00:00:00.000 0 0.0000 0.0 0.0000 0
0.00 0.00 0.000 0.000 0
2008-09-30 15:03:12.137 -8900 112.0800 -997512.0 112.0800 -8900
112.08 0.00 -1496.268 -1496.268 1
2008-10-01 16:00:00.000 8900 110.1169 980040.4 110.1169 0
0.00 17471.59 -1470.061 16001.529 1
>
> p$symbols$`A-US`$posPL['2008-09-28/2008-10-8']
Pos.Qty Con.Mult Ccy.Mult Pos.Value Pos.Avg.Cost
Txn.Value Period.Realized.PL Period.Unrealized.PL Gross.Trading.PL Txn.Fees
Net.Trading.PL
2008-09-29 00:00:00.000 0 1 1 0 0.00
0.0 0.00 0 0.00 0.000
0.000
2008-09-30 00:00:00.000 0 1 1 0 0.00
0.0 0.00 0 0.00 0.000
0.000
2008-09-30 11:27:38.586 34400 1 1 1020304 29.05
999320.0 0.00 20984 20984.00 -1498.980
19485.020
2008-10-01 00:00:00.000 34400 1 1 1020648 0.00
0.0 0.00 344 344.00 0.000
344.000
2008-10-02 00:00:00.000 34400 1 1 968016 0.00
0.0 0.00 -52632 -52632.00 0.000
-52632.000
2008-10-03 00:00:00.000 34400 1 1 972832 0.00
0.0 0.00 4816 4816.00 0.000
4816.000
2008-10-06 00:00:00.000 34400 1 1 928456 0.00
0.0 0.00 -44376 -44376.00 0.000
-44376.000
2008-10-06 16:00:00.000 0 1 1 0 0.00
-921238.9 -78081.12 70864 -7217.12 -1381.858
-8598.978
2008-10-07 00:00:00.000 0 1 1 0 0.00
0.0 0.00 0 0.00 0.000
0.000
2008-10-08 00:00:00.000 0 1 1 0 0.00
0.0 0.00 0 0.00 0.000
0.000
> p$symbols$`AAPL-US`$posPL['2008-09-28/2008-10-8']
Pos.Qty Con.Mult Ccy.Mult Pos.Value Pos.Avg.Cost
Txn.Value Period.Realized.PL Period.Unrealized.PL Gross.Trading.PL Txn.Fees
Net.Trading.PL
2008-09-29 00:00:00.000 0 1 1 0 0.00
0.0 0.00 0 0.00 0.000
0.00
2008-09-30 00:00:00.000 0 1 1 0 0.00
0.0 0.00 0 0.00 0.000
0.00
2008-09-30 15:03:12.137 -8900 1 1 -1011574 112.08
-997512.0 0.00 -14062 -14062.00 -1496.268
-15558.27
2008-10-01 00:00:00.000 -8900 1 1 -971168 0.00
0.0 0.00 40406 40406.00 0.000
40406.00
2008-10-01 16:00:00.000 0 1 1 0 0.00
980040.4 17471.59 -26344 -8872.41 -1470.061
-10342.47
2008-10-02 00:00:00.000 0 1 1 0 0.00
0.0 0.00 0 0.00 0.000
0.00
2008-10-03 00:00:00.000 0 1 1 0 0.00
0.0 0.00 0 0.00 0.000
0.00
2008-10-06 00:00:00.000 0 1 1 0 0.00
0.0 0.00 0 0.00 0.000
0.00
2008-10-07 00:00:00.000 0 1 1 0 0.00
0.0 0.00 0 0.00 0.000
0.00
2008-10-08 00:00:00.000 0 1 1 0 0.00
0.0 0.00 0 0.00 0.000
0.00
--
View this message in context: http://r.789695.n4.nabble.com/bug-in-updatePortf-tp4359010p4359010.html
Sent from the Rmetrics mailing list archive at Nabble.com.
More information about the R-SIG-Finance
mailing list