joshuaulrich/quantmod

Add ability for `getSymbols.yahoo()` to import from JSON endpoint

Closed this issue · 2 comments

There seems to be a rate limit for the number of tickers you can request via the CSV endpoint. The yfinance python library uses the JSON endpoint and doesn't seem to have this rate limit (cf #358 (comment)).

Try to use the other endpoint when there's an error from the current endpoint.

Here's an example that uses the JSON endpoint:

ticker <- "AAPL" 
 
.dateToUNIX <- function(Date) { 
  posixct <- as.POSIXct(as.Date(Date, origin = "1970-01-01")) 
  trunc(as.numeric(posixct)) 
} 
 
url <- paste0("https://query2.finance.yahoo.com/v8/finance/chart/", 
    ticker, 
    "?period1=", .dateToUNIX("2022-01-01"), 
    "&period2=", .dateToUNIX("2022-02-01"), 
    "&interval=1d") 
     
raw_result <- curl::curl_fetch_memory(url)
result <- jsonlite::fromJSON(rawToChar(raw_result$content))$chart$result

series <- unlist(result$indicators$quote[[1]], recursive = FALSE)
ohlcv <- xts(do.call(cbind, series), .POSIXct(result$timestamp[[1]]))

Here's an example using the JSON endpoint:

quantmod::getSymbols("SPY", use.json.api = TRUE)

@msperlin has an example that pulls 10 years of S&P 500 stocks using 14 cores without hitting a rate limit: #360 (comment)

Hi Josh,

No problem downloading data (no limitations downloading last 6 years of daily stocks price) but I have a problem maybe is my fault, when I tried to save the xts object using your code, it saves the character not the xts object.

idx <- c("MSFT", "GOO")
for (i in idx) {
suppressWarnings(temp <- quantmod::getSymbols(i, use.json.api = TRUE))}

So, temp = "GOO" which is the latest value but not the xts object with "GOO" stock data.

any idea why this behavior?

> sessionInfo()
R version 4.1.3 (2022-03-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 22000)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] clipr_0.8.0                tidyquant_1.0.3            PerformanceAnalytics_2.0.4
 [4] lubridate_1.8.0            quantmod_0.4.20            TTR_0.24.3                
 [7] xts_0.12.1                 zoo_1.8-10                 httr_1.4.2                
[10] jsonlite_1.8.0             forcats_0.5.1              stringr_1.4.0             
[13] dplyr_1.0.9                purrr_0.3.4                readr_2.1.2               
[16] tidyr_1.2.0                tibble_3.1.6               ggplot2_3.3.5             
[19] tidyverse_1.3.1

Thanks in advance

P.D.: Congratulations for the huge job you do.