python 我如何改进我的股票查询代码,使其在5分钟内运行?

yc0p9oo0  于 2022-11-21  发布在  Python
关注(0)|答案(1)|浏览(127)

我有一个股票符号列表,我需要从中提取金融数据。我写了一个函数来获取我需要的所有数据(见下文)。
我在35只股票上测试了它,我花了9分钟来运行。真实的的数据集有600多个股票符号,这将需要几个小时的运行。
你能检查我的代码,并建议如何使它运行在不到5分钟,请?
以下是我需要的每只股票的财务指标:

# Free Cash Flow 
# EV/EBIDTA
# P/E Ratio
# YoY Growth for Profit Margins
# EV/ Revenue

以下是示例数据集:
| | 符号|
| - -|- -|
| 第0页|AAOI系统|
| 一个|AAPL语言|
| 2个|ACCD公司|
| 三个|ACEV汽车|
| 四个|阿塞弗|
代码如下:
(简短说明:尝试为每只股票获得上面列出的5个财务指标,如果有一些数据缺失,则只需分配np.NaN值)

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

是否有一种方法可以使该功能在时间方面更有效?

rkkpypqq

rkkpypqq1#

我知道这个帖子很老了,但是我现在才发现它。去“yfinance”图书馆看看。那里有各种各样的东西!!

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

季度财务数据示例:

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

相关问题