所有变量都没有正确定义。?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")