3

I'm building a basic trading strategy with a few indicators. My problem is that I want it to run on multiple equities without having to specify each individual equity i want to test.

Currently i am able to use a vector to get multiple symbols at once such as below

 # Get Shares from Yahoo Finance
 Stocks<- ASX_200_Companies_Copy$Code
 getSymbols(Stocks, from = from, to = to, src =  "yahoo", adjust =  TRUE)

I can easily generate the vector with a list of stock codes in an excel document. So this would generate for me 200 separate symbols. After i generate all my indicators, i would create a test strategy as below on an individual asset

# Test the strategy
test_Master <- applyIndicators(strategy.st.Master, mktdata = OHLC(BHP.AX))
Master_Strategy <- applySignals(strategy = strategy.st.Master, mktdata = test_Master)

In this case i would only be able to test my strategy one asset at a time, which if i want to find trends in large data sets would not be effective.

Specifying Stocks as the argument for OHLC produces the following error

 test_Master <- applyIndicators(strategy.st.Master, mktdata = OHLC(Stocks))
 Error in Cl(mktdata) : subscript out of bounds: no column name containing "Close"

I thought to simply cbind a few of the separate stocks that generate. However this does not work either.

Stocks <- cbind(BHP.AX, CBA.AX)
test_Master <- applyIndicators(strategy.st.Master, mktdata = OHLC(Stocks))
Error in runSum(x, n) : ncol(x) > 1. runSum only supports univariate 'x'

And even if i did successfully cbind each symbol, i imagine the strategy would test the indicators on the OHLC for each of the symbols in the Stocks vector.

Is there anyway to test a quantstrat strategy on multiple assets at one time?

Any thoughts/feedback would be appreciated.

David
  • 31
  • 1
  • Welcome to Stackoverflow!. I highly recommend working with environments, their structure lends to ease in manipulation and applying various functions/strategies.See this for an [example](http://stackoverflow.com/questions/40119177/pairwise-correlation-r-code/) – Silence Dogood Feb 03 '17 at 13:33
  • If you've solved this problem, you can add answer. This will help others having same problem. – Tushar Mar 14 '17 at 03:15
  • @OdeToMyFiddle, quantstrat and blotter already make extensive use of environments internally to avoid copies where possible, and as stroage containers for persistent information. – Brian G. Peterson May 09 '18 at 18:05
  • Thanks @BrianG.Peterson for the explanation. I have not used `quantstrat` and `blotter` much but am heavy user of your `PerformanceAnalytics` package. Also your `PortfolioAnalytics` package has simplified optimization problems a lot, thank you for creating and maintaining such excellent packages! – Silence Dogood May 10 '18 at 09:55

1 Answers1

5

quantstrat does what you're asking by default.

Here's an example:

data(stratBBands) #load a test strategy, you'd use your own
symbols = c("XLF", "XLP", "XLE", "XLY", "XLV", 
            "XLI", "XLB", "XLK", "XLU")

getSymbols(symbols
           , src='yahoo'
           , index.class=c("POSIXt"
           ,"POSIXct")
           , from='1999-01-01')

out<-try(applyStrategy(strategy=stratBBands 
                      , portfolios='bbands'
                      , parameters=list(sd=2,n=60)) )

Or you can look at almost any of the many examples included with quantstrat, since almost all of them use multiple symbols.

Brian G. Peterson
  • 3,531
  • 2
  • 20
  • 21
  • #Brian, I just ran your code above and got an error: "Portfolio bbands not found, use initPortf() to create a new portfolio". In following yours & quantstrat examples of others, I normally see iniitPortf() being used now (perhaps it wasn't needed by quantstrat, circa mid-2017 ??), so I included the following line: initPortf(portfolios = "bbands", symbols), but still got an error, "argument 'symbols' is missing, with no default". Any suggestions, comments? (I'm moving now to the other examples to which you referred, but am commenting here to learn QS at every opportunity). Thanks. – W Barker Jul 25 '19 at 00:12