本文介绍了具有外部指标和Quantstrat中的多个股票的单变量误差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用与OLHC对象合并的交易数据外部的指标.我们的目标是建立一个解决多个股票的Quantstrat模型,但是我们继续收到错误消息,表明我们没有适当建立这样的模型.此外,即使我们未明确使用EMA,该错误也涉及EMA.

We use indicators external to trading data that we merge with an OLHC object. Our objective is to build a quantstrat model that addresses multiple equities, but we continue to get error messages that indicate we have not properly built such. Furthermore, the error refers to EMA even though we do not explicitly use an EMA.

我从这里开始接受#FXQuantTrader代码作为答案: quantstrat:如何创建多个指标,信号规则,我可以使用OHLC数据外部的指标以及add.signal调用的自定义函数,但无法跳至多个股票.

I started with #FXQuantTrader code accepted as the answer here: quantstrat: how to create multiple indicators, signal rules, which I can make work with indicators external to OHLC data and a custom function called by add.signal, but cannot make the jump to multiple equities.

此堆栈溢出条目 R-Quantstart:多种股票的测试策略旨在直接解决该问题,但是提供的示例为

This Stack Overflow entry R - Quantstart: Testing Strategy on Multiple Equities purports to address the subject directly, but the example provided offers the suggestion to

这引出了如何制定多股权策略的问题.尝试使用我的策略来实现本文中的applyStrategy调用会产生错误

which begs the question of how to build a multi-equity strategy. Trying to implement the applyStrategy call from this post using my strategy yields the error

注释此调用的参数部分会使我回到原始错误.

Commenting out the parameters section of this call gets me back to the original error.

Quantstrat文档 https://www.rdocumentation.org/packages/quantstrat/versions/0.16.2 涉及以下内容,但每个示例仅使用一个权益:(a)MaCross(文档主体),(b)DataCamp课程 https://www.datacamp.com/community/blog/financial-trading-in- r-with-ilya-kipnis (我将其完整介绍),(c)qauntstrat http://past.rinfinance.com/agenda/2018/BrianPeterson.html#1 在R/Finance 2018会议上使用一种EFT:EEM.

The Quantstrat documentation https://www.rdocumentation.org/packages/quantstrat/versions/0.16.2 refers to the following, but each example uses only one equity: (a) MaCross (body of the documentation), (b) DataCamp course https://www.datacamp.com/community/blog/financial-trading-in-r-with-ilya-kipnis (I took it in its entirety), (c) the presentation of qauntstrat http://past.rinfinance.com/agenda/2018/BrianPeterson.html#1 at the R/Finance 2018 conference uses one EFT: EEM.

其他资源包括Guy Yollin的笔记 http://www.r-programming.org/papers,但他的第一个(设置)卡座的幻灯片18、21和41呈现的标准偏差和回溯度为"n",与上述未说明的SO帖子相同.我尝试了各种组合,但仍然有相同的错误.

Other resources include Guy Yollin’s notes http://www.r-programming.org/papers, but slides 18, 21 and 41 of his first (setup) deck presents the same standard deviation and lookback "n" as does the SO post referred to above without an explanation. I tried various combinations, but still had the same errors.

伊尔娜·基普尼斯(Ilna Kipnis)的坚果和Bolts…"博客文章 https://quantstrattrader.wordpress.com/2014/09/09/nuts-and-bolts-of-quantstrat-part-i/演示了多个股票的用法,但他没有在applyStrategy调用中列出参数,所以也许这不是我的问题所在.蒂姆·崔斯(Tim Trice)的在线Quantstrat书(Quantstrat文档所指的另一种资源)似乎证实了这一点,他在多重股权申请的背景下说

Ilna Kipnis’s "Nuts & Bolts…" blog posts https://quantstrattrader.wordpress.com/2014/09/09/nuts-and-bolts-of-quantstrat-part-i/ demonstrate the use of multiple equities, but he does not list parameters in the applyStrategy call, so perhaps this is not where my problem is. This seems to be confirmed by to Tim Trice’s online quantstrat book, (another resource to which the quantstrat documentation refers) where he says – in the context of a multiple equity application – that

