0

I am trying to scrape stock market information from all the stocks concerning the FTSE250. I use Yahoo_Fin in order to do so. The code works, but I get an error that a stock is delisted. Hence, I tried to put-in an except exception code. I read the documentation about the try and except libraries, but could not find the correct answer. I don't receive a syntax error, but the except exception code doesn't do anything.

EDIT: Putting two except exceptions works, below is the updated code.

index_df = pdr.get_data_yahoo(index_name, start_date, end_date, progress=False)
index_df['Percent Change'] = index_df['Adj Close'].pct_change()
index_return = (index_df['Percent Change'] + 1).cumprod()[-1]

for ticker in tickers:
    # Download historical data as CSV for each stock (makes the process faster)
  try:
      df = pdr.get_data_yahoo(ticker, start_date, end_date,progress=False)
      df.to_csv(f'{ticker}.csv')
  except: 
    except Exception: 
    if ticker not in tickers:
    next(ticker)

for ticker in tickers:
  try:

   # Calculating returns relative to the market (returns multiple)
    df['Percent Change'] = df['Adj Close'].pct_change()
    stock_return = (df['Percent Change'] + 1).cumprod()[-1]
    
    returns_multiple = round((stock_return / index_return), 2)
    returns_multiples.extend([returns_multiple])

  except Exception: 
   if ticker not in tickers:
    next(ticker)
3
  • post the stack trace of the error Commented Mar 29, 2021 at 19:10
  • 1 Failed download: - 3IN: No data found, symbol may be delisted and IndexError: index -1 is out of bounds for axis 0 with size 0 Commented Mar 29, 2021 at 19:11
  • Please post the full stack traces in the question. Commented Mar 29, 2021 at 19:12

1 Answer 1

1

Your exception doesn't make sense to me. You are looping over the tickers, then you check if the ticker is not in tickers. That will always be False, so your "next" statement is never going to be executed and it will just continue on.

Seems what you want is something like this:

for ticker in tickers:
    # Download historical data as CSV for each stock (makes the process faster)
    try:
        df = pdr.get_data_yahoo(ticker, start_date, end_date,progress=False)
        df.to_csv(f'{ticker}.csv')

        # Calculating returns relative to the market (returns multiple)
        df['Percent Change'] = df['Adj Close'].pct_change()
        stock_return = (df['Percent Change'] + 1).cumprod()[-1]

        returns_multiple = round((stock_return / index_return), 2)
        returns_multiples.extend([returns_multiple])

        #change the name of the stock index
        print (f'Ticker: {ticker};  Returns Multiple against FTSE 250 : {returns_multiple}\n')
        time.sleep(1)
 
    except IndexError:
        print(f"Error in ticker: {ticker}, skipping...")

Where you pass a proper except condition depending on what the traceback is (can't tell if it's FileNotFoundError or IndexError). But I think all you want to do is process the next ticker in the list?

Sign up to request clarification or add additional context in comments.

4 Comments

I want to stop process if a ticker is not fond and continue with the next ticker. The code stops at stock_return = (df['Percent Change'] + 1).cumprod()[-1]. So to code can't calculate the returns of the stock relative to the market. The except: continue code does't help as well.
Probably best practice would be to put your entire code in the try block with an exception (you still haven't posted the actual Traceback) to catch whatever is causing the error. It sounds like you are saying the except block is before where the actual error occurs. I suggested continue because that should grab the next ticker in the list and try to process it instead.
Note that you might be running into two possible error conditions - one where the ticker isn't found and another where it's found but the data is missing. So you might need two exceptions if that is the case.
thank you, creating two exceptions works.

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.