ASH
ASH

Reputation: 20322

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

Upvotes: 0

Views: 416

Answers (1)

AOGSTA
AOGSTA

Reputation: 708

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.

Upvotes: 3

Related Questions