I have some code I wrote awhile back that used 4 portfolios in blotter. I would like to modify this code to use as many portfolios as I define entries in the MySymbols list. In the code below I show first what has worked in the past which is explicitly writing out the name of the 4 portfolios. In the second part I attempt to convert this to a loop where the name is constructed on the fly. The name appears to be correct but R cannot find the portfolios and I see the warning messages:
1: In rm(Str1, pos = .blotter) : object 'Str1' not found
2: In rm(Str1, pos = .blotter) : object 'Str1' not found
3: In rm(Str1, pos = .blotter) : object 'Str1' not found
4: In rm(Str1, pos = .blotter) : object 'Str1' not found
The portfolios are not found or removed:
> ls(.blotter)
[1] "portfolio.Port1" "portfolio.Port2" "portfolio.Port3" "portfolio.Port4"
What am I doing wrong here? How do I get R to use the name I've constructed in Str1?
library(blotter)
Date_Start = "2000-01-01"
InitialEquity = 100000
currency("USD")
MyPortfolios = c("Port1", "Port2", "Port3", "Port4")
MySymbols = list()
MySymbols[[1]]= c("AAPL","GOOG")
MySymbols[[2]]= c("BAC","GS")
MySymbols[[3]]= c("CVX","XOM")
MySymbols[[4]]= c("TWTR","FB")
CashSymbol = "SHY"
ls(.blotter)
suppressWarnings(rm("account.Test1", pos = .blotter))
suppressWarnings(rm("portfolio.Port1", pos = .blotter))
suppressWarnings(rm("portfolio.Port2", pos = .blotter))
suppressWarnings(rm("portfolio.Port3", pos = .blotter))
suppressWarnings(rm("portfolio.Port4", pos = .blotter))
ls(.blotter)
initPortf(MyPortfolios[1], as.list( c(MySymbols[[1]], CashSymbol)), initDate = Date_Start)
initPortf(MyPortfolios[2], as.list( c(MySymbols[[2]], CashSymbol)), initDate = Date_Start)
initPortf(MyPortfolios[3], as.list( c(MySymbols[[3]], CashSymbol)), initDate = Date_Start)
initPortf(MyPortfolios[4], as.list( c(MySymbols[[4]], CashSymbol)), initDate = Date_Start)
initAcct("Test1", MyPortfolios, initDate = Date_Start, initEq = InitialEquity)
ls(.blotter)
# Do the same thing in a loop
NumPorts = length(MySymbols)
MyPorts=NULL
suppressWarnings(rm("account.Test1", pos = .blotter))
for (i in 1:NumPorts){
stock(MySymbols[[i]], currency = "USD", multiplier = 1)
MyPorts = c(MyPorts, paste0("Port",i))
}
ls(.blotter)
for (i in 1:NumPorts){
Str1 = paste0("portfolio.", MyPorts[i])
print(Str1)
# suppressWarnings(rm(Str1, pos = .blotter))
rm(Str1, pos = .blotter)
}
ls(.blotter)
Here's an illustration of the problem
> e1 <- new.env(parent = baseenv()) # create a new environment e1
> assign("obj", 1, envir = e1) # create object 'obj' in e1
> ls(e1)
[1] "obj"
> str1 <- "obj" # character vector containing "obj" (in the global environment)
> rm(str1, envir = e1) # this doesn't work
Warning message:
In rm(str1, envir = e1) : object 'str1' not found
> ls(e1)
[1] "obj"
This approach does not work because the first argument of rm
has to be the name of the object itself. There is no object str1
in e1
.
You have to use the list
argument if you want to use a character vector:
> rm(list = str1, envir = e1)
> ls(e1)
character(0)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.