我正在尝试完成 Guy Yonlin 的 quantstrat & blogger 的优秀示例代码版本,但使其适用于一组投资组合。不幸的是,我一直在尝试读取符号列表并让 R 访问下载的实际 xts 数据。
在下面的代码中,我正确地找到"BND"
了第一个符号,但我不知道如何使TempSym
符号成为实际的 xts 对象,以便它实际上有行。
我在这里做错了什么?我看到的实际失败是这样的:
[1] "BND"
Error in 1:nrow(TempSym) : argument of length 0
请注意,此时注释掉的所有语句都没有被调试过。它们基于我认为我从 Guy 的示例代码中要去的地方。
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)