1

我正在尝试完成 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)
4

1 回答 1

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"

顺便提一句。您面临的具体错误是您需要将 xts 对象分配给TempSymusing 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)
    }
  }
}
于 2013-11-15T02:46:01.677 回答