【问题标题】:QuantStrat sigFormulaQuantStrat sigFormula
【发布时间】:2014-08-10 04:01:38
【问题描述】:

我在运行 sigFormula 时遇到问题。我收到错误:

Error in is.character(x) : 'x' is missing
Error in `colnames<-`(`*tmp*`, value = seq(ncol(tmp_val))) : 
  attempt to set colnames on object with less than two dimensions

它似乎出现在applySignalapplyStrategy 中。这是什么意思? sigFormula里面有bug吗?

我的代码:

library(quantstrat)

#set currency
currency('USD')

#Get Data
initDate="2000-01-01" 
endDate="2014-05-31"  
symbols = c("SPY")
for(symbol in symbols){
    stock(symbol, currency="USD",multiplier=1) #financial instrument and 
    getSymbols(symbols,from=initDate,to=endDate)
    assign(symbol, adjustOHLC(get(symbol),use.Adjusted=TRUE))
}

#Initialize Portfolio,Strategy,Orders,and Account
initEq=1000000
portfolio.st='db'
account.st='db'
initPortf(portfolio.st,symbols=symbols, initDate=initDate)
initAcct(account.st,portfolios=portfolio.st, initDate=initDate)
initOrders(portfolio=portfolio.st,initDate=initDate)
lookback = 50

strat<- strategy(portfolio.st)

ROC2 <- function(x, n = 50, ...)
{
    roc <- ROC(Cl(x), n)
    colnames(roc) <- NULL
    roc
}

TSI <- function(x, nShort = 10, nLong = 100)
{
    atr <- ATR(HLC(x), nShort)$atr
    absd <- abs(diff(Cl(x), nShort))
    ratio <- absd / atr
    tsi <- runMean(runMean(ratio, nShort), nLong)

    # SMA gives the same result as runMean
    #tsi <- SMA(SMA(ratio, nShort), nLong)
    # not needed, as runMean removes colname
    #colnames(tsi) <- NULL
    tsi
}

strat <- add.indicator(strategy=strat, name="SMA", arguments=list(x = quote(Cl(mktdata)), n=50), label="SMA50")
strat <- add.indicator(strategy=strat, name="ROC2", arguments=list(x = quote(Cl(mktdata)), n=150), label="ROC150")
strat <- add.indicator(strategy=strat, name="TSI", arguments=list(x = quote((mktdata)), nShort=10, nLong=100), label="TSI")
strat <- add.indicator(strategy=strat, name="RSI", arguments=list(price = quote(Cl(mktdata)), n=2), label="RSI")

# for debugging purposes
#strat.ind <- applyIndicators(strategy=strat, mktdata=get(symbol))
#tail(strat.ind)


# Cl > SMA(50)
strat <- add.signal(strat, name="sigComparison", arguments=list(columns=c("Close", "SMA50"), relationship="gt"), label="Cl.gt.SMA50")
# ROC(150) > 0
strat <- add.signal(strat, name="sigThreshold", arguments=list(column="ROC150", threshold=0), label="ROC150.gt.zero")
# LongCond: either Cl>SMA or ROC(150) == TRUE
strat <- add.signal(strat, name="sigFormula", arguments=list(columns=c("Cl.gt.SMA50", "ROC150.gt.zero"), formula="(Cl.gt.SMA50 == 1) | (ROC150.gt.zero == 1)", cross=FALSE), label="LongCond")
# Setup1 = LC and (TSI/RSI or TSI/RSI)
strat <- add.signal(strat, name="sigFormula",  arguments=list(columns=c("LongCond", "TSI", "RSI"), formula="(LongCond == 1) & (((TSI > 1.65) & (RSI < 18)) | ((TSI < 1.65) & (RSI < 8)))", cross=FALSE), label="Setup1")
# Setup2 = LC and (TSI/RSI or TSI/RSI)
strat <- add.signal(strat, name="sigFormula", arguments=list(columns=c("LongCond", "TSI", "RSI"), formula="(LongCond != 1) & (((TSI > 1.65) & (RSI < 13)) | ((TSI < 1.65) & (RSI < 2)))", cross=FALSE), label="Setup2")
# Entry = Setup1 or Setup2 == TRUE
strat <- add.signal(strat, name="sigFormula", arguments=list(columns=c("Setup1", "Setup2"), formula="((Setup1 == 1) | (Setup2 == 1))", cross=FALSE), label="EntryCond")
# Exit = TSI/RSI or TSI/RSI
strat <- add.signal(strat, name="sigFormula", arguments=list(columns=c("TSI", "RSI"), formula="(((TSI > 1.65) & (RSI > 61)) | ((TSI < 1.65) & (RSI > 39)))", cross=FALSE), label="ExitCond")

