Feb-13-2021, 07:51 AM
(This post was last modified: Feb-14-2021, 05:44 AM by detlefschmitt.)
To make the program more useful I added a few functions. I added importing ticker symbols from a csv file, deleting unwanted columns, renaming columns and changing the columns order. The program in this example imports the csv file column with the header name "Ticker". How to create a csv file containing e.g. the ticker symbols from the NASDAQ can be found in this post.
The program worked fine with about 350 stocks at once. However, running this program several times with that many tickers in a short time can cause a "HTTPError: Service Unavailable" type error. Also make sure that all data is retrieved. It happened to me that data in a couple columns was missing one time. The next morning then the program again was able to get all data. So, if you run into such a problem, it is not caused by an error in the program. It is an error related to retrieving the information from the website. A website access problem.
The program worked fine with about 350 stocks at once. However, running this program several times with that many tickers in a short time can cause a "HTTPError: Service Unavailable" type error. Also make sure that all data is retrieved. It happened to me that data in a couple columns was missing one time. The next morning then the program again was able to get all data. So, if you run into such a problem, it is not caused by an error in the program. It is an error related to retrieving the information from the website. A website access problem.
import pandas as pd from yahoo_fin import stock_info as si import glob ti = pd.read_csv('TickerList.csv') ti2 = ti["Ticker"].tolist() stock_list = ti2 stats = {} for ticker in stock_list: data = si.get_stats(ticker) stats[ticker] = data combined = pd.concat(stats) combined = combined.reset_index() del combined["level_1"] combined.columns = ["Ticker", "Attribute", "Recent"] df = pd.DataFrame(combined) df2 = df.pivot(index='Ticker', columns='Attribute', values='Recent') cols = [3,7,12,13,14,17,18,19,20,22,23,24,25,26,27,28,30,31,32,33,34,35,38,39,41,43,45,49,51,56,57,58] df2.drop(df2.columns[cols],axis=1,inplace=True) df2.rename(columns={ df2.columns[0]: "% Insider" }, inplace = True) df2.rename(columns={ df2.columns[1]: "% Institution" }, inplace = True) df2.rename(columns={ df2.columns[2]: "200-Day MA" }, inplace = True) df2.rename(columns={ df2.columns[3]: "50-Day MA" }, inplace = True) df2.rename(columns={ df2.columns[4]: "52-Week H" }, inplace = True) df2.rename(columns={ df2.columns[5]: "52-Week L" }, inplace = True) df2.rename(columns={ df2.columns[6]: "Avg Vol 10D" }, inplace = True) df2.rename(columns={ df2.columns[7]: "Avg Vol 3M" }, inplace = True) df2.rename(columns={ df2.columns[8]: "Beta" }, inplace = True) df2.rename(columns={ df2.columns[9]: "Book/Share" }, inplace = True) df2.rename(columns={ df2.columns[10]: "EBITDA" }, inplace = True) df2.rename(columns={ df2.columns[11]: "Enterprice V" }, inplace = True) df2.rename(columns={ df2.columns[12]: "Float" }, inplace = True) df2.rename(columns={ df2.columns[13]: "Market Cap" }, inplace = True) df2.rename(columns={ df2.columns[14]: "Price/Book" }, inplace = True) df2.rename(columns={ df2.columns[15]: "Price/Sales" }, inplace = True) df2.rename(columns={ df2.columns[16]: "Q Rev (YOY)" }, inplace = True) df2.rename(columns={ df2.columns[17]: "Return on Equity" }, inplace = True) df2.rename(columns={ df2.columns[18]: "Revenue/Share" }, inplace = True) df2.rename(columns={ df2.columns[19]: "Shares" }, inplace = True) df2.rename(columns={ df2.columns[20]: "Shares Short" }, inplace = True) df2.rename(columns={ df2.columns[21]: "Short prior" }, inplace = True) df2.rename(columns={ df2.columns[22]: "% Short" }, inplace = True) df2.rename(columns={ df2.columns[23]: "Cash" }, inplace = True) df2.rename(columns={ df2.columns[24]: "Cash/Share" }, inplace = True) df2.rename(columns={ df2.columns[25]: "Debt" }, inplace = True) df2.rename(columns={ df2.columns[26]: "Debt/Equity" }, inplace = True) order = [13,11,15,14,8,4,5,3,2,7,6,19,12,0,1,20,22,21,17,18,16,10,23,24,25,26,9] df3 = df2[[df2.columns[i] for i in order]] df3.to_csv(r'stats.csv')