我是 R 的新手并且被困在这里。我正在尝试用价格、sma 和 ema 绘制图表。
当我从命令行调用图表时,它绘制得很好,包括价格、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 线。
我在这里做错了什么?