1
$\begingroup$

I am using the Quantmod package in R for some data analysis. Now I can downbload price history of particular stocks or index with the following code:-

library(quantmod) # Loading quantmod library

getSymbols("^DJI", from = as.character(Sys.Date()-365*3))

I want to download all the ticker symbols that are composite of a particular Index such as DJI for example. What will be the best way to do that through R?

Thanks a lot in advance.

$\endgroup$

1 Answer 1

0
$\begingroup$

You need the components. You could use yahoo or cnn site, and read the table from that. Then get the tickers. Read all to an environment and work with the data.

library(XML) 
urlt <- "http://money.cnn.com/data/dow30/" 
doc.html = htmlTreeParse(urlt, useInternal = TRUE)
tables <- readHTMLTable(doc.html,as.data.frame=FALSE)
length(tables)
tables[[2]]
tables <- readHTMLTable(doc.html,
                        stringsAsFactors=FALSE,which = 2)

ticker=sapply(1:length(tables$Company),function(xs)
  strsplit(rawToChar(charToRaw(text[xs])),"Â",fixed=TRUE)[[1]][1]
  )
#ticker <- as.vector(as.character(ticker))
library(quantmod)
StockData <- new.env()
data <- getSymbols(ticker, env = StockData)
do.call(merge, eapply(StockData, Cl)[ticker])
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.