Пожалуйста, игнорируйте мой предыдущий пост. Я пытаюсь запустить торговую стратегию в quantstrat и застреваю. Любая помощь будет принята с благодарностью. Код ниже, и я получаю следующую ошибку при запуске applyStrategy:
Ошибка в if (inherits(sret$indicators, "xts") & nrow(mktdata) == nrow(sret$indicators)) { : аргумент имеет нулевую длину
Код:
getSymbols("AUD=X",src="yahoo",from="1975-01-02")
colnames(`AUD=X`) <- c("Open", "High", "Low", "Close", "Vol", "Adj")
if (!exists('.blotter')) .blotter <- new.env()
.strategy <- new.env()
initdate = as.character("2006-05-15")
from = as.character("2007-05-15")
to = as.character("2018-06-01")
Sys.setenv(TZ = "UTC")
currency("USD")
stock(`AUD=X`, currency="USD", multiplier = 1)
tradesize <- 100000
initeq <- 100000
strategy.st <- portfolio.st <- account.st <- "firststrat"
rm.strat(strategy.st)
initPortf(portfolio.st, symbols = na.locf("AUD=X"), initDate = initdate, currency = "USD")
initAcct(account.st, portfolios = portfolio.st, initDate = initdate, currency = "USD", initEq = initeq)
initOrders(portfolio.st, initDate = initdate)
strategy(strategy.st, store = TRUE)
add.indicator(strategy = strategy.st,
name = "SMA",
arguments = list(x = quote(na.omit(Cl(mktdata))), n = 200),
label = "SMA200")
add.indicator(strategy = strategy.st,
name = "SMA",
arguments = list(x = quote(na.omit(Cl(mktdata))), n = 50),
label = "SMA50")
test <- applyIndicators(strategy = strategy.st, mktdata = na.locf(Cl(`AUD=X`)))
tail(test)
add.signal(strategy.st,
name = "sigCrossover",
arguments = list(columns = c("SMA.SMA50", "SMA.SMA200"),
relationship = "gt"),
label = "Crossover")
add.signal(strategy.st,
name = "sigComparison",
arguments = list(columns = c("SMA.SMA50", "SMA.SMA200"),
relationship = "lt"),
label = "Compare")
add.signal(strategy.st,
name = "sigThreshold",
arguments = list(column = "Close",
threshold = 1.5,
cross = FALSE,
relationship = "lt"),
label = "threshold_high")
add.signal(strategy.st,
name = "sigThreshold",
arguments = list(column = "Close",
threshold = 1,
cross = FALSE,
relationship = "gt"),
label = "threshold_low")
test2 <- applySignals(strategy = strategy.st, mktdata = test)
tail(test2)
add.rule(strategy.st, name = "ruleSignal",
arguments = list(sigcol = "threshold_low", sigval = FALSE,
orderqty = "all", ordertype = "market",
orderside = "short", replace = FALSE,
prefer = "Open"),
type = "enter")
add.rule(strategy.st, name = "ruleSignal",
arguments = list(sigcol = "threshold_high", sigval = TRUE,
orderqty = "all", ordertype = "market",
orderside = "long", replace = FALSE,
prefer = "Open"),
type = "enter")
applyStrategy(strategy = strategy.st, portfolios = portfolio.st)