I was running backtestis with blotter package on many symbols and wanted to parallize the whole process with foreach loops.
Specifying portfolios inside the foreach loops like this:
.blotter <- new.env()
b.strategy <- paste(symbol, "strat", sep="_")
initPortf(b.strategy,symbol, initDate='2006-01-01')
initAcct(b.strategy,portfolios=b.strategy, initDate='2007-01-01', initEq=1e6)
Specifying portfolios inside the foreach loops like this:
.blotter <- new.env()
b.strategy <- paste(symbol, "strat", sep="_")
initPortf(b.strategy,symbol, initDate='2006-01-01')
initAcct(b.strategy,portfolios=b.strategy, initDate='2007-01-01', initEq=1e6)
Then running trough the data and adding transactions with:
# enter long position
addTxn(b.strategy, Symbol=symbol, TxnDate=CurrentDate,
TxnPrice=ClosePrice, TxnQty = tradeSize , TxnFees=-1)
It took some time to find out why I could not run updatePortf since it gave the error "object 'AAPL' not found".
Inside the loop I assigned the symbol with
assign(symbol, data)
Instead I needed to assign it with:
assign(symbol, data, envir=.GlobalEnv) since the parent frame to updatePortf inside the foreach loop is not the same as to assign and get.