请忽略我以前的帖子。我试图在quanstrat中运行一种贸易策略,但我陷入困境。任何帮助将不胜感激。代码在下面,当我运行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)
答案 0 :(得分:1)
一年多以前,Yahoo API已关闭。试试这个吧。
library(quantmod)
# enter tickers to download time-series data
e <- new.env()
getSymbols("SBUX", env = e)
pframe <- do.call(merge, as.list(e))
head(pframe)
您也可以尝试。
library(quantmod)
library(tidyverse) # rather than just dplyr
df <- data.frame(getSymbols(Symbols = 'SBUX', env = NULL))