Question

I'm trying to work through a version of Guy Yonlin's excellent example code for quantstrat & blotter but make it work for a set of portfolios. Unfortunately I'm stuck trying to read a list of symbols and get R to access the actual xts data that got downloaded.

In the code that follows I correctly find "BND" which is the first symbol but I can't figure out how to make TempSym be the actual xts object for the symbol so that it actually has rows.

What am I doing wrong here? The actual failure I see is this:

[1] "BND"
Error in 1:nrow(TempSym) : argument of length 0

Note that none of the statements commented out have been debugged at this point. They are based on where I think I'm going from Guy's example code.

library(blotter)
MyPortfolios = c("Port1", "Port2")

MySymbols=list()
MySymbols[[1]]= c("BND","DBC","DXJ")

MySymbols[[2]]= c("ALD", "BND","DBC","ECON")

currency("USD")

get("USD",envir=FinancialInstrument:::.instrument)

Date_Start = "2013-01-01"
Date_End = format(Sys.time(), "%Y-%m-%d")

Sys.setenv(TZ="UTC")

TotalSymbols = 0
for (j in 1:length(MySymbols)){
  TempSym = MySymbols[[j]]
  for (i in 1:length(TempSym)){
    if (!exists(paste(TempSym[i]))){
      stock(TempSym[i], currency="USD", multiplier=1)
      get(TempSym[i],envir=FinancialInstrument:::.instrument)
      getSymbols(TempSym[i], from=Date_Start, to=Date_End, adjust=T)
      TotalSymbols = TotalSymbols + 1
    }
  }
  rm(TempSym)
}

print(paste("Total symbols downloaded: ", TotalSymbols))
rm(TotalSymbols)

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")
initPortf(MyPortfolios[2], as.list(MySymbols[[2]]), initDate="2013-06-01")

initAcct("LongTerm", MyPortfolios, initDate="2013-06-01", initEq=600000)

addTxn("Port1", Symbol="BND", TxnDate="2013-06-10", TxnQty=733, TxnPrice=81.83, TxnFees=0)
addTxn("Port1", Symbol="DBC", TxnDate="2013-06-10", TxnQty=343, TxnPrice=26.22, TxnFees=0)
addTxn("Port1", Symbol="DXJ", TxnDate="2013-06-10", TxnQty=259, TxnPrice=46.30, TxnFees=0)

addTxn("Port2", Symbol="ALD", TxnDate="2013-06-11", TxnQty=60,  TxnPrice=49.92, TxnFees=0)
addTxn("Port2", Symbol="BND", TxnDate="2013-06-11", TxnQty=159, TxnPrice=81.83, TxnFees=0)
addTxn("Port2", Symbol="ECON", TxnDate="2013-06-11", TxnQty=58, TxnPrice=26.67, TxnFees=0)

###################
# For each portfolio
# look up each symbol
# and calculate equity for each bar
###################

for (k in 1:length(MyPortfolios)){
  TempList = MySymbols[[k]]
  for (j in 1:length(TempList)){
    TempSym = 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)
    }
  }
}


# create custom theme
myTheme<-chart_theme()
myTheme$col$dn.col<-'purple'
myTheme$col$dn.border <- 'lightgray'
myTheme$col$up.col<-'orange'
myTheme$col$up.border <- 'lightgray'

chart.Posn(MyPortfolios[1], Symbol = "BND", Dates = "2013::", theme=myTheme)
chart.Posn(MyPortfolios[1], Symbol = "DBC", Dates = "2013::", theme=myTheme)
chart.Posn(MyPortfolios[1], Symbol = "DXJ", Dates = "2013::", theme=myTheme)

chart.Posn(MyPortfolios[2], Symbol = "ALD", Dates = "2013::", theme=myTheme)
chart.Posn(MyPortfolios[2], Symbol = "BND", Dates = "2013::", theme=myTheme)
chart.Posn(MyPortfolios[2], Symbol = "ECON", Dates = "2013::", theme=myTheme)
Was it helpful?

Solution

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)
    }
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top