Skip to content

Commit

Permalink
Modification to tq_get to allow fetching key ratios from arbitrary ex…
Browse files Browse the repository at this point in the history
…changes.
  • Loading branch information
mwaldstein committed Jun 14, 2017
1 parent 4b9f4d6 commit 3812ae4
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions R/tq_get.R
Original file line number Diff line number Diff line change
Expand Up @@ -445,20 +445,30 @@ tq_get_util_2 <- function(x, get, complete_cases, map, ...) {

# Convert x to uppercase
x <- stringr::str_to_upper(x) %>%
stringr::str_trim(side = "both") %>%
stringr::str_replace_all("[[:punct:]]", "")
stringr::str_trim(side = "both")

# If the request has a ':', assume that it is in the form of EXCHANGE:SYMBOL
# Allows both forcing a specific exchange source and making requests from non-default exchanges
if ( stringr::str_detect(x,":") ) {
split_req <- stringr::str_split(x,":",2)
stock_exchange <- c(split_req[[1]][1])
x <- split_req[[1]][2]
} else {
stock_exchange <- c("XNAS", "XNYS", "XASE") # mornginstar gets from various exchanges
}

x <- stringr::str_replace_all(x,"[[:punct:]]", "")

tryCatch({

# Download file
stock_exchange <- c("XNAS", "XNYS", "XASE") # mornginstar gets from various exchanges
url_base_1 <- 'http://financials.morningstar.com/finan/ajax/exportKR2CSV.html?&callback=?&t='
url_base_2 <- '&region=usa&culture=en-US&cur=&order=asc'
# Three URLs to try
url <- paste0(url_base_1, stock_exchange, ":", x, url_base_2)

# Try various stock exchanges
for(i in 1:3) {
for(i in 1:length(url)) {
text <- httr::RETRY("GET", url[i], times = 5) %>%
httr::content()

Expand Down

0 comments on commit 3812ae4

Please sign in to comment.