我在尝试在用符号“T”引用的股票 AT&T 上运行 quantstrat 示例时遇到问题。我相信这是因为 R 在某个地方认为这个 T 指的是 TRUE。这是我的代码:
library(quantstrat)
ticker="T"
total_hist.start = as.Date("2006-06-22")
total_hist.end = as.Date("2008-06-20")
total_hist = total_hist.end - total_hist.start
currency("USD")
stock(ticker,currency="USD",multiplier=1)
getSymbols(ticker,from=total_hist.start,to=total_hist.end,to.assign=TRUE)
init.date = initDate=total_hist.start-1
strat.name<- "MyStrat"
port.name <- "MyPort"
acct.name <- "MyAcct"
TradeSize = 1000
initEq=as.numeric( TradeSize*max(Ad(get(ticker)) ) )
port <- initPortf(port.name,ticker,initDate=init.date)
acct <- initAcct(acct.name,portfolios=port.name, initDate=init.date, initEq=initEq)
ords <- initOrders(portfolio=port.name,initDate=init.date)
strat<- strategy(strat.name)
strat<- add.indicator(strategy = strat, name = "SMA", arguments = list(x=quote(Ad(mktdata)), n=20),label= "ma20" )
strat<- add.indicator(strategy = strat, name = "SMA", arguments = list(x=quote(Ad(mktdata)), n=50),label= "ma50")
strat<- add.signal(strat,name="sigCrossover",arguments =
list(columns=c("ma20","ma50"),relationship="gte"),label="ma20.gt.ma50")
strat<- add.signal(strat,name="sigCrossover",arguments =
list(column=c("ma20","ma50"),relationship="lt"),label="ma20.lt.ma50")
strat<- add.rule(strategy = strat,name='ruleSignal', arguments = list(sigcol="ma20.gt.ma50",sigval=TRUE,
orderqty=TradeSize, ordertype='market', orderside='long', pricemethod='market'),type='enter', path.dep=TRUE)
strat<- add.rule(strategy = strat,name='ruleSignal', arguments = list(sigcol="ma20.lt.ma50",sigval=TRUE, orderqty='all',
ordertype='market', orderside='long', pricemethod='market'),type='exit', path.dep=TRUE)
out<-try(applyStrategy(strategy=strat, portfolios=port.name))
我现在收到此错误消息:
Error in mktdata[, keep] : nombre de dimensions incorrect
我尝试了另一只股票,如安捷伦科技,其符号为“A”,但我没有收到此错误,所以我几乎可以肯定问题出在 T 与 TRUE 一样的事实。谢谢您的帮助!