r - Quantstrat : argument "price" is missing, 中的问题,没有默认值

标签 r quantstrat

我是一个新用户,尝试在 quantstrat 上进行回溯测试,当我运行以下代码时,会在底部显示消息。谁能帮我解决这个问题吗?

library(quantmod)
initdate = "1999-01-01"
from = "2003-01-01"
to = "2015-06-30"
remove(srs)
symbols("spy")
src = "yahoo"
getSymbols("SPY", from = from, to = to, src = src, adjust = TRUE)
plot(Cl(SPY))
getSymbols("GBP", from = from, to = to, src = src, adjust = TRUE)
lines(SMA(Cl(SPY),n = 200, col = "red"))
Sys.setenv(TZ = "UTC")
library(quantstrat)
currency("USD")
library(quantmod)
getSymbols("GDX", from = from, to = to, src = src, adjust = TRUE)
stock("GDX", currency = "USD")
stock("SPY", currency = "USD")
tradesize <- 100000
initeq <- 100000
strategy.st <-"firststrat"
portfolio.st <- "firststrat"
account.st <- "firststrat"
rm.strat(strategy.st)
initPortf(portfolio.st, symbols = "SPY", initdate = initdate, currency = "USD")
initAcct(account.st, portfolio = portfolio.st, initDate = initdate, currency = "USD",initEq = initeq)
initOrders(portfolio.st, initDate = initdate)
strategy(strategy.st, store = TRUE)
spy_sma <- SMA(x=Cl(SPY), n = 200)
spy_rsi <- RSI(price=Cl(SPY), n=3)
plot(Cl(SPY))
lines(SMA(Cl(SPY), n=200, col = "red"))
"trend"
plot(Cl(SPY))
plot(RSI(Cl(SPY), n = 2))
"reversion"
add.indicator(strategy = strategy.st, 
              name = "SMA",
              arguments = list(x=quote(Cl(mktdata)), n = 200),
              label = "SMA200")
add.indicator(strategy = strategy.st,
              name = "SMA",
              arguments = list(x=quote(Cl(mktdata)), n = 50),
              label = "SMA50")
add.indicator(strategy = strategy.st,
              name = "RSI",
              arguments = list(x=quote(Cl(maktdata)), n = 3),
              label = "RSI_3")
RSI_avg <- function(price, n1, n2) {
  rsi_1 <- RSI(price = price, n = 1)
  rsi_2 <- RSI(price = price, n = 2)
  RSI_avg <- (rsi_1/rsi_2)/2
  colnames(RSI_avg) <- "RSI_avg"
  return (RSI_avg)
}
add.indicator(strategy.st, name = "RSI_avg", arguments = list(price = quote(Cl(mktdata)), n1 = 3, n2 = 4), label = "RSI_3_4")
DVO <-function(HLC, navg = 2, percentlookback = 126){
  ratio <- Cl(HLC/(Hi(HLC) + Lo(HLC))/2)
  avgratio <- SMA(ratio, n = navg)
  out <- runPercentRank(avgratio, n = percentlookback, exact.multiplier = 1)*100
  colnames(out) <- "DVO"
  return(out)
}
add.indicator(strategy.st, name = "DVO", arguments = list (HLC=quote(HLC(mktdata)),navg = 2, percentlookback = 126), label = "DVO_2_126")
test <- applyIndicators(strategy = strategy.st, mktdata = OHLC(SPY))

我的控制台上显示以下消息

test <- applyIndicators(strategy = strategy.st, mktdata = OHLC(SPY)) Error in try.xts(price, error = as.matrix) : argument "price" is missing, with no default

最佳答案

RSI 采用参数 price,而不是 x。另请注意在 DVO 中构建比率的方式。您在 RSI_3 中的 mktdata 也有拼写错误。目前尚不清楚为什么您在此代码中请求“GBP”,也不清楚为什么还调用 symbols("spy"),但它们实际上并不是您问题的一部分。

这些更改应该能让您的代码正常工作:

