Search code examples
rquantmod

R appears to fail to execute a line in a function


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!


Solution

  • 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")