R错误:initPortf子分配问题

时间:2016-01-18 22:22:51

标签: r blotter

运行此代码时出现以下错误,有人知道如何修复它吗?:

library(blotter)
getSymbols('BP;AAPL')
symbols <- c(BP, AAPL)
initDate = "1990-01-01"
portfolio.st <- "mas"
initPortf(portfolio.st, symbols = symbols, initDate = initDate, currency = "USD")
Error in initPortf(portfolio.st, symbols = symbols, initDate = initDate,  : 
wrong args for environment subassignment

我相信所有变量都已正确定义......

1 个答案:

答案 0 :(得分:1)

未正确定义所有变量。 ?initPortf表示symbols应该是“投资组合中包含的那些工具的工具标识符列表”(重点补充)。

查看您的symbols对象:

> str(symbols)
An ‘xts’ object on 2007-01-03/2016-01-15 containing:
  Data: num [1:4552, 1:6] 67.3 86.3 65.7 84.1 64.9 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:6] "BP.Open" "BP.High" "BP.Low" "BP.Close" ...
  Indexed by objects of class: [Date] TZ: UTC
  xts Attributes:  
List of 2
 $ src    : chr "yahoo"
 $ updated: POSIXct[1:1], format: "2016-01-18 16:25:34"

你需要这样做:

library(blotter)
symbols <- c("BP", "AAPL")
getSymbols(symbols)
#initDate = "1990-01-01"  # You do not need to set initDate
portfolio.st <- "mas"
initPortf(portfolio.st, symbols = symbols, currency = "USD")

如果你看过一些带有吸墨纸的演示,那本来就很清楚。例如,“amzn_test”:

require(blotter)
# Remove portfolio and account data if run previously
try(rm("portfolio.amzn_port","account.amzn_acct",pos=.blotter), silent = TRUE)
# load the example data
data("amzn")
currency("USD")
stock("amzn",currency="USD",multiplier=1)
# Initialize the Portfolio
initPortf("amzn_port",symbols="amzn",initDate="2010-01-14")
initAcct("amzn_acct",portfolios="amzn_port",initDate="2010-01-14", initEq=10000)
# look at the transactions data
amzn.trades
# Add the transactions to the portfolio
blotter::addTxns("amzn_port","amzn",TxnData=amzn.trades,verbose=TRUE)
# update the portfolio stats
updatePortf("amzn_port",Dates="2010-01-14")
# update the account P&L
updateAcct("amzn_acct",Dates="2010-01-14")
# and look at it
chart.Posn("amzn_port","amzn",Dates="2010-01-14")