R - Quantstart: Testing Strategy on Multiple Equities

人盡茶涼 提交于 2019-12-10 18:14:17

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/42023896/r-quantstart-testing-strategy-on-multiple-equities

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!