2012-11-24 11 views
5

Sono un novizio per R e sono rimasto bloccato qui. Sto cercando di disegnare un grafico con prezzo, sma ed ema.addSMA non disegnato sul grafico quando chiamato dalla funzione

Quando chiamo il grafico dalla riga di comando disegna bene compreso il prezzo, sma e 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() 

Ma messo in funzione di:

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() 
    } 
} 

e chiamato come:

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

disegna solo il prezzo, ma ignora le linee sma ed ema.

Cosa sto facendo di sbagliato qui?

risposta

8

avvolgere plot intorno alle tue * aggiungere chiamate.

penso che si potrebbe anche solo aggiungere questi nella chiamata lineChart invece. (non testata)

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

Grazie Gsee .. la trama() ha funzionato ... Risolto il mio problema ... – user1848880

+0

@ user1848880, prego. Questa è in realtà una domanda comune sull'elenco [r-sig-finance] (https://stat.ethz.ch/mailman/listinfo/r-sig-finance) ([1] (https: //stat.ethz. ch/pipermail/r-sig-finance/2009q2/004018.html), [2] (https://stat.ethz.ch/pipermail/r-sig-finance/2009q1/003828.html), [3] (https://stat.ethz.ch/pipermail/r-sig-finance/2012q2/009865.html)), quindi puoi guardare lì per maggiori dettagli. – GSee

Problemi correlati