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:
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')
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
.