我正在尝试将 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"))