Error when running initPortf in Blotter package for R

42 Views Asked by At

I am having trouble initializing portfolios with the Blotter Package in R.

I am cycling through a list of variables that I want to initialize, and when I initialize with a single symbol, it works fine.

But when I try to do it with a list of symbols, it throws up the error:

Error in portfolio$symbols[[instrument]] <- new.env(hash = TRUE) : wrong arguments for environment subassignment

Here is my code:

for (i in strategies) {

temp_symbol_list <- as.list(c(portfolio_txns %>%
  filter(strategy == i) %>%
  select(symbol)))

# temp_symbol_list <- as.list(temp_symbol_list)

print (i)
print (temp_symbol_list)

initPortf(i, temp_symbol_list,initDate = earliest_date, currency = "CAD")

}

When there is only one symbol in the intialize function, it works fine. But if there are two symbols, I get the error.

I have tried many different ways of creating the symbol list, but I think it is expecting something that I am not able to create.

For example, if I do the following:

initPortf("strategy",c("symbol 1","symbol 2"), initDate = earliest_date, 
    currency = "CAD") 

it works fine.

Somehow, my list is the problem.

1

There are 1 best solutions below

1
Kevin Muir On

I figured it out.

I was created the variable to send to the initPortf as a "list". I changed it to a string and it worked.

Specifically, this is the new line

temp_symbol_list <- toString(temp_symbol_list)

I was confused as the documentation for the Blotter package said it was expecting a list, but I probably don't understand the different types of data storage properly.

I'm leaving this up in case someone else has the same problem.

Big thanks to the folks who wrote these packages btw.