比较ATR()的quantmod和quantstrat输出的问题

除其他事项外,我的目标是在许多股票上构建枝形吊灯出口,但是我在quantstrat和quantmod图表之间的代码某些方面苦苦挣扎。

以下代码将我带到我想要的quantmod位置:

chandelierstop <- function(HLC,n=22) {
  high22 <- runmax(Hi(HLC),n)
  custATR <- ATR(HLC,n)[,2]
  chandy <- high22 - custATR *3
  return(chandy)
}

但是,我在quantstrat本身的ATR代码中遇到了问题。我将附加示例代码,然后在下面突出显示该问题。问题的简介代码:

rm(list = ls(.blotter),envir = .blotter)

initdate <- "2018-01-01"
from <- "2018-01-01" 
to <- "2020-09-26" 

Sys.setenv(TZ= "EST") 

currency("USD") 


TENC <- getSymbols("0700.HK",from=from,to=to,adjust = TRUE,auto.assign=FALSE)

colnames(TENC) <- gsub(x = colnames(TENC),pattern = "0700.HK",replacement = "TENC")  


newsymbols <- c("TENC")#for simplicity,clearly I have other symbols in my code

stock(newsymbols,currency = "USD",multiplier = 1) 

tradesize <-10000 
initeq <- 100000 

strategy.st <- portfolio.st <- account.st <- "firststrat" 


rm.strat(portfolio.st)
rm.strat(strategy.st) 

##Initiate portfolios etc 

initPortf(portfolio.st,symbols = newsymbols,initDate = initdate,currency = "USD")

initacct(account.st,portfolios = portfolio.st,initEq = initeq)

initOrders(portfolio.st,initDate = initdate)
strategy(strategy.st,store=TRUE)


###Add indicators

add.indicator(strategy = strategy.st,name="ADX",arguments=list(HLC=quote(HLC(mktdata)),n=14),label="ADX")

add.indicator(strategy.st,name="runmin",arguments = list (x=quote(Lo(mktdata)[,1]),n=22),label = "Min22")

add.indicator(strategy.st,name="runmax",arguments = list (x=quote(Hi(mktdata)[,label = "Max22") 

到目前为止一切顺利。但是,我在ATR()部分遇到了问题。

我尝试过:

add.indicator(strategy.st,name="ATR",arguments = list(HLC=quote(HLC(mktdata)),label = "ATR22")

甚至:

myATR <- function(HLC,n = 22) {
  result <- ATR(HLC,2]   #,maType=SMA
  return(result)
}


add.indicator(strategy.st,name="myATR",arguments = list (HLC=quote(HLC(mktdata)),label = "ATRNew")

尝试调和我得到的东西

ATR(TENC,22) 

或者实际上

myATR(TENC,22)

但似乎没有。 Quantstrat中的ATR值是ATR()中一位数ATR的两倍。

为了清楚起见,我以以下方式将上述输出与quantstrat进行比较:

test = applyIndicators(strategy.st,mktdata=TENC)
test["2018-11::2018-11-30"]

myATR(TENC,22)的输出:

2018-11-01  6.111568
2018-11-02  7.006606
2018-11-05  7.241666
2018-11-06  7.494809
2018-11-07  7.829317
2018-11-08  8.353682
2018-11-09  8.507375
2018-11-12  8.561621
2018-11-13  8.064378
2018-11-14  8.245193
2018-11-15  8.028214
2018-11-16  8.353681
2018-11-19  8.100540
2018-11-20  8.218069
2018-11-21  7.829316
2018-11-22  8.091499
2018-11-23  7.946846
2018-11-26  8.109580
2018-11-27  8.272315
2018-11-28  8.516415
2018-11-29  9.158311
2018-11-30  9.058861

quantstrat的输出:

           X1.Min22 X1.Max22 atr.ATRNew 
2018-11-01 250.0137 323.2078   11.94287  
2018-11-02 250.0137 317.8376   12.66613  
2018-11-05 250.0137 313.6607   13.12721   
2018-11-06 250.0137 306.1027   13.11817   
2018-11-07 250.0137 305.1082   13.56117  
2018-11-08 250.0137 302.1247   13.64253  
2018-11-09 250.0137 302.1247   13.99512  
2018-11-12 250.0137 302.1247   13.90472  
2018-11-13 250.0137 302.1247   13.63349  
2018-11-14 250.0137 302.1247   12.90119   
2018-11-15 250.0137 302.1247   13.03680  
2018-11-16 250.0137 302.1247   12.81078  
2018-11-19 250.0137 302.1247   12.48531   
2018-11-20 250.0137 302.1247   12.37682   
2018-11-21 250.0137 302.1247   12.43107   
2018-11-22 250.0137 302.1247   12.06944   
2018-11-23 250.0137 302.1247   11.83438  
2018-11-26 250.0137 302.1247   11.68069  
2018-11-27 250.0137 302.1247   11.54507   
2018-11-28 250.0137 313.2630   11.61740   
2018-11-29 255.1850 319.0310   11.75301  
2018-11-30 258.5663 319.0310   11.38234  

因此,我正在将ATR(TENC,22)与atr.ATRNew进行比较。非常感谢您的帮助。

wubuwei111 回答:比较ATR()的quantmod和quantstrat输出的问题

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/1425479.html

大家都在问