在 quantstrat 中使用 ATR 作为指标的错误
Error in using ATR as an indicator in quantstrat
问题一:
我正在尝试在 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 # ,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)
}
您的 arguments
列表不适合 ATR()
指标。 ATR
没有名为“atrx”的参数。查看 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
和 add.signal
的 arguments 参数必须有一个 named 列表,其中名称(即本例中的 HLC、n)匹配在 add.indicator
函数中由 name
参数(例如这里的“ATR”)指定的函数的参数。
你的问题 2 是另一个问题,建议你 post 一个新问题,具体概述 NR7 是什么(它只是最后 7 个柱的滚动高 - 低等。是否有任何滞后定义等)。
问题一: 我正在尝试在 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 # ,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)
}
您的 arguments
列表不适合 ATR()
指标。 ATR
没有名为“atrx”的参数。查看 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
和 add.signal
的 arguments 参数必须有一个 named 列表,其中名称(即本例中的 HLC、n)匹配在 add.indicator
函数中由 name
参数(例如这里的“ATR”)指定的函数的参数。
你的问题 2 是另一个问题,建议你 post 一个新问题,具体概述 NR7 是什么(它只是最后 7 个柱的滚动高 - 低等。是否有任何滞后定义等)。