Last active
August 29, 2015 14:22
-
-
Save pengelbrecht/ddc51174c5b4db275787 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(rvest) | |
library(ggvis) | |
trim <- function (x) gsub("^\\s+|\\s+$", "", x) | |
to_number <- function(string) { | |
string <- trim(string) | |
len <- nchar(string) | |
last_char <-substr(string, len, len) | |
suppressWarnings( | |
if (last_char == "B") { | |
number <- as.numeric(substr(string, 1, len - 1)) * 1E9 | |
} else if (last_char == "M") { | |
number <- as.numeric(substr(string, 1, len - 1)) * 1E6 | |
} else if (last_char == "K") { | |
number <- as.numeric(substr(string, 1, len - 1)) * 1E3 | |
} else if (last_char == "%") { | |
number <- as.numeric(substr(string, 1, len - 1)) / 100 | |
} else | |
number <- as.numeric(string) | |
) | |
return(number) | |
} | |
symbol_key_stats <- function(symbol) { | |
url <- paste0( | |
"http://finance.yahoo.com/q/ks?s=", | |
symbol, | |
"+Key+Statistics" | |
) | |
page <- html(url) | |
tables <- page %>% html_nodes("table") | |
stats <- tables[[9]] %>% html_table() | |
for(i in c(12, 14, 16, 18, 20, 22)) { | |
table <- tables[[i]] %>% html_table() | |
table <- table[-1,] | |
stats <- rbind(stats, table) | |
} | |
df <- data.frame( | |
symbol = symbol, | |
market_cap = to_number(stats[1,2]), | |
ps = to_number(stats[6,2]), | |
ev = to_number(stats[2,2]), | |
ev_rev = to_number(stats[8,2]), | |
ev_ebitda = to_number(stats[9,2]), | |
profit_margin = to_number(stats[12,2]), | |
operating_margin = to_number(stats[13,2]), | |
revenue = to_number(stats[16,2]), | |
revenue_growth = to_number(stats[18,2]), | |
earnings_growth = to_number(stats[23,2]), | |
gross_profit = to_number(stats[19,2]) | |
) | |
return(df) | |
} | |
category_stats <- function(symbols, category_name) { | |
n = length(symbols) | |
stats = symbol_key_stats(symbols[1]) | |
stats$category = category_name | |
if(n > 1) { | |
for(i in 2:n) { | |
line = symbol_key_stats(symbols[i]) | |
line$category = category_name | |
stats = rbind(stats, line) | |
} | |
} | |
return(stats) | |
} | |
baskets <- function() { | |
saas_symbols = c("CRM", "HUBS", "ZEN", "SHOP", "BOX") | |
cloud_telco_symbols = c("RNG", "FIVN", "EGHT", "JCOM") | |
telco_symbols = c("T", "VZ", "TMUS", "S", "TDC.CO", "VOD") | |
telco_software_symbols = c("DOX", "CNSI", "CVG","BSFT") | |
saas_data = category_stats(saas_symbols, "SaaS") | |
cloud_telco_data = category_stats(cloud_telco_symbols, "Cloud Telco") | |
telco_data = category_stats(telco_symbols, "Telco") | |
telco_software_data = category_stats(telco_software_symbols, "Telco Software") | |
baskets = rbind(saas_data, cloud_telco_data, telco_data, telco_software_data) | |
return(baskets) | |
} | |
stocks = baskets() | |
plot = stocks %>% ggvis(x = ~revenue_growth, y = ~ev_rev) %>% | |
group_by(category) %>% | |
layer_text(text:=~symbol, dx:=-5, dy:=-5, stroke = ~category) %>% | |
layer_model_predictions(model = "lm", stroke = ~category) %>% | |
layer_points(fill = ~category) %>% | |
add_axis("x", title = "Revenue Growth", format ="%") %>% | |
add_axis("y", title = "Revenue Multiple (EV/Revn)") | |
print(plot) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment