3

I am struggling with the instructions for tryCatch() in R. I'm trying to capture the closing price for a ticker.

Case 2 = good case = TickersJuly2 = unique price to ticker relationship

Case 1 = bad case = TickersJuly1 = FABU close price is the repeat of CETX

Case 1 desired output is a 0 for FABU.

library(TTR)
close.price1=NULL
TickersJuly1 <- c('DIT','CETX','FABU')
TickersJuly2<- c('AAPL','A','AA')

for(i in TickersJuly1){
           tryCatch(close <- getYahooData(i,20150727,20150727,'daily',"price"),
               error = function(e) close$Close <- 0,
               warning = function(w) close$Close <- 0,
               finally = function(f) close$Close <- 0)
      close.price <- c(as.character(close$Close),i)
      close.price1 <- rbind(close.price1,close.price)
}

1 Answer 1

4

I think this works. You should be assigning the result of the tryCatch to a variable.

for(i in TickersJuly1){
    close <- tryCatch(
        getYahooData(i,20150727,20150727,'daily',"price"),
        error = function(e) list(Close=0),
        warning = function(w) list(Close=0),
        finally = function(f) list(Close=0))
    close.price <- c(as.character(close$Close),i)
    close.price1 <- rbind(close.price1,close.price)
}
Sign up to request clarification or add additional context in comments.

2 Comments

The for loop stops after tryCatch assigns close <-0. You could test this by having TickersJuly1 <- c('DIT',FABU',CETX') . The last ticker will not get evaluated, CETX.
it does when I run it, I get ` [,1] [,2] close.price "83.540001" "DIT" close.price "0" "FABU" close.price "2.91" "CETX" `

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.