I am trying to create data frames of the Balance Sheet, Income Statement, and Cash Flows of a list of stocks. However, some stocks do not have financial statements. Instead of deleting the stocks, I need to continue to loop through the stocks that have financial statements and create the three dataframes. I need to do this because I have over a thousand stocks. I have looked around the site. I have found that tryCatch
is a lot better in this scenerio versus try(expr, silent = TRUE)
.
When the code's "Stocklist" all have financial statements, the BS, IS, and CF dataframes contain multiple rows and columns in their data frame. However, the code below only has empty dataframes with 0 rows and 0 columns. Please help, thanks!
StockList <- c("AAIT", "AAL", "AAME", "AAOI", "AAON", "AAPC", "AAPL", "AAVL", "AAWW", "AAXJ")
for(i in 1:length(StockList)){
print(i)
get_fin<-tryCatch(lapply(StockList,
function(x)
getFinancials(x, auto.assign = FALSE)),
error=function(e) NULL)
if(!is.null(get_fin)){
BS <- data.frame(lapply(get_fin, function(x) {viewFinancials(x, type= 'BS', period = 'A')}))
IS <- data.frame(lapply(get_fin, function(x) {viewFinancials(x, type= 'IS', period = 'A')}))
CF <- data.frame(lapply(get_fin, function(x) {viewFinancials(x, type= 'CF', period = 'A')}))
}
else {
print("Error: NULL")
}
}
This is the output that I got, but I have my StockList as a character vector that was fetched from getSymbols:
[1] 1
[1] "Error: NULL"
[1] 2
Annual Balance Sheet for AAL
Annual Income Statement for AAL
Annual Cash Flow Statement for AAL
[1] 3
Annual Balance Sheet for AAME
Annual Income Statement for AAME
Annual Cash Flow Statement for AAME
[1] 4
Annual Balance Sheet for AAOI
Annual Income Statement for AAOI
Annual Cash Flow Statement for AAOI
[1] 5
Annual Balance Sheet for AAON
Annual Income Statement for AAON
Annual Cash Flow Statement for AAON
[1] 6
[1] "Error: NULL"
[1] 7
Annual Balance Sheet for AAPL
Annual Income Statement for AAPL
Annual Cash Flow Statement for AAPL
[1] 8
Annual Balance Sheet for AAVL
Annual Income Statement for AAVL
Annual Cash Flow Statement for AAVL
[1] 9
Annual Balance Sheet for AAWW
Annual Income Statement for AAWW
Annual Cash Flow Statement for AAWW
[1] 10
Annual Balance Sheet for AAXJ
Annual Income Statement for AAXJ
Annual Cash Flow Statement for AAXJ
But, I still can't get the dataframe. Here is a working example.
tickers <-new.env()
t <-c("AAL", "AAME", "AAOI")
lapply(t, getFinancials, env=tickers)
BS <- data.frame(lapply(tickers, function(x) {viewFinancials(x, type= 'BS', period = 'A')}))
IS <- data.frame(lapply(tickers, function(x) {viewFinancials(x, type= 'IS', period = 'A')}))
CF <- data.frame(lapply(tickers, function(x) {viewFinancials(x, type= 'CF', period = 'A')}))
Here is a non working example with AAIT
tickers <-new.env()
t <-c("AAIT", "AAME", "AAOI")
lapply(t, getFinancials, env=tickers)
BS <- data.frame(lapply(tickers, function(x) {viewFinancials(x, type= 'BS', period = 'A')}))
IS <- data.frame(lapply(tickers, function(x) {viewFinancials(x, type= 'IS', period = 'A')}))
CF <- data.frame(lapply(tickers, function(x) {viewFinancials(x, type= 'CF', period = 'A')}))