addSMA no se dibuja en el gráfico cuando se llama desde la función

Soy un novato para R y me he quedado atascado aquí. Estoy tratando de dibujar una gráfica con precio, sma y ema.

Cuando llamo al gráfico desde la línea de comando, se dibuja bien, incluido el precio, sma y ema:

tickers = c("BIIB","ISRG","AIG","FITB","GE","JNY","VIAB","WFM","WMB")

x= 1

print(paste("Preparing ADX graph for :",paste(tickers[x])))
tmp <- read.csv(paste(tickers[x],".csv", sep=""),as.is=TRUE, header=TRUE, row.names=NULL) 
tmp$Date<-as.Date(tmp$Date)
ydat = xts(tmp[,-1],tmp$Date) 
names(ydat) <- c("Open","High","Low","Close","Volume","Adjusted")

# convert it into montly price
ydat.monthly <- to.monthly(ydat)

jpegname <- paste(tickers[x], "MonthlyMovingAverage.jpeg", sep="") 
jpeg( filename=jpegname,height=600, width=1600) 

lineChart(ydat.monthly["1998/"], TA=NULL, name=paste(tickers[x],"Monthly & 10 Month Moving Average"))
addSMA(10)
addEMA(10)

dev.off()

Pero poner en función como:

MovingMonthlyAverageGraph <- function(tickers)
{

    source("code.r")
    load.packages('quantmod')   

    for (x in 1:(length(tickers)) ) 
    { 
       print(paste("Preparing ADX graph for :",paste(tickers[x])))
       tmp <- read.csv(paste(tickers[x],".csv", sep=""),as.is=TRUE, header=TRUE, row.names=NULL) 
       tmp$Date<-as.Date(tmp$Date)
       ydat = xts(tmp[,-1],tmp$Date) 
       names(ydat) <- c("Open","High","Low","Close","Volume","Adjusted")

       # convert it into montly price
       ydat.monthly <- to.monthly(ydat)

       jpegname <- paste(tickers[x], "MonthlyMovingAverage.jpeg", sep="") 
       jpeg( filename=jpegname,height=600, width=1600) 

       lineChart(ydat.monthly["1998/"], TA=NULL, name=paste(tickers[x],"Monthly & 10 Month Moving Average"))
       addSMA(10)
       addEMA(10)

       dev.off()
    }
}

y llamado como:

tickers = c("BIIB","ISRG","AIG","FITB","GE","JNY","VIAB","WFM","WMB")
MovingMonthlyAverageGraph(tickers)

solo dibuja el precio, pero ignora las líneas sma y ema.

¿Qué estoy haciendo mal aquí?

Respuestas a la pregunta(1)

Su respuesta a la pregunta