# For debugging purposes. For some reason this does not work, even though
# applyStrategy creates the columns properly?
#strat.sig <- applySignals(strategy=strat, mktdata=SPY, indicators=strat.ind)
#tail(strat.sig)

# entry rule
strat <- add.rule(strat, name="ruleSignal", arguments=list(sigcol="EntryCond", sigval=TRUE, orderqty=1000, ordertype="market", orderside="long", pricemethod="market"), type="enter")
# exit rule
# this generates a large number of orders into order book, which
# subsequently are rejected
strat <- add.rule(strat, name="ruleSignal", arguments=list(sigcol="ExitCond", sigval=TRUE, orderqty="all", ordertype="market", orderside="long", pricemethod="market", orderset="exit1"), type="exit")


out <- try(applyStrategy(strategy=strat, portfolios='db'))
updatePortf(Portfolio=strat.name, Dates=paste("::", as.Date(Sys.time()), sep=""))
chart.Posn(Portfolio = strat.name, Symbol=symbol)

【问题讨论】:

    标签: r quantstrat


    【解决方案1】:

    查看mktdata 对象。它没有 TSI 列,因此您在 sigFormula 中引用该列的尝试失败,词法范围搜索会找到您的 TSI 函数。

    R> colnames(mktdata)
     [1] "SPY.Open"     "SPY.High"     "SPY.Low"      "SPY.Close"    "SPY.Volume"  
     [6] "SPY.Adjusted" "SMA.SMA50"    "X1.ROC150"    "X1.TSI"       "RSI"
    

    解决方法是在函数定义中的TSI函数的输出上设置列名:

    TSI <- function(x, nShort = 10, nLong = 100)
    {
        atr <- ATR(HLC(x), nShort)$atr
        absd <- abs(diff(Cl(x), nShort))
        ratio <- absd / atr
        tsi <- runMean(runMean(ratio, nShort), nLong)
        colnames(tsi) <- "TSI"
        tsi
    }
    

    【讨论】:

    • 当我将 TSI 函数替换为您建议的更改 colnames 的函数时,我仍然遇到相同的错误。我在网上搜索过这个问题:r-forge.r-project.org/tracker/…
    • 这个链接建议了一个替代的 sigFormula 函数,它解决了我其他系统的代码相同的问题,但它不能解决我遇到的这个问题。
    • 我不知道该告诉你什么。当我使用来自 R-Forge 的最新 quantstrat 和 TTR 并确保公式变量与 mktdata colnames 匹配时,您的代码会为我运行。
    • 我正在使用吸墨纸版本 = 0.8.19,quanstrat = '0.8.2',金融工具 = '1.1.9',R 版本 = 2.15.3
    • 当你说公式变量应该匹配 mktdata 对象时,我发现每当我运行 applyIndicator 时,mktdata 的列名总是连接额外的符号,在你上面的例子中 x1;这会影响问题吗?
    【解决方案2】:

    定义你自己的 RSI

    RSII <- function(price, n)
    {
      rsi <- RSI(price=price, n=n)
      colnames(rsi) <- "RSII"
      rsi
    }
    

    将其余代码中的所有 RSI 替换为 RSII

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多