R / quanstrat / apply策略

时间:2018-07-15 00:57:10

标签: r

R的新手。我试图在quanstrat中运行一种贸易策略,但我陷入困境。任何帮助将不胜感激。代码在下面,我收到 运行applyStrategy时出现以下错误:     getPrice(mktdata,Preferred = Preferred)中的错误:     下标超出范围,从数据中看不到价格

AUD_USD <- Quandl("BOE/XUDLADD", type = "xts")
colnames(AUD_USD) <- c("AUD_USD")

if (!exists('.blotter')) .blotter <- new.env()
.strategy <- new.env()

initdate = as.character("1999-01-01")
from = as.character("2000-01-01")
to = as.character("2018-06-01")
Sys.setenv(TZ = "UTC")
currency("USD")
stock("AUD_USD", currency="USD", multiplier = 1)

tradesize <- as.numeric(100000)
initeq <- as.numeric(100000)
strategy.st <- portfolio.st <- account.st <- "firststrat"

rm.strat(strategy.st)
initPortf(portfolio.st, symbols = c("AUD_USD"), 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(mktdata[,1]), n = 200),
        label = "SMA200")

add.indicator(strategy = strategy.st,
        name = "SMA",
        arguments = list(x = quote(mktdata[,1]), n = 50),
        label = "SMA50")

test <- applyIndicators(strategy = strategy.st, mktdata = AUD_USD)
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 = "AUD.USD",
                      threshold = 1.5,
                      cross = FALSE,
                      relationship = "lt"),
     label = "threshold_high")

add.signal(strategy.st,
     name = "sigThreshold",
     arguments = list(column = "AUD.USD",
                      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 个答案:

没有答案