2012-11-24 44 views
5

我是R的新手,在這裏卡住了。我試圖用價格,sma和ema繪製圖表。當從函數調用時,addSMA未畫在圖上

當我打電話從中汲取精緻的命令行包括價格,SMA和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() 

但付諸功能:

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

,並呼籲爲:

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

只畫出價格,但忽略了sma和ema線。

我在這裏做錯了什麼?

回答

8

包裹plot在你添加*通話。

我想你也可以只在lineChart呼叫添加這些代替。 (未經測試)

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

感謝GSEE ..劇情()工作過......解決我的問題... – user1848880

+0

@ user1848880,歡迎您。這實際上是[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)),所以你可以在那裏看到更多的細節。 – GSee

相關問題