Downloading mutliple stocks at once from yahoo finance python

前端 未结 4 848
一向
一向 2020-12-06 02:26

I have a question about the function of yahoo finance using the pandas data reader. I\'m using for months now a list with stock tickers and execute it in the following lines

4条回答
  •  遥遥无期
    2020-12-06 02:41

    You can use the new Python YahooFinancials module with pandas to do this. YahooFinancials is well built and gets it's data by hashing out the datastore object present in each Yahoo Finance Web page, so it's fast and doesn't rely on the old discontinued api nor a web driver like a scraper does. Data is returned as JSON and you can pull as many stocks as you want at once by passing in a list of stock/index tickers to initialize the YahooFinancials Class with.

    $ pip install yahoofinancials

    Usage Example:

    from yahoofinancials import YahooFinancials
    import pandas as pd
    
    # Select Tickers and stock history dates
    ticker = 'AAPL'
    ticker2 = 'MSFT'
    ticker3 = 'INTC'
    index = '^NDX'
    freq = 'daily'
    start_date = '2012-10-01'
    end_date = '2017-10-01'
    
    
    # Function to clean data extracts
    def clean_stock_data(stock_data_list):
        new_list = []
        for rec in stock_data_list:
            if 'type' not in rec.keys():
                new_list.append(rec)
        return new_list
    
    # Construct yahoo financials objects for data extraction
    aapl_financials = YahooFinancials(ticker)
    mfst_financials = YahooFinancials(ticker2)
    intl_financials = YahooFinancials(ticker3)
    index_financials = YahooFinancials(index)
    
    # Clean returned stock history data and remove dividend events from price history
    daily_aapl_data = clean_stock_data(aapl_financials
                                         .get_historical_stock_data(start_date, end_date, freq)[ticker]['prices'])
    daily_msft_data = clean_stock_data(mfst_financials
                                         .get_historical_stock_data(start_date, end_date, freq)[ticker2]['prices'])
    daily_intl_data = clean_stock_data(intl_financials
                                         .get_historical_stock_data(start_date, end_date, freq)[ticker3]['prices'])
    daily_index_data = index_financials.get_historical_stock_data(start_date, end_date, freq)[index]['prices']
    stock_hist_data_list = [{'NDX': daily_index_data}, {'AAPL': daily_aapl_data}, {'MSFT': daily_msft_data},
                            {'INTL': daily_intl_data}]
    
    
    # Function to construct data frame based on a stock and it's market index
    def build_data_frame(data_list1, data_list2, data_list3, data_list4):
        data_dict = {}
        i = 0
        for list_item in data_list2:
            if 'type' not in list_item.keys():
                data_dict.update({list_item['formatted_date']: {'NDX': data_list1[i]['close'], 'AAPL': list_item['close'],
                                                                'MSFT': data_list3[i]['close'],
                                                                'INTL': data_list4[i]['close']}})
                i += 1
        tseries = pd.to_datetime(list(data_dict.keys()))
        df = pd.DataFrame(data=list(data_dict.values()), index=tseries,
                          columns=['NDX', 'AAPL', 'MSFT', 'INTL']).sort_index()
        return df
    

    Multiple stocks data at once example (returns list of JSON objects for each ticker):

    from yahoofinancials import YahooFinancials
    
    tech_stocks = ['AAPL', 'MSFT', 'INTC']
    bank_stocks = ['WFC', 'BAC', 'C']
    
    yahoo_financials_tech = YahooFinancials(tech_stocks)
    yahoo_financials_banks = YahooFinancials(bank_stocks)
    
    tech_cash_flow_data_an = yahoo_financials_tech.get_financial_stmts('annual', 'cash')
    bank_cash_flow_data_an = yahoo_financials_banks.get_financial_stmts('annual', 'cash')
    
    banks_net_ebit = yahoo_financials_banks.get_ebit()
    tech_stock_price_data = tech_cash_flow_data.get_stock_price_data()
    daily_bank_stock_prices = yahoo_financials_banks.get_historical_stock_data('2008-09-15', '2017-09-15', 'daily')
    

    JSON Output Example:

    Code:

    yahoo_financials = YahooFinancials('WFC')
    print(yahoo_financials.get_historical_stock_data("2017-09-10", "2017-10-10", "monthly"))
    

    JSON Return:

    {
        "WFC": {
            "prices": [
                {
                    "volume": 260271600,
                    "formatted_date": "2017-09-30",
                    "high": 55.77000045776367,
                    "adjclose": 54.91999816894531,
                    "low": 52.84000015258789,
                    "date": 1506830400,
                    "close": 54.91999816894531,
                    "open": 55.15999984741211
                }
            ],
            "eventsData": [],
            "firstTradeDate": {
                "date": 76233600,
                "formatted_date": "1972-06-01"
            },
            "isPending": false,
            "timeZone": {
                "gmtOffset": -14400
            },
            "id": "1mo15050196001507611600"
        }
    }
    

提交回复
热议问题