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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With