[R-SIG-Finance] Accounting in blotter query

Worik Stanton worik.stanton at gmail.com
Fri Oct 5 06:38:55 CEST 2012


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))
[1] 999819.1
> initEq + sum(XX[,6])
[1] 1000118
> sum(XX[,6])
[1] 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.

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

  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
  }else{

    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)

  }
}
## 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