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
-
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)0
Answers
-
Sorry for the delay, it does solve my problem with the "raw_output = TRUE" option! Thanks a lot!
0 -
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,
0
Categories
- All Categories
- 3 Polls
- 6 AHS
- 36 Alpha
- 166 App Studio
- 6 Block Chain
- 4 Bot Platform
- 18 Connected Risk APIs
- 47 Data Fusion
- 34 Data Model Discovery
- 685 Datastream
- 1.4K DSS
- 616 Eikon COM
- 5.2K Eikon Data APIs
- 10 Electronic Trading
- Generic FIX
- 7 Local Bank Node API
- 3 Trading API
- 2.9K Elektron
- 1.4K EMA
- 252 ETA
- 556 WebSocket API
- 38 FX Venues
- 14 FX Market Data
- 1 FX Post Trade
- 1 FX Trading - Matching
- 12 FX Trading – RFQ Maker
- 5 Intelligent Tagging
- 2 Legal One
- 23 Messenger Bot
- 3 Messenger Side by Side
- 9 ONESOURCE
- 7 Indirect Tax
- 60 Open Calais
- 275 Open PermID
- 44 Entity Search
- 2 Org ID
- 1 PAM
- PAM - Logging
- 6 Product Insight
- Project Tracking
- ProView
- ProView Internal
- 22 RDMS
- 1.9K Refinitiv Data Platform
- 652 Refinitiv Data Platform Libraries
- 4 LSEG Due Diligence
- LSEG Due Diligence Portal API
- 4 Refinitiv Due Dilligence Centre
- Rose's Space
- 1.2K Screening
- 18 Qual-ID API
- 13 Screening Deployed
- 23 Screening Online
- 12 World-Check Customer Risk Screener
- 1K World-Check One
- 46 World-Check One Zero Footprint
- 45 Side by Side Integration API
- 2 Test Space
- 3 Thomson One Smart
- 10 TR Knowledge Graph
- 151 Transactions
- 143 REDI API
- 1.8K TREP APIs
- 4 CAT
- 27 DACS Station
- 121 Open DACS
- 1.1K RFA
- 104 UPA
- 193 TREP Infrastructure
- 228 TRKD
- 917 TRTH
- 5 Velocity Analytics
- 9 Wealth Management Web Services
- 90 Workspace SDK
- 11 Element Framework
- 5 Grid
- 18 World-Check Data File
- 1 Yield Book Analytics
- 46 中文论坛