简体   繁体   English

R错误:initPortf子分配问题

[英]R error: initPortf subassignment issue

I keep getting the following error when running this code, anyone know how to fix it?: 运行此代码时,我不断收到以下错误,有人知道如何解决吗?:

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

All variables have been correctly defined I believe... 我相信所有变量都已正确定义...

All variables are not correctly defined. 没有正确定义所有变量。 ?initPortf says symbols should be, "A list of instrument identifiers for those instruments contained in the portfolio" (emphasis added). ?initPortf表示symbols应为“投资组合中包含的那些工具的工具标识符列表”(添加了重点)。

Look at your symbols object: 查看您的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"

You need to do this: 您需要这样做:

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")

Which would have been clear, if you had looked at some of the demos that come with blotter. 如果您查看了吸墨纸附带的一些演示,那将会很清楚。 For example, "amzn_test": 例如,“ 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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM