[R-SIG-Finance] Duplicated indexes in blotter
Mark Knecht
markknecht at gmail.com
Sat Sep 20 00:13:51 CEST 2014
In the attached code I have 3 transactions on the same date for the
symbol IVOG. When I go to roll up position values this is leading to
duplicate indexes in my xts objects and NA's in other columns, such as
in tempPos.Value:
2014-05-05 55182 45420.0
2014-05-06 54702 45135.0
2014-05-07 63945 68212.5
2014-05-07 73080 NA
2014-05-07 82215 NA
2014-05-08 81747 68025.0
2014-05-09 81891 68040.0
2014-05-12 83448 68985.0
Am I doing something improper in the way I'm entering the transactions
or is this a bug? It's pretty typical that I'll put in a limit order
and it will get filled in pieces with each portion having a different
price.
I could of course figure out by hand what the average price was and
then have a single transaction but I'd like to have what's saved in
the account match what actually happened.
In general I've been fixing these as they pop up using code like:
tempNet.Txn.Value[is.na(tempNet.Txn.Value)] = 0
but I'm finally tired enough of doing that I thought I'd ask if I'm
doing something wrong.
Additionally, I thought one of the things xts was supposed to ensure
against was duplicate indexes so maybe this is me using some older
version? I'm mostly CRAN oriented so if I need to load something else
please let me know.
Thanks,
Mark
library(blotter)
library(quantmod)
MyPortfolios = c("Test1")
MySymbols = list()
MySymbols[[1]]= c("IVOG", "IVOV")
currency("USD")
DataStart = "2014-03-01"
DataEnd = format(Sys.time(), "%Y-%m-%d")
Sys.setenv(TZ = "UTC")
getSymbols(MySymbols[[1]], from=DataStart, to=DataEnd, src="yahoo")
stock(MySymbols[[1]], currency = "USD", multiplier = 1)
suppressWarnings(rm("account.Test1", pos = .blotter))
suppressWarnings(rm("portfolio.Test1", pos = .blotter))
initPortf(MyPortfolios[1], as.list(MySymbols[[1]]), initDate = "2014-01-01")
initAcct("Test1", MyPortfolios, initDate = "2013-06-01", initEq = 1e+06)
addTxn("Test1", Symbol = "IVOG", TxnDate = "2014-03-25", TxnQty =
600, TxnPrice = 93.71, TxnFees = 0, verbose = FALSE)
addTxn("Test1", Symbol = "IVOG", TxnDate = "2014-05-07", TxnQty =
100, TxnPrice = 91.08, TxnFees = 0, verbose = FALSE)
addTxn("Test1", Symbol = "IVOG", TxnDate = "2014-05-07", TxnQty =
100, TxnPrice = 91.10, TxnFees = 0, verbose = FALSE)
addTxn("Test1", Symbol = "IVOG", TxnDate = "2014-05-07", TxnQty =
100, TxnPrice = 91.09, TxnFees = 0, verbose = FALSE)
addTxn("Test1", Symbol = "IVOG", TxnDate = "2014-08-11", TxnQty =
300, TxnPrice = 93.93, TxnFees = 0, verbose = FALSE)
addTxn("Test1", Symbol = "IVOV", TxnDate = "2014-03-25", TxnQty =
500, TxnPrice = 90.34, TxnFees = 0, verbose = FALSE)
addTxn("Test1", Symbol = "IVOV", TxnDate = "2014-05-07", TxnQty =
250, TxnPrice = 90.63, TxnFees = 0, verbose = FALSE)
addTxn("Test1", Symbol = "IVOV", TxnDate = "2014-08-11", TxnQty =
250, TxnPrice = 93.27, TxnFees = 0, verbose = FALSE)
updatePortf(MyPortfolios[1])
updateAcct("Test1")
updateEndEq("Test1")
# Loops appear to be required due to symbol name
# from list being in the middle of the path
# Study later
for (j in 1:length(MyPortfolios)){
ThisPort = getPortfolio(MyPortfolios[j])
for (i in 1:length(MySymbols[[j]])){
ThisSymbol = MySymbols[[j]][i]
tempVal = ThisPort$symbols[[ThisSymbol]]
if ((i==1) & (j==1)){
tempPos.Value = as.xts(tempVal$posPL$Pos.Value)
tempNet.Trading.PL = as.xts(tempVal$posPL$Net.Trading.PL)
tempNet.Txn.Value = as.xts(tempVal$posPL$Txn.Value)
}else{
tempPos.Value = cbind(tempPos.Value,
as.xts(tempVal$posPL$Pos.Value))
tempNet.Trading.PL = cbind(tempNet.Trading.PL,
as.xts(tempVal$posPL$Net.Trading.PL))
tempNet.Txn.Value = cbind(tempNet.Txn.Value,
as.xts(tempVal$posPL$Txn.Value))
}
}
}
colnames(tempPos.Value) =MySymbols[[1]]
colnames(tempNet.Trading.PL) =MySymbols[[1]]
colnames(tempNet.Txn.Value) =MySymbols[[1]]
More information about the R-SIG-Finance
mailing list