问题 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)
}