Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
311 views
in Technique[技术] by (71.8m points)

r - Loop Through Stock Tickers and Creat List of all Charts

How can I loop through stock tickers, copy each to some kind of report, such as a PDF file, and save the report? The script below loops through a bunch of tickers, but it overwrites each previous chart with the next chart. I want to list the next chart under the previous chart, and list everything in a PDF, or Word document.

library(quantmod)
stocks <- c("FIS", "AXP", "AVB")
stockEnv <- new.env()
symbols <- getSymbols(stocks, src='yahoo', env=stockEnv)
for (stock in ls(stockEnv)){
    chartSeries(stockEnv[[stock]], theme="white", name=stock,
        TA="addVo();addBBands();addCCI();addSMA(20, col='blue');
        addSMA(5, col='red');addSMA(50, col='black')", subset='last 30 days')     
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Calling the pdf() function and turning the plotting device off after creating all charts using dev.off() worked for me:

library(quantmod)
stocks <- c("FIS", "AXP", "AVB")
stockEnv <- new.env()
symbols <- getSymbols(stocks, src='yahoo', env=stockEnv)

pdf('test.pdf')
for (stock in ls(stockEnv)){
  chartSeries(stockEnv[[stock]], theme="white", name=stock,
              TA="addVo();addBBands();addCCI();addSMA(20, col='blue');
        addSMA(5, col='red');addSMA(50, col='black')", subset='last 30 days')     
}
dev.off()

That code created a pdf document in my working directory called test.pdf. It had three plots, one plot per page.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...