3

我在尝试在用符号“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 一样的事实。谢谢您的帮助!

4

1 回答 1

3

您的问题不在于quantstrat,而在于getSymbols

> head(T)
[1] TRUE
> get('T')
[1] TRUE
> getSymbols(T,from=total_hist.start,to=total_hist.end,to.assign=TRUE)
Error in do.call(paste("getSymbols.", symbol.source, sep = ""), 
list(Symbols = current.symbols, : could not find function "getSymbols.TRUE"
> getSymbols('T',from=total_hist.start,to=total_hist.end,to.assign=TRUE)
[1] "T"
> head(T)
           T.Open T.High T.Low T.Close T.Volume T.Adjusted
2006-06-22  27.34  27.44 27.13   27.29 14123800      19.85
2006-06-23  27.15  27.61 27.05   27.37 10474500      19.91
2006-06-26  27.32  27.53 27.19   27.33 11311200      19.88
2006-06-27  27.38  27.49 27.29   27.35  9869100      19.89
2006-06-28  27.27  27.44 27.24   27.41 14853300      19.94
2006-06-29  27.42  27.79 27.42   27.70 17314300      20.15

一种解决方法是改为执行以下操作:

stock('ATT',currency='USD')
ticker<-'ATT'
ATT<-getSymbols('T',from=total_hist.start,to=total_hist.end,auto.assign=FALSE)

这将避免 R 中的任何 T/F 与 TRUE/FALSE 混淆(这总是一个可怕的想法,imo)。

问候,

  • 布赖恩
于 2012-06-29T12:02:24.400 回答