R-Quantstart:多种股票的测试策略 [英] R - Quantstart: Testing Strategy on Multiple Equities

查看:153
本文介绍了R-Quantstart:多种股票的测试策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立带有一些指标的基本交易策略.我的问题是我希望它可以在多个股票上运行,而不必指定我要测试的每个股票.

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)

我可以轻松生成带有Excel文档中的股票代码列表的向量.因此,这将为我生成200个独立的符号.生成所有指标后,我将针对单个资产创建以下测试策略

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.

将股票指定为OHLC的参数会产生以下错误

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'

即使我确实成功绑定了每个交易品种,我也认为该策略将对Stock Vector中每个交易品种的OHLC指标进行测试.

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默认执行您要的操作.

quantstrat does what you're asking by default.

这是一个例子:

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)) )

或者您可以查看quantstrat附带的许多示例中的几乎任何示例,因为几乎所有示例都使用多个符号.

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

这篇关于R-Quantstart:多种股票的测试策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