简体   繁体   English

在 quantstrat 中使用 ATR 作为指标的错误

[英]Error in using ATR as an indicator in quantstrat

Question 1: I am trying to use the ATR indicator in quantstrat.问题 1:我正在尝试在 quantstrat 中使用 ATR 指标。 I am getting error Error in try.xts(HLC, error = as.matrix): argument "HLC" is missing, with no default我在 try.xts(HLC, error = as.matrix) 中收到错误错误:缺少参数“HLC”,没有默认值

add.indicator(strategy = strategy.st, 
              # correct name of function:
              name = "ATR",
              arguments = list(atrx=quote(HLC(mktdata)), n=14), 
              label="atr")

I mapped the name to ATR function and also provided the xts via HLC我将名称映射到 ATR function 并通过 HLC 提供了 xts

Question 2: I will be creating a NR7 (narrowest range in 7 bars).问题 2:我将创建一个 NR7(7 个小节中的最窄范围)。 How do i add a custom indicator for the same as this does not exist in quantstrat.我如何添加自定义指标,因为这在 quantstrat 中不存在。

    library(quantstrat)



portfolio.st <- "Port.Luxor"
account.st <- "Acct.Luxor"
strategy.st <- "Strat.Luxor"
init_date <- "2007-12-31"
start_date <- "2008-01-01"
end_date <- "2009-12-31"
adjustment <- TRUE
init_equity <- 1e4 # $10,000

Sys.setenv(TZ = "UTC")

currency('USD')

symbols <- c(
  "IWM", # iShares Russell 2000 Index ETF
  "QQQ", # PowerShares QQQ TRust, Series 1 ETF
  "SPY", # SPDR S&P 500 ETF Trust
  "TLT" # iShares Barclays 20+ Yr Treas. Bond ETF
)

getSymbols(Symbols = symbols,
           src = "yahoo",
           index.class = "POSIXct",
           from = start_date,
           to = end_date,
           adjust = adjustment)

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

rm.strat(portfolio.st)
rm.strat(account.st)

initPortf(name = portfolio.st,
          symbols = symbols,
          initDate = init_date)

initAcct(name = account.st,
         portfolios = portfolio.st,
         initDate = init_date,
         initEq = init_equity)

initOrders(portfolio = portfolio.st,
           symbols = symbols,
           initDate = init_date)

strategy(strategy.st, store = TRUE)

add.indicator(strategy = strategy.st,
              name = "SMA",
              arguments = list(x = quote(Cl(mktdata)),
                               n = 10),
              label = "nFast")



add.indicator(strategy = strategy.st,
              name = "SMA",
              arguments = list(x = quote(Cl(mktdata)),
                               n = 30),
              label = "nSlow")

 

add.indicator(strategy = strategy.st, 
              # correct name of function:
              name = "ATR",
              arguments = list(atrx=quote(HLC(mktdata)), n=14), 
              label="atr")




add.signal(strategy = strategy.st,
           name="sigCrossover",
           arguments = list(columns = c("nFast", "nSlow"),
                            relationship = "gte"),
           label = "long")

 

add.rule(strategy = strategy.st,
         name = "ruleSignal",
         arguments = list(sigcol = "long",
                          sigval = TRUE,
                          orderqty = 100,
                          ordertype = "stoplimit",
                          orderside = "long",
                          threshold = 0.0005,
                          prefer = "High",
                          TxnFees = -10,
                          replace = FALSE),
         type = "enter",
         label = "EnterLONG")

 

 

add.rule(strategy.st,
         name = "ruleSignal",
         arguments = list(sigcol = "long",
                          sigval = TRUE,
                          orderside = "short",
                          ordertype = "market",
                          orderqty = "all",
                          TxnFees = -10,
                          replace = TRUE),
         type = "exit",
         label = "Exit2LONG")




results <- applyStrategy(strategy.st, portfolios = portfolio.st, verbose = TRUE)
updatePortf(portfolio.st)
updateAcct(account.st)
updateEndEq(account.st)



for(symbol in symbols) {
  
  inds <- applyIndicators(strategy.st, get(symbol))
  print(  head(inds))
  # Optionally, if you also want the strategy signals per symbol, do this:
  sigs <- applySignals(strategy.st, inds)
  print(  head(sigs))  
  
  chart.Posn(portfolio.st, Symbol = symbol)
                             
}

Your arguments list is not right for the ATR() indicator.您的arguments列表不适合ATR()指标。 There is no argument named "atrx" for ATR . ATR没有名为“atrx”的参数。 Look at formals(ATR) / the function definition for ATR to see the correct parameter names.查看formals(ATR) / ATR的function 定义以查看正确的参数名称。

This fixes the issue:这解决了这个问题:

add.indicator(strategy = strategy.st, 
              # correct name of function:
              name = "ATR",
              arguments = list(HLC=quote(HLC(mktdata)), n=14), 
              label="atr")

the arguments parameter for add.indictor and add.signal must have a named list, where the names (ie HLC, n, in this example) match the parameters of the function specified by the name argument (e..g "ATR" here) in the add.indicator function. add.indictoradd.signal的 arguments 参数必须有一个命名列表,其中名称(即 HLC,在本例中为 n)与由name参数指定的 function 的参数匹配(例如,此处为“ATR” ) 在add.indicator function 中。

Your question 2 is another question, suggest you post a new question specifically outlining what NR7 is (is it simply the rolling High - Low for the last 7 bars, etc. Is there any lag in the definition, etc).您的问题 2 是另一个问题,建议您发布一个新问题,专门概述 NR7 是什么(它只是最后 7 个柱的滚动高 - 低等。定义中是否有任何滞后等)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM