Reputation: 234
I have a list of stock symbols, for which I need to extract financial data. I wrote a function to get all data I need (see below).
I tested it on 35 stocks, it took me 9 min to run. The real dataset has more than 600 stock symbols, which would take hours of running.
Can you review my code and advise on how to make it run in less than 5 minutes, please?
Here are financial indicators I need for each stock:
# Free Cash Flow
# EV/EBIDTA
# P/E Ratio
# YoY Growth for Profit Margins
# EV/ Revenue
Here is sample dataset:
Symbol | |
---|---|
0 | AAOI |
1 | AAPL |
2 | ACCD |
3 | ACEV |
4 | ACEVU |
Here is the code:
(Short Explanation: trying to get 5 financial indicators listed above for each stock, and if there is some missing data, then simply assign np.NaN value)
array=df['Symbol']
fin_df=pd.DataFrame()
for item in array:
#part 1
symbol=yf.Ticker(item)
info_df=pd.DataFrame(pd.Series(symbol.info)).T
values=['symbol','freeCashflow','enterpriseToEbitda','enterpriseToRevenue']
for value in values:
if value not in list(info_df.columns):
info_df[value]=np.NaN
else:
pass
info_df=info_df[['symbol','freeCashflow','enterpriseToEbitda','enterpriseToRevenue']]
#part 2
info_2=symbol.financials.T
try:
info_df['YoY Profit Margins Growth']=round(info_2['Gross Profit'][0]/info_2['Gross Profit'][1],2)
except:
info_df['YoY Profit Margins Growth']=np.NaN
#part 3
#info_3=pd.DataFrame(pd.Series(si.get_quote_table(item))).T
info_3=pd.DataFrame()
try:
info_3['PE Ratio (TTM)']=pd.Series(si.get_quote_table(item)).T['PE Ratio (TTM)']
except:
info_3['PE Ratio (TTM)']=np.NaN
info_df['PERatio']=info_3['PE Ratio (TTM)']
fin_df=pd.concat([fin_df,info_df])
fin_df.reset_index(drop=True, inplace=True)
fin_df
Is there is a way to make the function more efficient in terms of time?
Upvotes: -1
Views: 203
Reputation: 20302
I know this post is pretty old, but I just came across it now. Check out the 'yfinance' library. There's all kinds of stuff available over there!!
import pandas_datareader as web
import pandas as pd
df = web.DataReader('AAPL', data_source='yahoo', start='2011-01-01', end='2021-01-12')
df.head()
import yfinance as yf
aapl = yf.Ticker("AAPL")
aapl
# get stock info
aapl.info
# get historical market data
hist = aapl.history(period="max")
# show actions (dividends, splits)
aapl.actions
# show dividends
aapl.dividends
# show splits
aapl.splits
# show financials
aapl.financials
aapl.quarterly_financials
# show major holders
aapl.major_holders
# show institutional holders
aapl.institutional_holders
# show balance sheet
aapl.balance_sheet
aapl.quarterly_balance_sheet
# show cashflow
aapl.cashflow
aapl.quarterly_cashflow
# show earnings
aapl.earnings
aapl.quarterly_earnings
# show sustainability
aapl.sustainability
# show analysts recommendations
aapl.recommendations
# show next event (earnings, etc)
aapl.calendar
# show ISIN code - *experimental*
# ISIN = International Securities Identification Number
aapl.isin
# show options expirations
aapl.options
# get option chain for specific expiration
opt = aapl.option_chain('YYYY-MM-DD')
Quarterly Financials Example:
2022-09-24 2022-06-25 \
Research Development 6761000000.0 6797000000.0
Effect Of Accounting Charges None None
Income Before Tax 24657000000.0 23066000000.0
Minority Interest None None
Net Income 20721000000.0 19442000000.0
Selling General Administrative 6440000000.0 6012000000.0
Gross Profit 38095000000.0 35885000000.0
Ebit 24894000000.0 23076000000.0
Operating Income 24894000000.0 23076000000.0
Other Operating Expenses None None
Interest Expense -827000000.0 -719000000.0
Extraordinary Items None None
Non Recurring None None
Other Items None None
Income Tax Expense 3936000000.0 3624000000.0
Total Revenue 90146000000.0 82959000000.0
Total Operating Expenses 65252000000.0 59883000000.0
Cost Of Revenue 52051000000.0 47074000000.0
Total Other Income Expense Net -237000000.0 -10000000.0
Discontinued Operations None None
Net Income From Continuing Ops 20721000000.0 19442000000.0
Net Income Applicable To Common Shares 20721000000.0 19442000000.0
2022-03-26 2021-12-25
Research Development 6387000000.0 6306000000.0
Effect Of Accounting Charges None None
Income Before Tax 30139000000.0 41241000000.0
Minority Interest None None
Net Income 25010000000.0 34630000000.0
Selling General Administrative 6193000000.0 6449000000.0
Gross Profit 42559000000.0 54243000000.0
Ebit 29979000000.0 41488000000.0
Operating Income 29979000000.0 41488000000.0
Other Operating Expenses None None
Interest Expense -691000000.0 -694000000.0
Extraordinary Items None None
Non Recurring None None
Other Items None None
Income Tax Expense 5129000000.0 6611000000.0
Total Revenue 97278000000.0 123945000000.0
Total Operating Expenses 67299000000.0 82457000000.0
Cost Of Revenue 54719000000.0 69702000000.0
Total Other Income Expense Net 160000000.0 -247000000.0
Discontinued Operations None None
Net Income From Continuing Ops 25010000000.0 34630000000.0
Net Income Applicable To Common Shares 25010000000.0 34630000000.0
Upvotes: 1