alphavantager icon indicating copy to clipboard operation
alphavantager copied to clipboard

av_get returns data in the form of error when getting exchange rates

Open benilak opened this issue 5 years ago • 2 comments

EDIT: made a mistake, updated my comments

This is weird. This code should return a simple quote on the current rate of Bitcoin: av_get(symbol = NULL, av_fun = "CURRENCY_EXCHANGE_RATE", from_currency = "BTC", to_currency = "USD")

The API request is received without trouble and the data gets pulled as a json, instead of returning or converting to data frame, this code is executed

if (content_list[1] %>% names() == "Meta Data") { ... }
else {
      if (is.null(symbol)) {
        symbol <- "NULL"
      }
      params_list <- list(symbol = symbol, av_fun = av_fun)
      dots$symbol <- NULL
      params_list <- c(params_list, dots)
      params_list$apikey = "HIDDEN_FOR_YOUR_SAFETY"
      params_list$datatype = NULL
      params <- stringr::str_c(names(params_list), params_list, 
        sep = "=", collapse = ", ") %>% stringr::str_replace("av_fun", 
        "function")
      content <- content %>% jsonlite::fromJSON(flatten = T) %>% 
        stringr::str_c(". API parameters used: ", params)
      stop(content, call. = F)
    }

After determining the content isn't "Meta Data", the function decides to throw away the data as an error. Why? The data is already there, might as well return it as a list instead.

benilak avatar May 15 '19 00:05 benilak

After referencing the code in this repository (as opposed to debugging in RStudio), I see now that if (content_list[1] %>% names() != "Meta Data") then this is considered a "bad call". Maybe I misunderstand what that means, but in the case above I know the data was in fact retrieved via the API.

benilak avatar May 15 '19 01:05 benilak

I couldn't scrape a correct JSON file from the API. I did it manually and there seems to be an error in the formatting in the JSON. e.g.

{
    "Meta Data": { 
        "1. Information": "Forex Daily Prices (open, high, low, close)",
        "2. From Symbol": "GBP",
        "3. To Symbol": "USD",
        "4. Output Size": "Full size",
        "5. Last Refreshed": "2019-07-13 04:35:00",
        "6. Time Zone": "GMT+8"
    },
    "Time Series FX (Daily)": {
        "2019-07-13": {
            "1. open": "1.2552",
            "2. high": "1.2576",
            "3. low": "1.2550",
            "4. close": "1.2575"
        },
        "2019-07-12": {
            "1. open": "1.2528",
            "2. high": "1.2529",
            "3. low": "1.2518",
            "4. close": "1.2526"
        },

The

"Time Series FX (Daily)": {

should not be there as this messed up my columns when I loaded it in locally. Also, do remove a curly bracket at the end of the code in order get closure.

I made a function like this to load a json file called "USDJPY.json"

scraping_cur <- function(x){
  a <- jsonlite::fromJSON(paste0(x,".json"))
  a <- lapply(a,unlist)
  a <- do.call(rbind, a[-1])
  a <- as.xts(a)
  return(a)
}

LJPLux avatar Jul 12 '19 21:07 LJPLux