library(quantmod)
initdate = "1999-01-01"
from = "2003-01-01"
to = "2015-06-30"
#remove(srs)
#symbols("spy")
src = "yahoo"
getSymbols("SPY", from = from, to = to, src = src, adjust = TRUE)
plot(Cl(SPY))
getSymbols("GBP", from = from, to = to, src = src, adjust = TRUE)
lines(SMA(Cl(SPY),n = 200, col = "red"))
Sys.setenv(TZ = "UTC")
library(quantstrat)
currency("USD")
library(quantmod)
getSymbols("GDX", from = from, to = to, src = src, adjust = TRUE)
stock("GDX", currency = "USD")
stock("SPY", currency = "USD")
tradesize <- 100000
initeq <- 100000
strategy.st <-"firststrat"
portfolio.st <- "firststrat"
account.st <- "firststrat"
rm.strat(strategy.st)
initPortf(portfolio.st, symbols = "SPY", initdate = initdate, currency = "USD")
initAcct(account.st, portfolio = portfolio.st, initDate = initdate, currency = "USD",initEq = initeq)
initOrders(portfolio.st, initDate = initdate)
strategy(strategy.st, store = TRUE)
spy_sma <- SMA(x=Cl(SPY), n = 200)
spy_rsi <- RSI(price=Cl(SPY), n=3)
plot(Cl(SPY))
lines(SMA(Cl(SPY), n=200, col = "red"))
"trend"
plot(Cl(SPY))
plot(RSI(Cl(SPY), n = 2))
"reversion"
add.indicator(strategy = strategy.st, 
              name = "SMA",
              arguments = list(x=quote(Cl(mktdata)), n = 200),
              label = "SMA200")
add.indicator(strategy = strategy.st,
              name = "SMA",
              arguments = list(x=quote(Cl(mktdata)), n = 50),
              label = "SMA50")
add.indicator(strategy = strategy.st,
              name = "RSI",
              arguments = list(price=quote(Cl(mktdata)), n = 3),
              label = "RSI_3")
RSI_avg <- function(price, n1, n2) {
    rsi_1 <- RSI(price = price, n = 1)
    rsi_2 <- RSI(price = price, n = 2)
    RSI_avg <- (rsi_1/rsi_2)/2
    colnames(RSI_avg) <- "RSI_avg"
    return (RSI_avg)
}
add.indicator(strategy.st, name = "RSI_avg", arguments = list(price = quote(Cl(mktdata)), n1 = 3, n2 = 4), label = "RSI_3_4")
DVO <-function(HLC, navg = 2, percentlookback = 126){
    ratio <- Cl(HLC)/(Hi(HLC) + Lo(HLC))/2
    avgratio <- SMA(ratio, n = navg)
    out <- runPercentRank(avgratio, n = percentlookback, exact.multiplier = 1)*100
    colnames(out) <- "DVO"
    return(out)
}
add.indicator(strategy.st, name = "DVO", arguments = list (HLC=quote(HLC(mktdata)),navg = 2, percentlookback = 126), label = "DVO_2_126")
test <- applyIndicators(strategy = strategy.st, mktdata = OHLC(SPY))

关于r - Quantstrat : argument "price" is missing, 中的问题,没有默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45175481/

相关文章:

R-量子启动 : Testing Strategy on Multiple Equities

r - 当 quantstrat 中 applyIndi​​cators 或 applyStrategy 时,如何在自定义函数内获取当前 "symbol"

r - 是否可以一键将整个 Shiny 应用程序(包括 react 值和多个表格和图表)下载为 pdf 或图像?

r - Quantstrat 能否用于为生产系统生成订单

r - do.call 在函数中 - 找不到对象错误

r - R 和 Stata 中全局变量的危险示例

r - 为 R 最新 R 版本安装 quantstrat ()

r - Quantstrat 多种货币。 Blotter::UpdateAcct 中可能有错误吗?

r - 双 y 轴和 facet_wrap

r - 在 R 中,如何在数据框中找到所有字典单词的位置?