# FDIC API ---------------------------------------------------------------- # Pull financial information from the FDIC API using package: fdicdata # Use ?fdicdata or check online pdf documentation for functionality # Use dataTaxonomy() and search function to explore available information # The script relies on the getFinancials() function within fdicdata and # creates a custom function getFinancialsForAllBanks() to retrieve multiple # variables for multiple banks across a chosen time frame # # Author: Wade Litt # Imports ----------------------------------------------------------------- install.packages("fdicdata") library(fdicdata) library(dplyr) # Available Data taxonomy <- dataTaxonomy("financial") taxonomy <- dataTaxonomy("institution") # # Check a single call to ensure the API is responding correctly; example: # getFinancials(CERT, desired_variables, # records to retrieve, IDRSSD = FALSE, time_range) getFinancials(3510, c("DRLNLS"), limit=10, FALSE, range = c("2015-01-01","2022-01-01") ) # Bank Selection ---------------------------------------------------------- # Choose population of banks to work with # Add NAME== or CERT== or IDRSSD== to filter if one specific bank is desired # Search for specific banks at very bottom of script df_banks_all <- getInstitutionsAll() df_banks_selection <- df_banks_all %>% rename(IDRSSD = FED_RSSD) %>% filter(ACTIVE==1, # Only active banks ASSET > 1000000000) %>% # Asset filter (in actual dollars) select(NAME, IDRSSD, CERT, ASSET, DATEUPDT, everything()) # reorder columns bank_ids <- df_banks_selection$CERT # create secondary list of banks to join with financial data later df_banks <- df_banks_selection %>% select(NAME, CITY, STNAME, IDRSSD, CERT) # Gather Financial Data --------------------------------------------------- # Create function to gather financial data from all banks selected above getFinancialsForAllBanks <- function(bank_ids, metrics, start_date, end_date, limit = 10000) { all_data <- data.frame() # Initialize an empty data frame to store results total_banks <- length(bank_ids) message(paste("Fetching data... it actually can takes few mins")) for (i in seq_along(bank_ids)) { bank_id <- bank_ids[i] tryCatch({ # Fetch financial data for the given range, specified below temp_data <- getFinancials(bank_id, metrics = metrics, limit = limit, IDRSSD = FALSE, range = c(start_date, end_date)) # Bind this bank's data to the overall dataframe all_data <- rbind(all_data, temp_data) }, error = function(e) { message(paste("Error fetching data for bank ID", bank_id, ":", e$message)) }) } return(all_data) } # !!!!!!! MAIN SELECTION PARAMETERS !!!!!!!!! # Define metrics and date range metrics <- c("DRLNLS", "ASSET", "NETINC") # Defaulted to DRLNLS: Total Loan and Lease Charge-Offs (i.e., loan losses) start_date <- "2019-01-01" end_date <- "*" # Specify an end date or use "*" to run until most recent data # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! # Fetch financial data for all banks (using the function created above and parameters # specified directly above) df_bank_data <- getFinancialsForAllBanks(bank_ids, metrics, start_date, end_date) %>% left_join(df_banks) %>% select(IDRSSD, CERT, NAME, CITY, STNAME, DATE, everything()) # Extras ------------------------------------------------------------------ # Search for a Specific Bank ---------------------------------------------- searchBankByName <- function(data, search_term) { # Convert search term to lowercase to make the search case-insensitive search_term <- tolower(search_term) # Filter data where NAME contains the search_term, case-insensitively filtered_data <- data %>% filter(grepl(search_term, tolower(NAME))) # Use grepl for partial matching return(filtered_data) } # Search for banks with last parameter df_bank_search <- searchBankByName(df_banks_selection, "Park")