Response time too long, Error Handling in R

R code:

library(eikonapir)

eikonapir::set_app_id("my-key")
time.start <- Sys.time()
tickers <- c("BRGV5YUSAC=R", "AAHR5YEUAM=R", "ANZ5YUSAR=R", "BBVA5YEUAM=R", "SAN5YEUAM=R", "BAC5YUSAX=R", "BKCH5YUSAC=R", "BNS5YUSAX=MP", "BARC5YEUAM=R", "BNPP5YEUAM=R", "CNDA5YUSAC=R", "C5YUSAX=R", "CBKG5YEUAM=R", "CBA5YUSAR=R", "RABO5YEUAM=R", "CAGR5YEUAM=R", "CSGN5YEUAM=R", "DB5YEUAM=R", "GS5YUSAX=R", "HBCA5YEUAM=R", "INGB5YEUAM=R", "BCIN5YEUAM=R", "JPM5YUSAX=R", "LLTS5YEUAM=R", "MBL5YUSAR=R", "MZFC5YUSAC=R", "MS5YUSAX=R", "MTFC5YUSAC=R", "CNAT5YEUAM=MG", "NDAA5YEUAM=MG", "FINA5YUSAC=MP", "SHIA5YUSAC=R", "SG5YEUAM=R", "STAN5YEUAM=R", "SUMA5YUSAC=R", "SHB5YEUAM=R", "UBSN5YEUAM=R", "UNIC5YEUAM=R", "UOBH5YUSAC=R", "WFC5YUSAX=R", "WBC5YUSAR=R", "WOOR5YUSAC=R", "OCBC5YUSAC=R", "ICBC5YUSAC=MG", "RBS5YEUAM=R")
df <- data.frame(Data = NA, CDS = NA, Ticker = NA)[c(-1),]
for (t in tickers) {
df_aux <- get_timeseries(rics = as.list(t),
fields = list("TIMESTAMP", "CLOSE"),
start_date = format(Sys.Date() - 7, "%Y-%m-%dT%H:%M:%SZ"),
end_date = format(Sys.Date(), "%Y-%m-%dT%H:%M:%SZ"),
interval = "daily")
if (ncol(df_aux) == 3) {
colnames(df_aux) <- c("Data", "CDS", "Ticker")
df <- rbind(df, df_aux)
}
}
time.end <- Sys.time()
(time.taken <- time.end - time.start)

Result: 21.6 minutes

So, my questions:

I) First time I run that script, I didn't mesure, but it took less than 4 minutes, but nowadays it's so slow.. Is there any reason for that long execution?

II) I have to use that if (ncol(df_aux) == 3) for treat no data or no access data since it return a 2 column dataframe instead of 3 on those cases. Is this the best practice?

III) How to error handling in R? How to get the error message in get_data and get_timeseries?


Best Answer

  • Jirapongse
    Answer ✓

    @ty

    The efficient way is getting raw data and then create a data frame from the raw data.

    You can send one request of all items if it doesn't exceed 3000 datapoints as mentioned in the EIKON DATA API USAGE AND LIMITS GUIDELINE.

    The code looks like:


    get_formatted_data_frame  <- function(data)
    {
      input_data_frame = data$timeseriesData
      data_frames <- list()
      for (i in 1:nrow(input_data_frame))
      { current_row = input_data_frame[i,]    
        if (is.na(current_row$errorCode)==FALSE) {
            next
        }   
        current_fields  = current_row$fields[[1]]$name
        ric_column = rep( current_row$ric,nrow(input_data_frame[i,]))
        data_frame  <- as.data.frame(current_row$dataPoints[[1]],stringsAsFactors = FALSE)   
        data_frame <- cbind(data_frame, Security=ric_column)
        names(data_frame) <- current_fields
        data_frames[[i]] = data_frame
      }
      return (do.call("rbind", data_frames))
    }

    raw <- get_timeseries(rics = list("BRGV5YUSAC=R", "AAHR5YEUAM=R", "ANZ5YUSAR=R", "BBVA5YEUAM=R", "SAN5YEUAM=R", "BAC5YUSAX=R", "BKCH5YUSAC=R", "BNS5YUSAX=MP", "BARC5YEUAM=R", "BNPP5YEUAM=R", "CNDA5YUSAC=R", "C5YUSAX=R", "CBKG5YEUAM=R", "CBA5YUSAR=R", "RABO5YEUAM=R", "CAGR5YEUAM=R", "CSGN5YEUAM=R", "DB5YEUAM=R", "GS5YUSAX=R", "HBCA5YEUAM=R", "INGB5YEUAM=R", "BCIN5YEUAM=R", "JPM5YUSAX=R", "LLTS5YEUAM=R", "MBL5YUSAR=R", "MZFC5YUSAC=R", "MS5YUSAX=R", "MTFC5YUSAC=R", "CNAT5YEUAM=MG", "NDAA5YEUAM=MG", "FINA5YUSAC=MP", "SHIA5YUSAC=R", "SG5YEUAM=R", "STAN5YEUAM=R", "SUMA5YUSAC=R", "SHB5YEUAM=R", "UBSN5YEUAM=R", "UNIC5YEUAM=R", "UOBH5YUSAC=R", "WFC5YUSAX=R", "WBC5YUSAR=R", "WOOR5YUSAC=R", "OCBC5YUSAC=R", "ICBC5YUSAC=MG", "RBS5YEUAM=R"),
                               fields = list("TIMESTAMP", "CLOSE"),
                               start_date = format(Sys.Date() - 7, "%Y-%m-%dT%H:%M:%SZ"),
                               end_date = format(Sys.Date(), "%Y-%m-%dT%H:%M:%SZ"),
                               interval = "daily",
                               raw_output = TRUE)
    data = jsonlite::fromJSON(raw)
    get_formatted_data_frame(data)

Answers

  • Sorry for the delay, it does solve my problem with the "raw_output = TRUE" option! Thanks a lot!

  • When you are writing big programs, sometimes something goes wrong with your R code. What do you do if the program stops unexpectedly? What tools do you have to address the problem? This is where the tryCatch() function will help you. Debugging is the art and science of fixing unexpected problems in your code.


    You can use the tryCatch() exception handling method to resolve most of your issues.


    Thanks,