【问题标题】:problems in quantstrat : argument "price" is missing, with no defaultquantstrat 中的问题:缺少参数“price”,没有默认值
【发布时间】:2017-07-18 19:29:16
【问题描述】:

我是一个尝试在 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))

在我的控制台上出现以下消息

测试

【问题讨论】:

    标签: r quantstrat


    【解决方案1】:

    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))
    

    【讨论】:

      【解决方案2】:

      global environment 我测试了这段代码,它可以工作。我必须安装 R 3.5 才能使用 quantstrat ,但它给出的答案与“Datacamp”练习中的答案相同。我也遇到了和你一样的问题,但是意识到我没有正确格式化 DVO add.indicator 代码。

      # Create a 200-day SMA
      spy_sma <- SMA(Cl(SPY), n = 200)
      
      # Create an RSI with a 3-day lookback period
      spy_rsi <- RSI(Cl(SPY), n = 3)
      
      # Plot the closing prices of SPY
      plot(Cl(SPY))
      # Overlay a 200-day SMA
      lines(SMA(Cl(SPY), n = 200), col = "red")
      
      add.indicator(strategy = strategy.st, 
                    # Add the SMA function
                    name = "SMA", #TTR function
                    # Create a lookback period
                    arguments = list(x = quote(Cl(mktdata)), n = 200), 
                    # Label your indicator SMA200 ________ equivalent to "<-"
                    label = "SMA200")
      
      # Add a 50-day SMA indicator to strategy.st
      add.indicator(strategy = strategy.st, 
                    # Add the SMA function
                    name = "SMA", 
                    # Create a lookback period
                    arguments = list(x = quote(Cl(mktdata)), n = 50), 
                    # Label your indicator SMA50
                    label = "SMA50")
      
      
      # Add an RSI 3 indicator to strategy.st
      add.indicator(strategy = strategy.st, 
                    # Add the RSI 3 function
                    name = "RSI", 
                    # Create a lookback period
                    arguments = list(x = quote(Cl(mktdata)), n = 3), 
                    # Label your indicator RSI_3
                    label = "RSI_3")
      
      
      library(TTR)
      library(quantmod)
      # Write the calc_RSI_avg function
      calc_RSI_avg <- function(price, n1, n2) {
        
        # RSI 1 takes an input of the price and n1
        RSI_1 <- RSI(price = price, n = 1)
        
        # RSI 2 takes an input of the price and n2
        RSI_2 <- RSI(price = price, n = 2)
        
        # RSI_avg is the average of RSI_1 and RSI_2
        RSI_avg <- (RSI_1 + RSI_2)/2
        
        # Your output of RSI_avg needs a column name of RSI_avg
        colnames(RSI_avg) <- "RSI_avg"
        return(RSI_avg)
      }
      
      # Add this function as RSI_3_4 to your strategy with n1 = 3 and n2 = 4
      add.indicator(strategy.st, name = "RSI_3_4", arguments = list(price = quote(Cl(mktdata)), n1 = 3, n2 = 4), label = "RSI_3_4")
      
      DVO <- function(HLC, navg = 2, percentlookback = 126) {
        
        # Compute the ratio between closing prices to the average of high and low
        ratio <- Cl(HLC)/((Hi(HLC) + Lo(HLC))/2)
        
        # Smooth out the ratio outputs using a moving average
        avgratio <- SMA(ratio, n = 2)
        
        # Convert ratio into a 0-100 value using runPercentRank()
        out <- runPercentRank(avgratio, n = percentlookback, exact.multiplier = 1) * 100
        colnames(out) <- "DVO"
        return(out)
      }
        
      # Add the DVO indicator to your strategy
      add.indicator(strategy = strategy.st, name = "DVO", 
                    arguments = list(HLC = quote(HLC(mktdata)), navg = 2, percentlookback = 126),
                    label = "DVO_2_126")
      
      # Use applyIndicators to test out your indicators
      test <- applyIndicators(strategy = strategy.st, mktdata = OHLC(SPY))
      
      # Subset your data between Sep. 1 and Sep. 5 of 2013
      test_subset <- test["2013-09-01/2013-09-05"]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-17
        • 1970-01-01
        • 2021-07-30
        • 1970-01-01
        • 2016-06-08
        • 2017-12-09
        相关资源
        最近更新 更多