rblotter

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...


Solution

  • 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).

    Look at your symbols object:

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

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