Click here to Skip to main content
15,889,651 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a list of US stocks (around 3000 stocks) in Excel and I want to get the stock price and other related daily stock info on each stock in certain time period. So I wrote a for loop to get all related stock info on each stock (and I have tried both yahoo_fin.stock_info and yfinance packages). But then, it just got stuck at certain stock and never return the result. I am using spyder for my Python coding. Below is the code:

Python
import pandas as pd
import numpy as np
import yahoo_fin.stock_info as si

ticker_list = pd.read_excel(r'C:\Users\user\Desktop\ticker_list.xlsx',engine="openpyxl")

n=[]
et=[] 
df_empty=[]
for i in range(len(ticker_list)): 
    try:
        df = df_empty[0:0]
        df=si.get_data(ticker_list.iloc[i]['ticker'], 
           start_date='2021-09-30', end_date='2022-04-25') 
# I used yf.download for using yfinance package
        mkt_cap=si.get_quote_table(ticker_list.iloc[i]['ticker'])["Market Cap"]
        df.insert (7,"mkt_cap",mkt_cap) 
        print(ticker_list.iloc[i]['ticker'])
        df.replace(np.inf, np.nan)        
        df = df.dropna()
        df.reset_index(inplace=True)   
        n.append(df)   
    except:
        et.append(ticker_list.iloc[i]['ticker'])
        print('error: ' + ticker_list.iloc[i]['ticker'])
        pass
print(n)


When I run the code below, what it shows in my console:

Python 3.8.13 (default, Mar 28 2022, 06:59:08) [MSC v.1916 64 bit (AMD64)]
Type "copyright", "credits" or "license" for more information.

IPython 7.31.1 -- An enhanced Interactive Python.

runfile('C:/Users/USER/get_all_stock_data.py', wdir='C:/Users/USER')
BYDDY
CHTR
JD
KYCCF
RY-PT
GE
BP
MO
VLKPF
SMAWF
AMAT


I tired many times already, it just stuck on different ticker everytime. I have also tried to leave the code running for a day but it just stopped it right there and never give me return. When I tried to debug, it also stopped running at either the si.get.data line or si.get_quote_table line. I actually have been using this code since last year and it works fine, it just did not work when I run this code again last week. I also tried several other packages (e.g., pandas_datareader.data) still not able to get the stock info. Appreciate if anyone can let me know what is the problem.

What I have tried:

yfinance and yahoo_finance and also padas_datareader.
Posted
Updated 6-May-22 11:06am
v2
Comments
Richard MacCutchan 29-Apr-22 7:16am    
The issue is with the remote site that is doing the lookup. You need to find out why it is not returning.

1 solution

I believe Yahoo has stopped allowing the free download of data. They have started a paid service. I hate Yahoo.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900