の実行に関して問題があります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
またはで ether が発生するようapplyStrategy
です。どういう意味ですか?内部にバグはありsigFormula
ますか?
私のコード:
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)