[R-SIG-Finance] Accounting in blotter query

Worik Stanton worik.stanton at gmail.com
Fri Oct 5 02:39:06 CEST 2012


I am not sure I quite understand how blotter keeps its running totals.

Using the attached code I simulate some trading in IBM.  (The trades are
basically random)

I have three transactions with quantity==1 at the closing price then a
transaction with quantity ==  -3

I do this twice and get this output (via getTxns(..)):

                    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       1    101.42                0.00
2009-04-23 00:00:00      -3   -304.26               -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       1    210.39                0.00
2012-10-04 00:00:00      -3   -631.17              127.26
>

The signs are not what I expect.  For Txn.Value is -1 x the flow in and
out of the portfolio.  OK. 

Net.Txn.Realized.PL seems correct to me, recording the changes in equity
when the position of the portfolio goes to zero.

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?

cheers
Worik

-- 
it does not matter                      I think that I shall never see
how much I dig and dig                    A billboard lovely as a tree
this hole just                      Indeed, unless the billboards fall
keeps getting deeper                      I'll never see a tree at all

-------------- next part --------------

## required libraries
require(quantmod)
require(TTR)
##install.packages(c("blotter_0.8.12.tar.gz"), repos=NULL)
require(blotter)

symbols <- c("IBM")
rm("portfolio.testy", pos=.blotter)
rm("account.testy", pos=.blotter)

## Portfolio Parameters
getSymbols("IBM")
## Make this the same every day
IBM <- IBM["2007-01-03::2012-10-04",]


initDate <- start(IBM)-1
initEq <- 1000000
## Set up a portfolio object and an account object
portfolio = "testy" 
assign("portfolio", portfolio, pos=1)
currency("USD")

stock("IBM", currency="USD",multiplier=1)


initPortf(name=portfolio, c("IBM"), initDate=initDate, currency="USD")
account = "testy"
assign("account", account, pos=1)
initAcct(name=account,portfolios="testy", initDate=initDate, initEq=initEq)

  
count <- 0
limit <- 3
## Create trades
idx <- trunc(seq(1, nrow(IBM), length.out = limit*2))
for( i in idx) {

  ClosePrice <- Cl(IBM[i,])
  CurrentDate <- index(IBM[i,])
  TxnQty <- 1
  count <- count+1
  addTxn(Portfolio=portfolio, Symbol="IBM", TxnDate=CurrentDate,
         TxnPrice=ClosePrice, TxnQty = TxnQty,
         TxnFees=0, verbose=TRUE)
  updatePortf(Portfolio = portfolio, Dates = CurrentDate)
  updateAcct(account, Dates = CurrentDate)
  updateEndEq(account, Dates = CurrentDate)


  if(count==limit) {
    ## Close trades
    ClosePrice <- Cl(IBM[i,])
    CurrentDate <- index(IBM[i,])
    Posn <- getPosQty(Portfolio=portfolio, Symbol="IBM", Date=CurrentDate)
    addTxn(Portfolio=portfolio, Symbol=symbol, TxnDate=CurrentDate,
           TxnPrice=ClosePrice, TxnQty = -Posn,
           TxnFees=0, verbose=TRUE)
    count <- 0
  }
}
## Final values

XX <-  getTxns(portfolio, "IBM")
XX[, c(1, 4, 6)]
Ret <- (getEndEq(Account=account, Date=end(IBM))-initEq)/initEq
cat('Return: ', Ret,'\n')
getEndEq(account, end(IBM))
initEq + sum(XX[,6])
sum(XX[,6])


More information about the R-SIG-Finance mailing list