You don't need all those loops as both stocks
and getSymbols
are both vectorized functions.
You also don't need get
command. Also you don't really need to call updatePortf
, updateAcct
and updateEndEq
for every time stamp. You can call them with portfolio name and account name and it will automatically mark to market the accounts and portfolios for all the time stamps where you have price data available.
library(blotter)
MyPortfolios = c("Port1", "Port2")
MySymbols = list()
MySymbols[[1]] = c("BND", "DBC", "DXJ")
MySymbols[[2]] = c("ALD", "BND", "DBC", "ECON")
currency("USD")
## [1] "USD"
Date_Start = "2013-01-01"
Date_End = format(Sys.time(), "%Y-%m-%d")
Sys.setenv(TZ = "UTC")
stock(MySymbols[[1]], currency = "USD", multiplier = 1)
## [1] "BND" "DBC" "DXJ"
stock(MySymbols[[2]], currency = "USD", multiplier = 1)
## [1] "ALD" "BND" "DBC" "ECON"
getSymbols(MySymbols[[1]], from = Date_Start, to = Date_End, adjust = T)
## [1] "BND" "DBC" "DXJ"
getSymbols(MySymbols[[2]], from = Date_Start, to = Date_End, adjust = T)
## [1] "ALD" "BND" "DBC" "ECON"
suppressWarnings(rm("account.LongTerm", pos = .blotter))
suppressWarnings(rm("portfolio.Port1", pos = .blotter))
suppressWarnings(rm("portfolio.Port2", pos = .blotter))
initPortf(MyPortfolios[1], as.list(MySymbols[[1]]), initDate = "2013-06-01")
## [1] "Port1"
initPortf(MyPortfolios[2], as.list(MySymbols[[2]]), initDate = "2013-06-01")
## [1] "Port2"
initAcct("LongTerm", MyPortfolios, initDate = "2013-06-01", initEq = 6e+05)
## [1] "LongTerm"
addTxn("Port1", Symbol = "BND", TxnDate = "2013-06-10", TxnQty = 733, TxnPrice = 81.83, TxnFees = 0)
## [1] "2013-06-10 00:00:00 BND 733 @ 81.83"
addTxn("Port1", Symbol = "DBC", TxnDate = "2013-06-10", TxnQty = 343, TxnPrice = 26.22, TxnFees = 0)
## [1] "2013-06-10 00:00:00 DBC 343 @ 26.22"
addTxn("Port1", Symbol = "DXJ", TxnDate = "2013-06-10", TxnQty = 259, TxnPrice = 46.3, TxnFees = 0)
## [1] "2013-06-10 00:00:00 DXJ 259 @ 46.3"
addTxn("Port2", Symbol = "ALD", TxnDate = "2013-06-11", TxnQty = 60, TxnPrice = 49.92, TxnFees = 0)
## [1] "2013-06-11 00:00:00 ALD 60 @ 49.92"
addTxn("Port2", Symbol = "BND", TxnDate = "2013-06-11", TxnQty = 159, TxnPrice = 81.83, TxnFees = 0)
## [1] "2013-06-11 00:00:00 BND 159 @ 81.83"
addTxn("Port2", Symbol = "ECON", TxnDate = "2013-06-11", TxnQty = 58, TxnPrice = 26.67, TxnFees = 0)
## [1] "2013-06-11 00:00:00 ECON 58 @ 26.67"
updatePortf(MyPortfolios[1])
## [1] "Port1"
updatePortf(MyPortfolios[2])
## [1] "Port2"
updateAcct("LongTerm")
## [1] "LongTerm"
updateEndEq("LongTerm")
## [1] "LongTerm"
Btw. The specific error you were facing was that you need to assign xts object to TempSym
using get
. But you really don't need to use this loop as shown in my code above.
for (k in 1:length(MyPortfolios)){
TempList = MySymbols[[k]]
for (j in 1:length(TempList)){
TempSym = get(TempList[[j]]) # <---------------------
print(paste(TempSym))
for (i in 1:nrow(TempSym)){
CurrentDate <- time(TempSym)[i]
updatePortf(MyPortfolios[k], Dates = CurrentDate)
updateAcct( MyPortfolios[k], Dates = CurrentDate)
updateEndEq(MyPortfolios[k], Dates = CurrentDate)
}
}
}