我正在尝试使用Guy Yonlin的一个优秀的示例代码for quantstrat&吸墨纸,但它适用于一组投资组合。不幸的是,我试图读取一个符号列表并让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)
答案 0 :(得分:2)
您不需要所有这些循环,因为stocks
和getSymbols
都是矢量化函数。
您也不需要get
命令。此外,您无需为每个时间戳调用updatePortf
,updateAcct
和updateEndEq
。您可以使用投资组合名称和帐户名称来呼叫它们,并且它会自动标记为您提供价格数据的所有时间戳的市场帐户和投资组合。
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"
顺便说一下。您遇到的具体错误是您需要使用TempSym
将xts对象分配给get
。但是你真的不需要使用这个循环,如上面的代码所示。
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)
}
}
}