1

我在跑步方面遇到问题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

它似乎出现在applySignal或中applyStrategy。这是什么意思?里面有bugsigFormula吗?

我的代码:

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

2 回答 2

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
}
于 2014-08-10T12:05:15.273 回答
0

定义你自己的 RSI

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

在其余代码中用 RSII 替换所有 RSI

于 2020-08-15T20:08:43.497 回答