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
- 6 AHS
- 39 Alpha
- 161 App Studio
- 4 Block Chain
- 4 Bot Platform
- 16 Connected Risk APIs
- 47 Data Fusion
- 30 Data Model Discovery
- 608 Datastream
- 1.3K DSS
- 577 Eikon COM
- 4.9K Eikon Data APIs
- 7 Electronic Trading
- Generic FIX
- 7 Local Bank Node API
- Trading API
- 2.7K Elektron
- 1.3K EMA
- 236 ETA
- 519 WebSocket API
- 33 FX Venues
- 10 FX Market Data
- 1 FX Post Trade
- 1 FX Trading - Matching
- 12 FX Trading – RFQ Maker
- 5 Intelligent Tagging
- 2 Legal One
- 20 Messenger Bot
- 2 Messenger Side by Side
- 9 ONESOURCE
- 7 Indirect Tax
- 59 Open Calais
- 264 Open PermID
- 39 Entity Search
- 2 Org ID
- PAM
- PAM - Logging
- 8.4K Private Comments
- 6 Product Insight
- Project Tracking
- ProView
- ProView Internal
- 20 RDMS
- 1.4K Refinitiv Data Platform
- 367 Refinitiv Data Platform Libraries
- 3 Refinitiv Due Diligence
- LSEG Due Diligence Portal API
- 3 Refinitiv Due Dilligence Centre
- Rose's Space
- 1.1K Screening
- 18 Qual-ID API
- 13 Screening Deployed
- 23 Screening Online
- 10 World-Check Customer Risk Screener
- 990 World-Check One
- 44 World-Check One Zero Footprint
- 45 Side by Side Integration API
- Test Space
- 3 Thomson One Smart
- 1.2K TR Internal
- Global Hackathon 2015
- 2 Specialists Who Code
- 10 TR Knowledge Graph
- 150 Transactions
- 142 REDI API
- 1.7K TREP APIs
- 4 CAT
- 21 DACS Station
- 117 Open DACS
- 1.1K RFA
- 103 UPA
- 172 TREP Infrastructure
- 224 TRKD
- 886 TRTH
- 5 Velocity Analytics
- 5 Wealth Management Web Services
- 59 Workspace SDK
- 9 Element Framework
- 5 Grid
- 13 World-Check Data File
- Yield Book Analytics
- 46 中文论坛