1

I'm admittedly a beginner in R and financial analysis as a whole. To better myself I've been working on a side project to automate a "Trending Value" screener. Essentially you pull 6 financial metrics - P/E, P/B, P/FCF, P/S, EV/EBITDA, and Shareholder Yield, which is dividend yield + stock buyback yield (i.e. equity returned to the shareholder in some way). There's more to it here if you'd like to know more but pulling metrics is the first part.

The code I've put together based on other posts has pulled the tickers P/E, P/B, & P/S. So I'm missing Market Cap, P/FCF, EV/EBITDA, and Shareholder Yield. Here's what I have thus far:

library(quantmod)  # also loads xts and TTR
require(plyr)

# Fetch all Symbols & store only the tickers to retrieve the data
symbols <- stockSymbols()

# Convert to just tickers.
tickers <- symbols[,1]

# Pull P/E, P/B, P/S
what_metrics <- yahooQF(c( "P/E Ratio",
                           "Price/Book",
                           "Price/Sales"))

# Not all the metrics are returned by Yahoo.
metrics <- getQuote(paste(tickers, sep="", collapse=";"), what=what_metrics)

#Add tickers as the first column and remove the first column which had date stamps
metrics <- data.frame(Symbol=tickers, metrics[,2:length(metrics)]) 

#Change colnames
colnames(metrics) <- c("Symbol","P/E","P/B","Price/Sales")

#Persist this to the csv file
write.csv(metrics, "FinancialMetrics.csv", row.names=FALSE)

Any help would be appreciated!

shayaa
  • 2,787
  • 13
  • 19
  • Welcome to stack overflow. You should use a minimal dataset when asking a question whereas you have fetched data from all the stocks. Also, your question is very broad and liable to be closed if you don't narrow it. What sort of trend value analysis do you want help computing and what might that look like? – shayaa Nov 05 '16 at 00:55
  • Thanks! So essentially, similar to how I am pulling the P/E, P/B, & P/S per each ticker thus far, I was aiming to add columns of data to the dataset (Price/Free Cash Flow, Enterprise Value/EBITDA, Market Capitalization, and Shareholder Yield). Unfortunately I can't access that information from the yahooQF feature so was hoping there might be another method or package which I'm not currently aware of. Sorry if this is still vague! – Shane Conner Nov 05 '16 at 02:33

1 Answers1

0

This will do what you want.

library(XML)

stocks <- c("AXP","BA","CAT","CSCO")

for (s in stocks) {
      url <- paste0("http://finviz.com/quote.ashx?t=", s)
      webpage <- readLines(url)
      html <- htmlTreeParse(webpage, useInternalNodes = TRUE, asText = TRUE)
      tableNodes <- getNodeSet(html, "//table")

      # ASSIGN TO STOCK NAMED DFS
      assign(s, readHTMLTable(tableNodes[[9]], 
                header= c("data1", "data2", "data3", "data4", "data5", "data6",
                          "data7", "data8", "data9", "data10", "data11", "data12")))

      # ADD COLUMN TO IDENTIFY STOCK 
      df <- get(s)
      df['stock'] <- s
      assign(s, df)
}

# COMBINE ALL STOCK DATA 
stockdatalist <- cbind(mget(stocks))
stockdata <- do.call(rbind, stockdatalist)
# MOVE STOCK ID TO FIRST COLUMN
stockdata <- stockdata[, c(ncol(stockdata), 1:ncol(stockdata)-1)]

# SAVE TO CSV
write.table(stockdata, "C:/Users/your_path_here/Desktop/MyData.csv", sep=",", 
            row.names=FALSE, col.names=FALSE)

# REMOVE TEMP OBJECTS
rm(df, stockdatalist)
  • Thank you very much! Though unfortunately I don't think EV/EBITDA & buyback yield (part of the equation for Shareholder's Yield) is on Finviz. That information is on Yahoo though I'm having trouble pulling that data from there as well. – Shane Conner Nov 13 '16 at 04:34