ADX しきい値信号を macd デモに追加しようとしています。そうすると、次のエラーが発生します。私は traceback() をいじりましたが、何が問題なのか理解できません。任意のポインタをいただければ幸いです。ありがとう!
エラーは、次のコードの最後のコマンドによって生成されます。
エラー:
Error in inherits(x, "xts") : Argument "HLC" missing (without default value)
Additional: Warning:
In applyIndicators(strategy = strategy, mktdata = mktdata, parameters = parameters, : some arguments stored for ADX do not match
コード:
# Set initial Dates
initDate <- "2000-01-01"
endDate <- "2012-09-01"
# Set initial Equity
initEq <- 100000
# Pull Yahoo Finance data
symbols <- c("TFM")
getSymbols(symbols, from=initDate, to=endDate, index.class=c("POSIXt","POSIXct"))
# adjust for splits/dividends (comment to replicate blotter example)
TFM <- adjustOHLC(TFM, use.Adjusted=TRUE)
# set currency
for(symbol in symbols) {
stock(symbol, currency="USD", multiplier=1)
}
# Delete (1) portfolio, (2) account, and (3) order book if they already exist
suppressWarnings(rm("account.macd","portfolio.macd",pos=.blotter))
suppressWarnings(rm("order_book.macd",pos=.strategy))
# Initialize new/blank (1) portfolio, (2) account, and (3) order book
initPortf("macd", symbols=symbols, initDate=initDate)
initAcct("macd", portfolios="macd", initDate=initDate, initEq=initEq)
initOrders(portfolio="macd", initDate=initDate)
# Initialize a strategy object
stratMACD <- strategy("macd")
# Add the MACD indicator to the strategy
stratMACD <- add.indicator(strategy=stratMACD, name="MACD",arguments=list(x=quote(Cl(mktdata))))
# Add the ADX indicator to the strategy
stratMACD <- add.indicator(strategy=stratMACD, name="ADX", arguments=list(x=quote(HLC(mktdata))))
# There are two MACD crossover signals:
# (1) MACD crosses Signal from below
stratMACD <- add.signal(stratMACD, name="sigCrossover", arguments=list(columns=c("macd","signal"), relationship="gt"), label="MACD.gt.SIG")
# (2) MACD crosses Signal from above
stratMACD <- add.signal(stratMACD, name="sigCrossover", arguments=list(columns=c("macd","signal"), relationship="lt"), label="MACD.lt.SIG")
# There are two ADX threshold (30) signals
stratMACD <- add.signal(stratMACD, name="sigThreshold", arguments=list(column="ADX",relationship="gt",threshold=30,cross=TRUE), label="ADX.gt.30")
stratMACD <- add.signal(stratMACD, name="sigThreshold", arguments=list(column="ADX",relationship="lt",threshold=30,cross=TRUE), label="ADX.lt.30")
# There are two rules:
# (1) buy when MACD crosses above the SIGNAL -AND- ADX > 30
stratMACD <- add.rule(stratMACD, name="ruleSignal", arguments=list(sigcol="MACD.gt.SIG", sigval=TRUE, orderqty=1000, ordertype="market", orderside="long", pricemethod="market", TxnFees=-5, osFUN=osMaxPos), type="enter", path.dep=TRUE)
stratMACD <- add.rule(stratMACD, name="ruleSignal", arguments=list(sigcol="ADX.gt.30", sigval=TRUE, orderqty=1000, ordertype="market", orderside="long", pricemethod="market", TxnFees=-5, osFUN=osMaxPos), type="enter", path.dep=TRUE)
# (2) sell when MACD crosses below the SIGNAL -AND- ADX < 30
stratMACD <- add.rule(stratMACD, name="ruleSignal", arguments=list(sigcol="MACD.lt.SIG", sigval=TRUE, orderqty="all", ordertype="market", orderside="long", pricemethod="market", TxnFees=-5), type="exit", path.dep=TRUE)
stratMACD <- add.rule(stratMACD, name="ruleSignal", arguments=list(sigcol="ADX.lt.30", sigval=TRUE, orderqty="all", ordertype="market", orderside="long", pricemethod="market", TxnFees=-5), type="exit", path.dep=TRUE)
# Set position limits so we don't add to the position every month Close > SMA10
addPosLimit(portfolio="macd", symbol="TFM", timestamp=initDate, maxpos=1000, minpos=0)
# Process the indicators and generate trades
out <- try(applyStrategy(strategy=stratMACD, portfolios="macd"))