Note that there are some explanatory texts on larger screens.

plurals
  1. POConstruct name of blotter portfolio on the fly
    text
    copied!<p>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:</p> <pre><code>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 </code></pre> <p>The portfolios are not found or removed:</p> <pre><code>&gt; ls(.blotter) [1] "portfolio.Port1" "portfolio.Port2" "portfolio.Port3" "portfolio.Port4" </code></pre> <p>What am I doing wrong here? How do I get R to use the name I've constructed in Str1?</p> <pre><code>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) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload