I have a problem when trying to run an example of quantstrat on the stock AT&T referred with the symbole "T". I believe it is because R is somewhere thinking this T is refering to TRUE. Here is my code:
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))
I now get this error message:
Error in mktdata[, keep] : nombre de dimensions incorrect
I tried with another stock like Agilent Technologies who's symbol is "A" and I don't get this error so I am almost sure the problem is coming from the fact T is like TRUE. Thanks for the help!
Your problem isn't in quantstrat, but in 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
one workaround would be to do something like this instead:
stock('ATT',currency='USD')
ticker<-'ATT'
ATT<-getSymbols('T',from=total_hist.start,to=total_hist.end,auto.assign=FALSE)
this would avoid any T/F confusion in R with TRUE/FALSE (which were always a horrible idea, imo).
Regards,