0

I'm a bit puzzled. I have a created a custom function:

doGraph <- function(x){
dev.new();
symb <- getSymbols(x, auto.assign = FALSE);
chartSeries(symb, subset = 'last 3 months', name=x);
addBBands();
addMACD();
}
doGraph("AAPL")

The above code does not add the Bollinger Bands, as I would expect (by the addBBands() call). However, if I remove the addMACD() call, the bands are added as expected. Additionally, if I type addBBands() after the function call, the bands are added. Does anyone have any thoughts on what could be going wrong here? Thanks!

GSee
  • 48,880
  • 13
  • 125
  • 145

1 Answers1

1

Wrap plot around your add* calls

doGraph <- function(x){
  dev.new()
  symb <- getSymbols(x, auto.assign = FALSE)
  chartSeries(symb, subset = 'last 3 months', name=x)
  plot(addBBands())
  plot(addMACD())
}
doGraph("AAPL")
GSee
  • 48,880
  • 13
  • 125
  • 145
  • Or, better yet, put them in the `chartSeries` call: `chartSeries(symb, subset='last 3 months', name=x, TA="addBBands();addMACD()")`. – Joshua Ulrich Aug 20 '13 at 18:47
  • Thanks Guys -- both solutions work. However, I'd like to understand why this happens? If I put both calls in the function, `addBBands()` gets skipped. However, if I call `addBBands()` afterwards, it appears with no issues. Is there an underlying restriction that I'm missing here? Any input appreciated! – Mark Cukier Aug 20 '13 at 19:36
  • 1
    @MarkCukier did you click the links in my comment on my answer to the duplicate post? – GSee Aug 20 '13 at 19:43
  • @GSee -- no, I didn't even notice that there was a comment on the original question. I'm now reading R-SIG-Finance history... thanks for the links! – Mark Cukier Aug 20 '13 at 19:50