0

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')}))
Jthorpe
  • 9,756
  • 2
  • 49
  • 64
dragon
  • 88
  • 8
  • you probably want `if(is.null(get_fin)) next` (see `?next` for details), or use `try()` for `tryCatch()`. See [this SO post](http://stackoverflow.com/questions/29001596/not-avoiding-skipping-errors-with-try-and-trycatch/29051517#29051517) for a primer on `try()` and `tryCatch()` – Jthorpe Jun 28 '15 at 03:39
  • next with out the brackets did not work. The best thing to do is work with the bottom two codes to see how to figure out the problem. I am researching. Thanks for the link! – dragon Jun 28 '15 at 03:49
  • To all: I was trying to think of an automatic delete; if I ran into the error like `StockList[-c(i)]`, but that was only taking off the 10th element in the list. – dragon Jun 28 '15 at 03:58

1 Answers1

0

next breaks out of the inner most for loop, and you don't need braces when the body is just a single statement. Try this:

StockList <- c("AAIT", "AAL",  "AAME", "AAOI", "AAON", "AAPC", "AAPL", "AAVL", "AAWW", "AAXJ")
for(i in 1:length(StockList)){
    worked  <-  TRUE
    get_fin<-tryCatch(stuff that might fail, 
                      error=function(e)
                          # use the scoping assignment operator to change the value of `worked` 
                          worked  <<-  FALSE)                          
    if(!worked) 
        next 
    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')}))
}

BTW, I noticed you loop over i in 1:length(stocklist) but you don't reference i in the body of your for loop, which is weird.

Jthorpe
  • 9,756
  • 2
  • 49
  • 64
  • The i is a mistake when I was trying to make a reproducible example. You're answer is leading me towards the right direction, but getFinancials cannot be deleted from the code. Thanks, for the input. I need time to go over the errors, but thanks. I'll let you know, if this answers my question. – dragon Jun 28 '15 at 04:13
  • this is what I had initially, which might tie in better than lapply and with your example `get_fin<-tryCatch(getFinancials(StocksList[i], auto.assign = FALSE), error=function(e) NULL)` – dragon Jun 28 '15 at 04:22
  • What I like about the `worked` "function" in the new code, is that `NULL` is no longer in the equation. Also, I like the overriding scooping assignment operator, but the code does not output dataframes. – dragon Jun 28 '15 at 04:31