Question 1: I am trying to use the ATR indicator in quantstrat. I am getting error Error in try.xts(HLC, error = as.matrix) : argument "HLC" is missing, with no default
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
Question 2: I will be creating a NR7 (narrowest range in 7 bars). How do i add a custom indicator for the same as this does not exist in 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. There is no argument named "atrx" for ATR
. Look at formals(ATR)
/ the function definition for ATR
to see the correct parameter names.
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 (i.e. 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.
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).