8

I know some ways to get daily stock prices and volumes in R or python, but just wondering whether these is a way (using either R or python) to get more info about stocks such as P/E ratio, company website, Yield and so on, preferably not just current value, but also historical values.

Thanks.

danioyuan
  • 1,268
  • 2
  • 13
  • 16
  • Since there haven't been any python answers, and you accepted an R-only answer, should we make this R-specific and remove the [python] tag? – GSee Jun 22 '12 at 15:12

2 Answers2

12

Historical is going to be difficult. The quantmod package for R has getQuote which together with yahooQF will be all you need to get current values.

require("quantmod")
getQuote("GS", what = yahooQF(c("Market Capitalization", "Earnings/Share", 
         "P/E Ratio", "Book Value", "EBITDA", "52-week Range")))

            Trade Time Market Capitalization Earnings/Share P/E Ratio Book Value EBITDA  52-week Range
GS 2012-06-21 04:00:00               47.870B          6.764     14.27    134.476      0 84.27 - 139.25

Also, try

getQuote("GS", what=yahooQF())

which will give you a menu of choices for what fields to request.

You can get recent financial statements from Google Finance with getFinancials

There is also the FinancialInstrument package which has several update_instruments.* functions to download metadata about instruments (stocks in this case). For example, here's what the yahoo one does

require("FinancialInstrument")
stock("GS", currency("USD")) # define the stock
#[1] "GS"
update_instruments.yahoo("GS") #update with yahoo
#[1] "GS"
getInstrument("GS")
#primary_id          :"GS"
#currency            :"USD"
#multiplier          :1
#tick_size           :0.01
#identifiers         : list()
#type                :"stock"
#name                :"Goldman Sachs Gro"
#exchange            :"NYSE"
#market.cap          :"47.870B"
#avg.volume          :5480530
#EPS                 :6.76
#EPS.current.year.est:11.4
#EPS.next.year.est   :12.9
#book.value          :134
#EBITDA              :0
#range.52wk          :"84.27 - 139.25"
#defined.by          :"yahoo"
#updated             : POSIXct, format: "2012-06-21 19:31:11"

If you have an InteractiveBrokers account, you can use the outstanding IBrokers package to get lots of info about lots of instruments. Also, if you have an IB account you'll want to look at my twsInstrument package which has a lot of convenience functions.

GSee
  • 48,880
  • 13
  • 125
  • 145
  • 1
    the oft-cited [gummy-stuff.org](http://www.gummy-stuff.org/Yahoo-data.htm) is a wonderful resource for info about downloading data from yahoo. – GSee Jun 22 '12 at 01:06
  • One thing in my question that didn't get solved by these commands is the website of the company. Is there a way to get that automatically? – danioyuan Jun 22 '12 at 06:24
  • 1
    quantmod also has `getDividends`(historic dividends), `getSplits` (historic splits), `getSymbols` (historic stock prices), and `getOptionChain` (snapshot quote option chain) – GSee Jun 22 '12 at 06:29
  • Hi GSee, it seems that you are quite experienced with quantmod and handling financial data. Do you mind sharing how you use them and maybe some insights? Wanted to message you but not sure how to do that in stackoverflow. Thanks. – danioyuan Jun 22 '12 at 22:55
  • You can find my e-mail is in the DESCRIPTION file of some of the packages I linked to above (qmao, twsInstrument, or FinancialInstrument). Have you seen [quantmod.com](http://www.quantmod.com/)? (particularly the examples section). – GSee Jun 22 '12 at 23:05
2

Just to answer the website part of my question:

  str <- paste("http://investing.money.msn.com/investments/company-report?symbol=", ticker, sep = "")
  page <- paste(readLines(url(str, open = "rt")), collapse = "\n")
  match <- regexpr("<a href=\"http://www\\.(\\S+)\">Website</a>", page, perl = TRUE)

  if (attr(match, "match.length") > 0) {
    site <- substring(page, attr(match, "capture.start"), attr(match, "capture.start") + attr(match, "capture.length") - 1)    
    site <- strsplit(site, "/")[[1]][1]
  }
danioyuan
  • 1,268
  • 2
  • 13
  • 16