我也尝试了应用"功能,但没有成功.

I also experimented with the "apply" function but without any success.

.blotter <- new.env()
.strategy <- new.env()

fastMA = 12 
slowMA = 26 

currency('USD')
startDate='2017-03-24'
endDate = "2017-08-05" 

initEq=1000000
portfolio.st='macd'
account.st='macd'

symbols <- c("NOV", # National-Oilwell Varco, Inc.
         "AERI", # Aerie Pharmaceuticals Inc
         "AGN" # Allergan plc
)

Cx.AERI <- c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0)
Cx.AGN <- c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
Cx.NOV <- c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0)
getSymbols(symbols,from=startDate, to=endDate) # gets xts object

Tplus <- merge.xts(AERI, AGN, NOV, Cx.AERI,Cx.AGN,Cx.NOV) 
stock.st='Tplus'
initPortf(portfolio.st,symbols=stock.st)
initAcct(account.st,portfolios=portfolio.st)
initOrders(portfolio=portfolio.st)
stock(symbols,currency="USD", multiplier =1) 
strat.st<-portfolio.st
strategy(strat.st, store=TRUE)

add.indicator(strat.st, name = "MACD", 
          arguments = list(x=quote(Cl(mktdata)),
                           nFast=fastMA, 
                           nSlow=slowMA),
          label='_' 
)

macdSMAsig2 <- function(data) {
  sig <- data[, "Cx._"] >0 & data[, "macd._"] > 0
  colnames(sig) <- "upSig"
  sig
}

add.signal(strat.st,name="macdSMAsig2",
       arguments = list(data = quote(mktdata)),
       label="enterSig"
)

add.signal(strat.st,name="sigThreshold",
       arguments = list(column="signal._",
                        relationship="lt",
                        threshold=0,
                        cross=TRUE),
       label="signal.lt.zero"
)

add.rule(strat.st,name='ruleSignal', 
     # be careful to get the label of the signal column correct:
     arguments = list(sigcol="upSig.enterSig",
                      sigval=TRUE, 
                      orderqty=100, 
                      ordertype='market', 
                      orderside='long', 
                      threshold=NULL),
     type='enter',
     label='enter',
     storefun=FALSE
)

add.rule(strat.st,name='ruleSignal', 
     arguments = list(sigcol="signal.lt.zero",
                      sigval=TRUE, 
                      orderqty='all', 
                      ordertype='market', 
                      orderside='long', 
                      threshold=NULL,
                      orderset='exit2'),
     type='exit',
     label='exit'
)

out<-applyStrategy(strat.st , portfolios=portfolio.st,verbose=TRUE)

我期望进行一些交易,但是却收到此错误消息:

I expected some trades, but instead got this error message:

 >Error in EMA(c(45.849998, 45.549999, 45.450001, 45.25, 45.450001, 
 45.349998,  :   ncol(x) > 1. EMA only supports univariate 'x'

推荐答案

我认为您的问题一定是因为MACD必须调用EMA且数据的尺寸存在冲突(不能一次按您期望的方式一次被馈入) ).据我所知,quantstrat一次只能对一个以上的股票进行一次个人回测,却不知道其他回测中发生了什么.您可能要做的一件事是分别运行每个回测,存储结果/事务,然后在循环中将其组合使用以组合结果以创建某种投资组合"回报.意思是,这将为您提供以下信息:是否/何时您将在另一个交易品种中拥有头寸,相应地更新损益表,等等.

I think your issue must be because MACD must call EMA and there is a clash in the dimensions of the data (not being fed one at a time as you expect). As far as I know quantstrat can only run an INDIVIDUAL backtest for more than one equity at a time but not be aware of what happened in the other backtests. One thing you could possibly do is run each backtest individually, store the results/transactions, and then use that in a loop to combine the results create a sort of "portfolio" return. Meaning this would give you details of if/when you would have a position in another symbol, update your P/L accordingly, etc.

这篇关于具有外部指标和Quantstrat中的多个股票的单变量误差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 06:56