3

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!

Brian G. Peterson
  • 3,531
  • 2
  • 20
  • 21
jeremy.staub
  • 369
  • 4
  • 12
  • Which package are you using to give you the `currency`, `stock` functions etc? Or at which function call are you getting the error message? My guess is your call `get(ticker)` in `initEq=as.numeric(...)`, which is saying "get me the value of the variable `T`", being `true`. But without further details it is hard to diagnose. – mathematical.coffee Jun 29 '12 at 05:01
  • To add to @mathematical.coffee 's comment: what is `mktdata` ? It's not defined in the code you show. The answer might be as simple as `ticker<-as.character(ticker)` – Carl Witthoft Jun 29 '12 at 11:39
  • You'll get the same issue when you pass 'F' (Ford) to the ticker variable. – Milktrader Jun 29 '12 at 12:33
  • @Carl-whitthoft: as described in the documentation 'mktdata' is an internal object created inside the strategy execution code. – Brian G. Peterson Jun 29 '12 at 13:08

1 Answers1

3

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,

  • Brian
Brian G. Peterson
  • 3,531
  • 2
  • 20
  • 21