0

问题 1:我正在尝试在 quantstrat 中使用 ATR 指标。我在 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")

我将名称映射到 ATR 函数,还通过 HLC 提供了 xts

问题 2:我将创建一个 NR7(7 个小节中的最窄范围)。我如何添加自定义指标,因为这在 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)
                             
}
4

1 回答 1

0

您的arguments列表不适合该ATR()指标。没有名为“atrx”的参数ATR。查看formals(ATR)/ 函数定义ATR以查看正确的参数名称。

这解决了这个问题:

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

add.indictor和的 arguments 参数add.signal必须有一个命名列表,其中名称(即 HLC,n,在此示例中)与函数中的name参数(例如,此处的“ATR”)指定的函数的参数匹配add.indicator

您的问题 2 是另一个问题,建议您发布一个新问题,专门概述 NR7 是什么(它只是最后 7 个柱的滚动高 - 低等。定义中是否有任何滞后等)。

于 2022-01-14T06:48:51.137 回答