Coder Social home page Coder Social logo

jecsand / yahoofinancials Goto Github PK

View Code? Open in Web Editor NEW
880.0 57.0 212.0 429 KB

A powerful financial data module used for pulling data from Yahoo Finance. This module can pull fundamental and technical data for stocks, indexes, currencies, cryptos, ETFs, Mutual Funds, U.S. Treasuries, and commodity futures.

Home Page: https://pypi.python.org/pypi/yahoofinancials

License: MIT License

Python 100.00%
stock-data yahoo-finance financial-data stock-quotes fundamentals cryptocurrencies forex commodities etfs mutual-funds

yahoofinancials's Introduction

yahoofinancials

A python module that returns stock, cryptocurrency, forex, mutual fund, commodity futures, ETF, and US Treasury financial data from Yahoo Finance.

image

image

image

image

Current Version: v1.20

Version Released: 12/17/2023

Report any bugs by opening an issue here: https://github.com/JECSand/yahoofinancials/issues

Overview

A powerful financial data module used for pulling both fundamental and technical data from Yahoo Finance.

  • New analytic methods in v1.20:
    • get_insights()
      • returns data for:
        • 'instrumentInfo'
        • 'companySnapshot'
        • 'recommendation'
        • 'sigDevs'
        • 'secReports'
    • get_recommendations()
  • Example:
print(YahooFinancials('C').get_recommendations())
  • Example Output:
{
    "C": [
        {
            "recommendedSymbols": [
                {
                    "score": 0.239602,
                    "symbol": "BAC"
                },
                {
                "score": 0.225134,
                "symbol": "JPM"
                },
                {
                "score": 0.167669,
                "symbol": "WFC"
                },
                {
                "score": 0.145864,
                "symbol": "GS"
                },
                {
                "score": 0.134071,
                "symbol": "F"
                }
            ],
            "symbol": "C"
        }
    ]
}
  • As of Version 1.20, YahooFinancials supports a new optional parameter called flat_format.
    • When YahooFinancials(flat_format=True), financial statement data will return in a dict instead of a list. The keys of the dict will be the reporting dates.
    • Default is False, to ensure backwards compatibility.
  • As of Version 1.9, YahooFinancials supports optional parameters for asynchronous execution, proxies, and international requests.
from yahoofinancials import YahooFinancials
tickers = ['AAPL', 'GOOG', 'C']
yahoo_financials = YahooFinancials(tickers, concurrent=True, max_workers=8, country="US")
balance_sheet_data_qt = yahoo_financials.get_financial_stmts('quarterly', 'balance')
print(balance_sheet_data_qt)

proxy_addresses = [ "mysuperproxy.com:5000", "mysuperproxy.com:5001"]
yahoo_financials = YahooFinancials(tickers, concurrent=True, proxies=proxy_addresses)
balance_sheet_data_qt = yahoo_financials.get_financial_stmts('quarterly', 'balance')
print(balance_sheet_data_qt)

Installation

  • yahoofinancials runs on Python 3.7, 3.8, 3.9, 3.10, 3.11, and 3.12
  1. Installation using pip:
  • Linux/Mac:
$ pip install yahoofinancials
  • Windows (If python doesn't work for you in cmd, try running the following command with just py):
> python -m pip install yahoofinancials
  1. Installation using github (Mac/Linux):
$ git clone https://github.com/JECSand/yahoofinancials.git
$ cd yahoofinancials
$ python setup.py install
  1. Demo using the included demo script:
$ cd yahoofinancials
$ python demo.py -h
$ python demo.py
$ python demo.py WFC C BAC
  1. Test using the included unit testing script:
$ cd yahoofinancials
$ python test/test_yahoofinancials.py

Module Methods

  • The financial data from all methods is returned as JSON.
  • You can run multiple symbols at once using an inputted array or run an individual symbol using an inputted string.
  • YahooFinancials works with Python 3.7, 3.8, 3.9, 3.10, 3.11 and 3.12 and runs on all operating systems. (Windows, Mac, Linux).
  1. get_financial_stmts(frequency, statement_type, reformat=True)
    • frequency can be either 'annual' or 'quarterly'.
    • statement_type can be 'income', 'balance', 'cash' or a list of several.
    • reformat optional value defaulted to true. Enter False for unprocessed raw data from Yahoo Finance.
  2. get_stock_price_data(reformat=True)
  3. get_stock_earnings_data()
    • reformat optional value defaulted to true. Enter False for unprocessed raw data from Yahoo Finance.
  4. get_summary_data(reformat=True)
    • Returns financial summary data for cryptocurrencies, stocks, currencies, ETFs, mutual funds, U.S. Treasuries, commodity futures, and indexes.
    • reformat optional value defaulted to true. Enter False for unprocessed raw data from Yahoo Finance.
  5. get_stock_quote_type_data()
  6. get_historical_price_data(start_date, end_date, time_interval)
    • This method will pull historical pricing data for stocks, currencies, ETFs, mutual funds, U.S. Treasuries, cryptocurrencies, commodities, and indexes.
    • start_date should be entered in the 'YYYY-MM-DD' format and is the first day that data will be pulled for.
    • end_date should be entered in the 'YYYY-MM-DD' format and is the last day that data will be pulled for.
    • time_interval can be either 'daily', 'weekly', or 'monthly'. This variable determines the time period interval for your pull.
    • Data response includes relevant pricing event data such as dividends and stock splits.
  7. get_num_shares_outstanding(price_type='current')
    • price_type can also be set to 'average' to calculate the shares outstanding with the daily average price.

Additional Module Methods

  • get_daily_dividend_data(start_date, end_date)
  • get_stock_profile_data()
  • get_financial_data()
  • get_interest_expense()
  • get_operating_income()
  • get_total_operating_expense()
  • get_total_revenue()
  • get_cost_of_revenue()
  • get_income_before_tax()
  • get_income_tax_expense()
  • get_esg_score_data()
  • get_gross_profit()
  • get_net_income_from_continuing_ops()
  • get_research_and_development()
  • get_current_price()
  • get_current_change()
  • get_current_percent_change()
  • get_current_volume()
  • get_prev_close_price()
  • get_open_price()
  • get_ten_day_avg_daily_volume()
  • get_stock_exchange()
  • get_market_cap()
  • get_daily_low()
  • get_daily_high()
  • get_currency()
  • get_yearly_high()
  • get_yearly_low()
  • get_dividend_yield()
  • get_annual_avg_div_yield()
  • get_five_yr_avg_div_yield()
  • get_dividend_rate()
  • get_annual_avg_div_rate()
  • get_50day_moving_avg()
  • get_200day_moving_avg()
  • get_beta()
  • get_payout_ratio()
  • get_pe_ratio()
  • get_price_to_sales()
  • get_exdividend_date()
  • get_book_value()
  • get_ebit()
  • get_net_income()
  • get_earnings_per_share()
  • get_key_statistics_data()
  • get_stock_profile_data()
  • get_financial_data()

Usage Examples

  • The class constructor can take either a single ticker or a list of tickers as it's parameter.
  • This makes it easy to initiate multiple classes for different groupings of financial assets.
  • Quarterly statement data returns the last 4 periods of data, while annual returns the last 3.

Single Ticker Example

from yahoofinancials import YahooFinancials

ticker = 'AAPL'
yahoo_financials = YahooFinancials(ticker)

balance_sheet_data_qt = yahoo_financials.get_financial_stmts('quarterly', 'balance')
income_statement_data_qt = yahoo_financials.get_financial_stmts('quarterly', 'income')
all_statement_data_qt =  yahoo_financials.get_financial_stmts('quarterly', ['income', 'cash', 'balance'])
apple_earnings_data = yahoo_financials.get_stock_earnings_data()
apple_net_income = yahoo_financials.get_net_income()
historical_stock_prices = yahoo_financials.get_historical_price_data('2008-09-15', '2018-09-15', 'weekly')

Lists of Tickers Example

from yahoofinancials import YahooFinancials

tech_stocks = ['AAPL', 'MSFT', 'INTC']
bank_stocks = ['WFC', 'BAC', 'C']
commodity_futures = ['GC=F', 'SI=F', 'CL=F']
cryptocurrencies = ['BTC-USD', 'ETH-USD', 'XRP-USD']
currencies = ['EURUSD=X', 'JPY=X', 'GBPUSD=X']
mutual_funds = ['PRLAX', 'QASGX', 'HISFX']
us_treasuries = ['^TNX', '^IRX', '^TYX']

yahoo_financials_tech = YahooFinancials(tech_stocks)
yahoo_financials_banks = YahooFinancials(bank_stocks)
yahoo_financials_commodities = YahooFinancials(commodity_futures)
yahoo_financials_cryptocurrencies = YahooFinancials(cryptocurrencies)
yahoo_financials_currencies = YahooFinancials(currencies)
yahoo_financials_mutualfunds = YahooFinancials(mutual_funds)
yahoo_financials_treasuries = YahooFinancials(us_treasuries)

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 = yahoo_financials_tech.get_stock_price_data()
daily_bank_stock_prices = yahoo_financials_banks.get_historical_price_data('2008-09-15', '2018-09-15', 'daily')
daily_commodity_prices = yahoo_financials_commodities.get_historical_price_data('2008-09-15', '2018-09-15', 'daily')
daily_crypto_prices = yahoo_financials_cryptocurrencies.get_historical_price_data('2008-09-15', '2018-09-15', 'daily')
daily_currency_prices = yahoo_financials_currencies.get_historical_price_data('2008-09-15', '2018-09-15', 'daily')
daily_mutualfund_prices = yahoo_financials_mutualfunds.get_historical_price_data('2008-09-15', '2018-09-15', 'daily')
daily_treasury_prices = yahoo_financials_treasuries.get_historical_price_data('2008-09-15', '2018-09-15', 'daily')

Examples of Returned JSON Data

  1. Annual Income Statement Data for Apple:
yahoo_financials = YahooFinancials('AAPL')
print(yahoo_financials.get_financial_stmts('annual', 'income'))
{
    "incomeStatementHistory": {
        "AAPL": [
            {
                "2016-09-24": {
                    "minorityInterest": null,
                    "otherOperatingExpenses": null,
                    "netIncomeFromContinuingOps": 45687000000,
                    "totalRevenue": 215639000000,
                    "totalOtherIncomeExpenseNet": 1348000000,
                    "discontinuedOperations": null,
                    "incomeTaxExpense": 15685000000,
                    "extraordinaryItems": null,
                    "grossProfit": 84263000000,
                    "netIncome": 45687000000,
                    "sellingGeneralAdministrative": 14194000000,
                    "interestExpense": null,
                    "costOfRevenue": 131376000000,
                    "researchDevelopment": 10045000000,
                    "netIncomeApplicableToCommonShares": 45687000000,
                    "effectOfAccountingCharges": null,
                    "incomeBeforeTax": 61372000000,
                    "otherItems": null,
                    "operatingIncome": 60024000000,
                    "ebit": 61372000000,
                    "nonRecurring": null,
                    "totalOperatingExpenses": 0
                }
            }
        ]
    }
}
  1. Annual Balance Sheet Data for Apple:
yahoo_financials = YahooFinancials('AAPL')
print(yahoo_financials.get_financial_stmts('annual', 'balance'))
{
    "balanceSheetHistory": {
        "AAPL": [
            {
                "2016-09-24": {
                    "otherCurrentLiab": 8080000000,
                    "otherCurrentAssets": 8283000000,
                    "goodWill": 5414000000,
                    "shortTermInvestments": 46671000000,
                    "longTermInvestments": 170430000000,
                    "cash": 20484000000,
                    "netTangibleAssets": 119629000000,
                    "totalAssets": 321686000000,
                    "otherLiab": 36074000000,
                    "totalStockholderEquity": 128249000000,
                    "inventory": 2132000000,
                    "retainedEarnings": 96364000000,
                    "intangibleAssets": 3206000000,
                    "totalCurrentAssets": 106869000000,
                    "otherStockholderEquity": 634000000,
                    "shortLongTermDebt": 11605000000,
                    "propertyPlantEquipment": 27010000000,
                    "deferredLongTermLiab": 2930000000,
                    "netReceivables": 29299000000,
                    "otherAssets": 8757000000,
                    "longTermDebt": 75427000000,
                    "totalLiab": 193437000000,
                    "commonStock": 31251000000,
                    "accountsPayable": 59321000000,
                    "totalCurrentLiabilities": 79006000000
                }
            }
        ]
    }
}
  1. Quarterly Cash Flow Statement Data for Citigroup:
yahoo_financials = YahooFinancials('C')
print(yahoo_financials.get_financial_stmts('quarterly', 'cash'))
{
    "cashflowStatementHistoryQuarterly": {
        "C": [
            {
                "2017-06-30": {
                    "totalCashFromOperatingActivities": -18505000000,
                    "effectOfExchangeRate": -117000000,
                    "totalCashFromFinancingActivities": 39798000000,
                    "netIncome": 3872000000,
                    "dividendsPaid": -760000000,
                    "salePurchaseOfStock": -1781000000,
                    "capitalExpenditures": -861000000,
                    "changeToLiabilities": -7626000000,
                    "otherCashflowsFromInvestingActivities": 82000000,
                    "totalCashflowsFromInvestingActivities": -22508000000,
                    "netBorrowings": 33586000000,
                    "depreciation": 901000000,
                    "changeInCash": -1332000000,
                    "changeToNetincome": 1444000000,
                    "otherCashflowsFromFinancingActivities": 8753000000,
                    "changeToOperatingActivities": -17096000000,
                    "investments": -23224000000
                }
            }
        ]
    }
}
  1. Monthly Historical Stock Price Data for Wells Fargo:
yahoo_financials = YahooFinancials('WFC')
print(yahoo_financials.get_historical_price_data("2018-07-10", "2018-08-10", "monthly"))
{
    "WFC": {
        "currency": "USD",
        "eventsData": {
            "dividends": {
                "2018-08-01": {
                    "amount": 0.43,
                    "date": 1533821400,
                    "formatted_date": "2018-08-09"
                }
            }
        },
        "firstTradeDate": {
            "date": 76233600,
            "formatted_date": "1972-06-01"
        },
        "instrumentType": "EQUITY",
        "prices": [
            {
                "adjclose": 57.19147872924805,
                "close": 57.61000061035156,
                "date": 1533096000,
                "formatted_date": "2018-08-01",
                "high": 59.5,
                "low": 57.08000183105469,
                "open": 57.959999084472656,
                "volume": 138922900
            }
        ],
        "timeZone": {
            "gmtOffset": -14400
        }
    }
}
  1. Monthly Historical Price Data for EURUSD:
yahoo_financials = YahooFinancials('EURUSD=X')
print(yahoo_financials.get_historical_price_data("2018-07-10", "2018-08-10", "monthly"))
{
    "EURUSD=X": {
        "currency": "USD",
        "eventsData": {},
        "firstTradeDate": {
            "date": 1070236800,
            "formatted_date": "2003-12-01"
        },
        "instrumentType": "CURRENCY",
        "prices": [
            {
                "adjclose": 1.1394712924957275,
                "close": 1.1394712924957275,
                "date": 1533078000,
                "formatted_date": "2018-07-31",
                "high": 1.169864296913147,
                "low": 1.1365960836410522,
                "open": 1.168961763381958,
                "volume": 0
            }
        ],
        "timeZone": {
            "gmtOffset": 3600
        }
    }
}
  1. Monthly Historical Price Data for BTC-USD:
yahoo_financials = YahooFinancials('BTC-USD')
print(yahoo_financials.get_historical_price_data("2018-07-10", "2018-08-10", "monthly"))
{
    "BTC-USD": {
        "currency": "USD",
        "eventsData": {},
        "firstTradeDate": {
            "date": 1279321200,
            "formatted_date": "2010-07-16"
        },
        "instrumentType": "CRYPTOCURRENCY",
        "prices": [
            {
                "adjclose": 6285.02001953125,
                "close": 6285.02001953125,
                "date": 1533078000,
                "formatted_date": "2018-07-31",
                "high": 7760.740234375,
                "low": 6133.02978515625,
                "open": 7736.25,
                "volume": 4334347882
            }
        ],
        "timeZone": {
            "gmtOffset": 3600
        }
    }
}
  1. Weekly Historical Price Data for Crude Oil Futures:
yahoo_financials = YahooFinancials('CL=F')
print(yahoo_financials.get_historical_price_data("2018-08-01", "2018-08-10", "weekly"))
{
    "CL=F": {
        "currency": "USD",
        "eventsData": {},
        "firstTradeDate": {
            "date": 1522555200,
            "formatted_date": "2018-04-01"
        },
        "instrumentType": "FUTURE",
        "prices": [
            {
                "adjclose": 68.58999633789062,
                "close": 68.58999633789062,
                "date": 1532923200,
                "formatted_date": "2018-07-30",
                "high": 69.3499984741211,
                "low": 66.91999816894531,
                "open": 68.37000274658203,
                "volume": 683048039
            },
            {
                "adjclose": 67.75,
                "close": 67.75,
                "date": 1533528000,
                "formatted_date": "2018-08-06",
                "high": 69.91999816894531,
                "low": 66.13999938964844,
                "open": 68.76000213623047,
                "volume": 1102357981
            }
        ],
        "timeZone": {
            "gmtOffset": -14400
        }
    }
}
  1. Apple Stock Quote Data:
yahoo_financials = YahooFinancials('AAPL')
print(yahoo_financials.get_stock_quote_type_data())
{
    "AAPL": {
        "underlyingExchangeSymbol": null,
        "exchangeTimezoneName": "America/New_York",
        "underlyingSymbol": null,
        "headSymbol": null,
        "shortName": "Apple Inc.",
        "symbol": "AAPL",
        "uuid": "8b10e4ae-9eeb-3684-921a-9ab27e4d87aa",
        "gmtOffSetMilliseconds": "-14400000",
        "exchange": "NMS",
        "exchangeTimezoneShortName": "EDT",
        "messageBoardId": "finmb_24937",
        "longName": "Apple Inc.",
        "market": "us_market",
        "quoteType": "EQUITY"
    }
}
  1. U.S. Treasury Current Pricing Data:
yahoo_financials = YahooFinancials(['^TNX', '^IRX', '^TYX'])
print(yahoo_financials.get_current_price())
{
    "^IRX": 2.033,
    "^TNX": 2.895,
    "^TYX": 3.062
}
  1. BTC-USD Summary Data:
yahoo_financials = YahooFinancials('BTC-USD')
print(yahoo_financials.get_summary_data())
{
    "BTC-USD": {
        "algorithm": "SHA256",
        "ask": null,
        "askSize": null,
        "averageDailyVolume10Day": 545573809,
        "averageVolume": 496761640,
        "averageVolume10days": 545573809,
        "beta": null,
        "bid": null,
        "bidSize": null,
        "circulatingSupply": 17209812,
        "currency": "USD",
        "dayHigh": 6266.5,
        "dayLow": 5891.87,
        "dividendRate": null,
        "dividendYield": null,
        "exDividendDate": "-",
        "expireDate": "-",
        "fiftyDayAverage": 6989.074,
        "fiftyTwoWeekHigh": 19870.62,
        "fiftyTwoWeekLow": 2979.88,
        "fiveYearAvgDividendYield": null,
        "forwardPE": null,
        "fromCurrency": "BTC",
        "lastMarket": "CCCAGG",
        "marketCap": 106325663744,
        "maxAge": 1,
        "maxSupply": 21000000,
        "navPrice": null,
        "open": 6263.2,
        "openInterest": null,
        "payoutRatio": null,
        "previousClose": 6263.2,
        "priceHint": 2,
        "priceToSalesTrailing12Months": null,
        "regularMarketDayHigh": 6266.5,
        "regularMarketDayLow": 5891.87,
        "regularMarketOpen": 6263.2,
        "regularMarketPreviousClose": 6263.2,
        "regularMarketVolume": 755834368,
        "startDate": "2009-01-03",
        "strikePrice": null,
        "totalAssets": null,
        "tradeable": false,
        "trailingAnnualDividendRate": null,
        "trailingAnnualDividendYield": null,
        "twoHundredDayAverage": 8165.154,
        "volume": 755834368,
        "volume24Hr": 750196480,
        "volumeAllCurrencies": 2673437184,
        "yield": null,
        "ytdReturn": null
    }
}
  1. Apple Key Statistics Data:
yahoo_financials = YahooFinancials('AAPL')
print(yahoo_financials.get_key_statistics_data())
{
    "AAPL": {
        "annualHoldingsTurnover": null,
        "enterpriseToRevenue": 2.973,
        "beta3Year": null,
        "profitMargins": 0.22413999,
        "enterpriseToEbitda": 9.652,
        "52WeekChange": -0.12707871,
        "morningStarRiskRating": null,
        "forwardEps": 13.49,
        "revenueQuarterlyGrowth": null,
        "sharesOutstanding": 4729800192,
        "fundInceptionDate": "-",
        "annualReportExpenseRatio": null,
        "totalAssets": null,
        "bookValue": 22.534,
        "sharesShort": 44915125,
        "sharesPercentSharesOut": 0.0095,
        "fundFamily": null,
        "lastFiscalYearEnd": 1538179200,
        "heldPercentInstitutions": 0.61208,
        "netIncomeToCommon": 59531001856,
        "trailingEps": 11.91,
        "lastDividendValue": null,
        "SandP52WeekChange": -0.06475246,
        "priceToBook": 6.7582316,
        "heldPercentInsiders": 0.00072999997,
        "nextFiscalYearEnd": 1601337600,
        "yield": null,
        "mostRecentQuarter": 1538179200,
        "shortRatio": 1,
        "sharesShortPreviousMonthDate": "2018-10-31",
        "floatShares": 4489763410,
        "beta": 1.127094,
        "enterpriseValue": 789555511296,
        "priceHint": 2,
        "threeYearAverageReturn": null,
        "lastSplitDate": "2014-06-09",
        "lastSplitFactor": "1/7",
        "legalType": null,
        "morningStarOverallRating": null,
        "earningsQuarterlyGrowth": 0.318,
        "priceToSalesTrailing12Months": null,
        "dateShortInterest": 1543536000,
        "pegRatio": 0.98,
        "ytdReturn": null,
        "forwardPE": 11.289103,
        "maxAge": 1,
        "lastCapGain": null,
        "shortPercentOfFloat": 0.0088,
        "sharesShortPriorMonth": 36469092,
        "category": null,
        "fiveYearAverageReturn": null
    }
}
  1. Apple and Wells Fargo Daily Dividend Data:
start_date = '1987-09-15'
end_date = '1988-09-15'
yahoo_financials = YahooFinancials(['AAPL', 'WFC'])
print(yahoo_financials.get_daily_dividend_data(start_date, end_date))
{
    "AAPL": [
        {
            "date": 564157800,
            "formatted_date": "1987-11-17",
            "amount": 0.08
        },
        {
            "date": 571674600,
            "formatted_date": "1988-02-12",
            "amount": 0.08
        },
        {
            "date": 579792600,
            "formatted_date": "1988-05-16",
            "amount": 0.08
        },
        {
            "date": 587655000,
            "formatted_date": "1988-08-15",
            "amount": 0.08
        }
    ],
    "WFC": [
        {
            "date": 562861800,
            "formatted_date": "1987-11-02",
            "amount": 0.3008
        },
        {
            "date": 570724200,
            "formatted_date": "1988-02-01",
            "amount": 0.3008
        },
        {
            "date": 578583000,
            "formatted_date": "1988-05-02",
            "amount": 0.3344
        },
        {
            "date": 586445400,
            "formatted_date": "1988-08-01",
            "amount": 0.3344
        }
    ]
}
  1. Apple key Financial Data:
yahoo_financials = YahooFinancials("AAPL")
print(yahoo_financials.get_financial_data())
{
    'AAPL': {
        'ebitdaMargins': 0.29395,
        'profitMargins': 0.21238,
        'grossMargins': 0.37818,
        'operatingCashflow': 69390999552,
        'revenueGrowth': 0.018,
        'operatingMargins': 0.24572,
        'ebitda': 76476997632,
        'targetLowPrice': 150,
        'recommendationKey': 'buy',
        'grossProfits': 98392000000,
        'freeCashflow': 42914250752,
        'targetMedianPrice': 270,
        'currentPrice': 261.78,
        'earningsGrowth': 0.039,
        'currentRatio': 1.54,
        'returnOnAssets': 0.11347,
        'numberOfAnalystOpinions': 40,
        'targetMeanPrice': 255.51,
        'debtToEquity': 119.405,
        'returnOnEquity': 0.55917,
        'targetHighPrice': 300,
        'totalCash': 100556996608,
        'totalDebt': 108046999552,
        'totalRevenue': 260174004224,
        'totalCashPerShare': 22.631,
        'financialCurrency': 'USD',
        'maxAge': 86400,
        'revenuePerShare': 56.341,
        'quickRatio': 1.384,
        'recommendationMean': 2.2
    }
}

yahoofinancials's People

Contributors

bjosun avatar flipdazed avatar jecsand avatar sedwards2000 avatar shaunpatterson avatar vkorelsky avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

yahoofinancials's Issues

SystemExit: 1

<ipython-input-5-5c0deba095ce> in get_financial_stats(ticker)
     30 
     31     # Get balance sheet related info
---> 32     financial_statements = yahoo_financials.get_financial_stmts('annual', ['balance', 'income'])
     33     for year in financial_statements['balanceSheetHistory'][ticker]:
     34         df1 = pd.concat([df1, pd.DataFrame(year).T], sort=True)

/anaconda3/lib/python3.6/site-packages/yahoofinancials/__init__.py in get_financial_stmts(self, frequency, statement_type, reformat)
    403             data = {}
    404             for stmt_type in statement_type:
--> 405                 re_data = self._run_financial_stmt(stmt_type, report_num, reformat)
    406                 data.update(re_data)
    407         return data

/anaconda3/lib/python3.6/site-packages/yahoofinancials/__init__.py in _run_financial_stmt(self, statement_type, report_num, reformat)
    390         if reformat:
    391             raw_data = self.get_stock_data(statement_type, report_name=report_name)
--> 392             data = self.get_reformatted_stmt_data(raw_data, statement_type)
    393         else:
    394             data = self.get_stock_data(statement_type, report_name=report_name)

/anaconda3/lib/python3.6/site-packages/yahoofinancials/__init__.py in get_reformatted_stmt_data(self, raw_data, statement_type)
    347         dataType = raw_data['dataType']
    348         if isinstance(self.ticker, str):
--> 349             sub_dict_ent = self._get_sub_dict_ent(self.ticker, raw_data, statement_type)
    350             sub_dict.update(sub_dict_ent)
    351             dict_ent = {dataType: sub_dict}

/anaconda3/lib/python3.6/site-packages/yahoofinancials/__init__.py in _get_sub_dict_ent(self, ticker, raw_data, statement_type)
    317     # Private Method to return subdict entry for the statement reformat process
    318     def _get_sub_dict_ent(self, ticker, raw_data, statement_type):
--> 319         form_data_list = self._reformat_stmt_data_process(raw_data[ticker], statement_type)
    320         return {ticker: form_data_list}
    321 

/anaconda3/lib/python3.6/site-packages/yahoofinancials/__init__.py in _reformat_stmt_data_process(self, raw_data, statement_type)
    299     def _reformat_stmt_data_process(self, raw_data, statement_type):
    300         final_data_list = []
--> 301         stmt_id = self._get_stmt_id(statement_type, raw_data)
    302         hashed_data_list = raw_data[stmt_id]
    303         for data_item in hashed_data_list:

/anaconda3/lib/python3.6/site-packages/yahoofinancials/__init__.py in _get_stmt_id(self, statement_type, raw_data)
    293                 i += 1
    294         if i != 1:
--> 295             sys.exit(1)
    296         return stmt_id
    297 

SystemExit: 1

I haven't seen this one before, any ideas? I'm right now trying to scrape the financial data (in a loop) for all NASDAQ and SP500 tickers, by the way.

Export YahooFinancials data into Excel

Perhaps an easy question, but I am a beginner :-)
How do I convert the data, received by the code below, into Excel

from yahoofinancials import YahooFinancials
import datetime
ticker = 'MCD'
yahoo_financials = YahooFinancials(ticker)
r = yahoo_financials.get_historical_price_data('2015-01-01', str(datetime.date.today()), 'monthly')

terminated symbols do not work

First of all, thanks for your great library. But I ran into a problem today:

I wanted to download historical data for the ticker symbol VRX, which does not work.
This code belonged to Valeant Pharmaceuticals International, Inc. which is now called Bausch Health Companies Inc. (VRX.SW). But the symbol VRX.SW does not give me historical data for the original company.
The data is still there (https://finance.yahoo.com/quote/VRX/history?p=VRX) but the code does not work with your library.

Error occurs in get_net_income()

This is the code (after delete unrelated lines) given in the "Single Ticker Example" of github frontpage.

Code

from yahoofinancials import YahooFinancials

ticker = 'AAPL'
yahoo_financials = YahooFinancials(ticker)

apple_net_income = yahoo_financials.get_net_income()

Output

Traceback (most recent call last):
File "test.py", line 6, in
apple_net_income = yahoo_financials.get_net_income()
File "/export/home/tienyiah/miniconda3/lib/python3.6/site-packages/yahoofinancials/init.py", line 700, in get_net_income
return self._financial_statement_data('income', 'incomeStatementHistory', 'netIncome', 'annual')
File "/export/home/tienyiah/miniconda3/lib/python3.6/site-packages/yahoofinancials/init.py", line 587, in _financial_statement_data
date_key = re_data[self.ticker][0].keys()[0]
TypeError: 'dict_keys' object does not support indexing

Version information:

Python 3.6.5 :: Anaconda, Inc.
yahoofinancials: v1.0

How to use question

Hi there! Here is my project and the challenges I'm facing :)

THE GOAL

End up with a Google Sheet that will do my stocks calculations by being fed automatically by the Python 3 script when I run it. I don't want this to be automatic real-time updates, just want it to update when I run the python script.

THE CHALLENGE

I would like to fetch data from the YahooFinancials python API for a list of stock tickers, but only need a few details from all the pulled data. Once I've been able to select those specific data - if possible - I would like to send them to Google Sheets automatically via the API. So when I run the Python 3 script, everything is automated and returns the data in the Google Sheet.

THE PROGRESSION

The current state of the project is that I can import the stock tickers from an external TXT file and fetch the data from the YahooFinancials python API: https://pypi.org/project/yahoofinancials/

The quotes.txt file

ABBV
T
VOD

The python script.py

from yahoofinancials import YahooFinancials

#To get the tickers list
myquotes = open("quotes.txt","r")
mytickers = myquotes.read().splitlines()

yahoo_financials = YahooFinancials(tickers)

#To fetch the data from the API
summary = yahoo_financials.get_summary_data()
stats = yahoo_financials.get_key_statistics_data()
financial = yahoo_financials.get_financial_stmts('quarterly','balance')

#To build the data set
data_output = {
    'summary': summary,
    'stats': stats,
    'financial': financial
}

#To print the data set
print(data_output)

The output you get from those 3 separate calls is quite a lot of data with 3 repetitions of the stock tickers, nested data under random quarterly dates...etc, so I would like to reconstruct a proper data structure to have a simpler data set at the end.

Let's take the ticker ABBV as an example and here is the output in Python with indentation:

{
  'summary': {
    'ABBV': {
      'previousClose': 80.51,
      'regularMarketOpen': 80.14,
      'twoHundredDayAverage': 88.50618,
      'trailingAnnualDividendYield': 0.049062226,
      'payoutRatio': 0.9809,
      'volume24Hr': None,
      'regularMarketDayHigh': 80.14,
      'navPrice': None,
      'averageDailyVolume10Day': 5402866,
      'totalAssets': None,
      'regularMarketPreviousClose': 80.51,
      'fiftyDayAverage': 82.46147,
      'trailingAnnualDividendRate': 3.95,
      'open': 80.14,
      'averageVolume10days': 5402866,
      'expireDate': '-',
      'yield': None,
      'algorithm': None,
      'dividendRate': 4.28,
      'exDividendDate': '2019-04-12',
      'beta': 0.974589,
      'circulatingSupply': None,
      'startDate': '-',
      'regularMarketDayLow': 78.8,
      'priceHint': 2,
      'currency': 'USD',
      'trailingPE': 21.721312,
      'regularMarketVolume': 1665852,
      'lastMarket': None,
      'maxSupply': None,
      'openInterest': None,
      'marketCap': 119585488896,
      'volumeAllCurrencies': None,
      'strikePrice': None,
      'averageVolume': 6921888,
      'priceToSalesTrailing12Months': 3.651131,
      'dayLow': 78.8,
      'ask': 79.7,
      'ytdReturn': None,
      'askSize': 1100,
      'volume': 1665852,
      'fiftyTwoWeekHigh': 121.45,
      'forwardPE': 8.43054,
      'maxAge': 1,
      'fromCurrency': None,
      'fiveYearAvgDividendYield': 3.27,
      'fiftyTwoWeekLow': 75.77,
      'bid': 79.72,
      'tradeable': True,
      'dividendYield': 0.0533,
      'bidSize': 800,
      'dayHigh': 80.14
    }
  },
  'stats': {
    'ABBV': {
      'annualHoldingsTurnover': None,
      'enterpriseToRevenue': 4.678,
      'beta3Year': None,
      'profitMargins': 0.17363001,
      'enterpriseToEbitda': 10.948,
      '52WeekChange': -0.3192119,
      'morningStarRiskRating': None,
      'forwardEps': 9.43,
      'revenueQuarterlyGrowth': None,
      'sharesOutstanding': 1504220032,
      'fundInceptionDate': '-',
      'annualReportExpenseRatio': None,
      'totalAssets': None,
      'bookValue': -1.942,
      'sharesShort': 25312582,
      'sharesPercentSharesOut': 0.0168,
      'fundFamily': None,
      'lastFiscalYearEnd': 1546214400,
      'heldPercentInstitutions': 0.70872,
      'netIncomeToCommon': 5687000064,
      'trailingEps': 3.66,
      'lastDividendValue': None,
      'SandP52WeekChange': 0.018081188,
      'priceToBook': None,
      'heldPercentInsiders': 0.00075999997,
      'nextFiscalYearEnd': 1609372800,
      'yield': None,
      'mostRecentQuarter': 1546214400,
      'shortRatio': 2.98,
      'sharesShortPreviousMonthDate': '2018-12-31',
      'floatShares': 1502997912,
      'beta': 0.974589,
      'enterpriseValue': 153203523584,
      'priceHint': 4,
      'threeYearAverageReturn': None,
      'lastSplitDate': '-',
      'lastSplitFactor': None,
      'legalType': None,
      'morningStarOverallRating': None,
      'earningsQuarterlyGrowth': None,
      'priceToSalesTrailing12Months': None,
      'dateShortInterest': 1548892800,
      'pegRatio': 0.95,
      'ytdReturn': None,
      'forwardPE': 8.431602,
      'maxAge': 1,
      'lastCapGain': None,
      'shortPercentOfFloat': 0.0182,
      'sharesShortPriorMonth': 29758016,
      'category': None,
      'fiveYearAverageReturn': None
    }
  },
  'financial': {
    'balanceSheetHistoryQuarterly': {
      'ABBV': [{
        '2018-12-31': {
          'totalStockholderEquity': -2921000000,
          'netTangibleAssets': -45264000000
        }
      }, {
        '2018-09-30': {
          'intangibleAssets': 26625000000,
          'capitalSurplus': 14680000000,
          'totalLiab': 69085000000,
          'totalStockholderEquity': -2921000000,
          'otherCurrentLiab': 378000000,
          'totalAssets': 66164000000,
          'commonStock': 18000000,
          'otherCurrentAssets': 112000000,
          'retainedEarnings': 6789000000,
          'otherLiab': 16511000000,
          'goodWill': 15718000000,
          'treasuryStock': -24408000000,
          'otherAssets': 943000000,
          'cash': 8015000000,
          'totalCurrentLiabilities': 15387000000,
          'shortLongTermDebt': 1026000000,
          'otherStockholderEquity': -2559000000,
          'propertyPlantEquipment': 2950000000,
          'totalCurrentAssets': 18465000000,
          'longTermInvestments': 1463000000,
          'netTangibleAssets': -45264000000,
          'shortTermInvestments': 770000000,
          'netReceivables': 5780000000,
          'longTermDebt': 37187000000,
          'inventory': 1786000000,
          'accountsPayable': 10981000000
        }
      }, {
        '2018-06-30': {
          'intangibleAssets': 26903000000,
          'capitalSurplus': 14596000000,
          'totalLiab': 65016000000,
          'totalStockholderEquity': -3375000000,
          'otherCurrentLiab': 350000000,
          'totalAssets': 61641000000,
          'commonStock': 18000000,
          'otherCurrentAssets': 128000000,
          'retainedEarnings': 5495000000,
          'otherLiab': 16576000000,
          'goodWill': 15692000000,
          'treasuryStock': -23484000000,
          'otherAssets': 909000000,
          'cash': 3547000000,
          'totalCurrentLiabilities': 17224000000,
          'shortLongTermDebt': 3026000000,
          'otherStockholderEquity': -2639000000,
          'propertyPlantEquipment': 2787000000,
          'totalCurrentAssets': 13845000000,
          'longTermInvestments': 1505000000,
          'netTangibleAssets': -45970000000,
          'shortTermInvestments': 196000000,
          'netReceivables': 5793000000,
          'longTermDebt': 31216000000,
          'inventory': 1580000000,
          'accountsPayable': 10337000000
        }
      }, {
        '2018-03-31': {
          'intangibleAssets': 27230000000,
          'capitalSurplus': 14519000000,
          'totalLiab': 65789000000,
          'totalStockholderEquity': 3553000000,
          'otherCurrentLiab': 125000000,
          'totalAssets': 69342000000,
          'commonStock': 18000000,
          'otherCurrentAssets': 17000000,
          'retainedEarnings': 4977000000,
          'otherLiab': 17250000000,
          'goodWill': 15880000000,
          'treasuryStock': -15961000000,
          'otherAssets': 903000000,
          'cash': 9007000000,
          'totalCurrentLiabilities': 17058000000,
          'shortLongTermDebt': 6024000000,
          'otherStockholderEquity': -2630000000,
          'propertyPlantEquipment': 2828000000,
          'totalCurrentAssets': 20444000000,
          'longTermInvestments': 2057000000,
          'netTangibleAssets': -39557000000,
          'shortTermInvestments': 467000000,
          'netReceivables': 5841000000,
          'longTermDebt': 31481000000,
          'inventory': 1738000000,
          'accountsPayable': 10542000000
        }
      }]
    }
  }
}

If you take the 3 calls one by one and all that data, those are examples of data I would be interested in getting to reconstruct a smaller and better output.

THE EXPECTED RESULT

#First getting rid of the "summary", "stats" and "financials"keys.

{
  'ABBV': {

    #Data from "summary"

    'previousClose': 80.51,
    'trailingAnnualDividendYield': 0.049062226,
    'trailingAnnualDividendRate': 3.95,

    #Data from "stats"

    'forwardEps': 9.43,
    'bookValue': -1.942,

    #Data from "financial"

    'cash': 8015000000,
    'longTermDebt': 37187000000,

  },

  'T': {

    #Same data

  },

  'VOD': {

    #Same data

  },

}

WHERE I NEED HELP

Issue A - How can I adapt the initial data set to my expected result?

Issue B - As you can see in the financial data, there is a quarterly key with a date in it (2018-12-31) and in the first key, the cash variable I'm interested in isn't available, so the script has to be smart enough to go to the next key and look for the cash variable... and so on. Please note that the cash variable might not be in the second key so the script would have to search until it gets it. Please also note that the quarterly date can be completely random from one ticker to the other. i.e. for ABBV the last quarter can be 2018-12-31 but for T it could be the month before 2018-11-30 so the date value has to be skipped and replace by a key number.

Thanks in advance for your help and let me know if you have any question. Cheers!

20 sma and 100 sma

first thanks for everything!
second..
did you write a method for 20 sma and 100 sma?

Proxy support

It seems like at the moment there is no support for proxies

I got the PR (support for proxies) ready to be merged if you are happy with contribution from the outside :)

Levered Free Cash Flow value missing?

Hello, is there a way to get the "Levered Free Cash Flow" value please? It seems to be missing even tho it's part of the "key statistics" data available on Yahoo Finance.

Getter methods Missing

I saw a similar comment in the ".get_stock_outstanding" thread. Basically, it would be great if we could add some more getter methods to the package. For me, I wrote a local extension to the YahooFinancials class with getters for 1Y performance and short ratio.

Is there any way the community could do pull requests to add more getter methods? This way, we could make some nice bespoke methods and you would have less work :)

-Anthony

feature request to work around yahoo financials dropping dividends

There is a bug in the yahoo data server (not a bug in the yahoofinancials package here) that adversely affect peoples who use this package. I'm not sure where else to document it, and I would like to request a feature for the package that will allow working around the bug.

The bug occurs when one asks for historical data in weekly, monthly, or larger periods. For example, the VBILX fund has regular dividends each month, but if you ask for monthly historical data for more than a few years back, many recent dividends will not be reported. You can see this at the web site e.g. at the following URL:

https://sg.finance.yahoo.com/quote/VBILX/history?period1=1264482000&period2=1548478800&interval=1mo&filter=history&frequency=1mo

Note that If you reduce the time period (e.g. to the last year), or you ask for daily instead of monthly data, the server reports all recent dividends correctly. Apparently, the bug is triggered when more than one dividend occurs within a given period (e.g. two in a month), in which case the yahoo server allocates some dividends to the wrong periods, resulting in recent ones being dropped. (If you look into the data returned by the corresponding call to get_historical_stock_data of this package, you can see that the dividends reported are sometimes reported for the wrong periods, and some dividends are being lost.)

One workaround for those who want historical dividends is simply to request the daily historical data. But this can increase the downloaded data by a factor of 30 or so (since daily prices are downloaded but dividends occur on average less than monthly), making it unwieldy when requesting prices over, say, decades.

My feature request (for working around this bug) is as follows. Please add a method to allow querying historical dividend data (daily) that does not also (internally) download daily price data. (Or is there already some method to do this? I couldn't find one.) Thanks.

Returned JSON difficult to parse

First - thanks for the great tools. I am rather new to Python and am having difficulty parsing the returned json into a dataframe. Is there a plan in the works to do so or can anyone offer assistance?
The json returned appears as one long unstructured dictionary string.

Getting Price data causes error: TypeError: unsupported type for timedelta seconds component: NoneType

I have a list of about 250 stock symbols (e.g. 'AAPL', 'IBM', 'QQQ', ...) and try to fetch price data from Yahoo Finance.

nr_periods = 333

date_N_days_ago = (datetime.now() - timedelta(days = nr_periods)).strftime('%Y-%m-%d')
date_today = datetime.now().strftime('%Y-%m-%d')

yahoo_financials = YahooFinancials(symbols_list)
hist_prices = yahoo_financials.get_historical_price_data(date_N_days_ago, date_today, 'daily')

This code causes the error message:

The problem is that this error message doesn't arise everytime I execute the code but randomly. I couldn't figure out why. Here is the full stack trace

Traceback (most recent call last):
  File "testfile.py", line 273, in <module>
    updatePrices(logger)
  File "testfile.py", line 185, in updatePrices
    hist_prices = yahoo_financials.get_historical_price_data(date_N_days_ago, date_today, 'daily')
  File "/usr/local/lib/python3.5/dist-packages/yahoofinancials/__init__.py", line 547, in get_historical_price_data
    return self.get_stock_data('history', hist_obj=hist_obj)
  File "/usr/local/lib/python3.5/dist-packages/yahoofinancials/__init__.py", line 428, in get_stock_data
    dict_ent = self._create_dict_ent(tick, statement_type, tech_type, report_name, hist_obj)
  File "/usr/local/lib/python3.5/dist-packages/yahoofinancials/__init__.py", line 364, in _create_dict_ent
    cleaned_re_data = self._clean_historical_data(re_data)
  File "/usr/local/lib/python3.5/dist-packages/yahoofinancials/__init__.py", line 270, in _clean_historical_data
    cleaned_date = self.format_date(v)
  File "/usr/local/lib/python3.5/dist-packages/yahoofinancials/__init__.py", line 111, in format_date
    form_date = str((datetime.datetime(1970, 1, 1) + datetime.timedelta(seconds=in_date)).date())
TypeError: unsupported type for timedelta seconds component: NoneType

I've even tried to hardcode the date but receive same error message (randomly).
hist_prices = yahoo_financials.get_historical_price_data('2017-11-20', '2018-10-19', 'daily')

Incorrect labeling of dates while fetching date

I am trying to get the price data for CCL.AX for the last few days in the following day

hsp = yf.get_historical_price_data('2019-10-16', '2019-10-18', 'daily')
{'CCL.AX': {'eventsData': {},
 'firstTradeDate': {'formatted_date': '1988-01-28', 'date': 570398400},
 'currency': 'AUD',
 'instrumentType': 'EQUITY',
 'timeZone': {'gmtOffset': 39600},
 'prices': [{'date': 1571180400,
   'high': 10.680000305175781,
   'low': 10.460000038146973,
   'open': 10.5600004196167,
   'close': 10.630000114440918,
   'volume': 2166314,
   'adjclose': 10.630000114440918,
   'formatted_date': '2019-10-15'},
  {'date': 1571266800,
   'high': 10.619999885559082,
   'low': 10.34000015258789,
   'open': 10.609999656677246,
   'close': 10.359999656677246,
   'volume': 4474727,
   'adjclose': 10.359999656677246,
   'formatted_date': '2019-10-16'},
  {'date': 1571375407,
   'high': 10.569999694824219,
   'low': 10.350000381469727,
   'open': 10.40999984741211,
   'close': 10.390000343322754,
   'volume': 2578999,
   'adjclose': 10.390000343322754,
   'formatted_date': '2019-10-18'}]}}

Notice how the price for Oct 17 is missing and the first trade date listed is . In fact, the price data for Oct 16 is in fact the data corresponding to Oct 17!

What is causing this issues? Ideally I'd like trade days to be dated consistently with how they corresponded on the underlying exchange.

AssertionError running test_yahoofinancials.py (line 93)

test_historical_price line failed the assertDictEqual(single_stock_prices['C']['prices'][0], expect_dict)

single_stock_prices['C']['prices'][0] = {date, high, low, open, close, volume, adjclose, formatted_date}

while....
expect_dict = {high, volume, formatted_date, low, adjclose, date, close, open}

Financial data does not match for Twitter Inc.

was trying to compare twitter to facebook. The financial data matches for facebook but for twitter it does not match with that on Yahoo Finance.

e.g. research and development expenses are quite different

I checked with a few other tickers and it worked for GOOG, FB, CMRE, DAC. Weirdly, it's twitter that is not giving accurate figures.

Stock splits?

Looked through the project and could not find historical stock split data as I could find for dividend. Any ideas on this?

Thanks, wonderful tool

get_historical_price_data returns None for a single close value

For some tickers, get_historical_data returns None for a single close value.

  • Occurs for both pip install and imported yahoofinancials.py
  • Linux
  • Python 3.6.7

Some tickers this occurs for: SGHIX, FSHCX

Example:

from yahoofinancials import YahooFinancials
import datetime

ticker = 'SGHIX'
yahoo_financials = YahooFinancials(ticker)
r = yahoo_financials.get_historical_price_data('1900-01-01', str(datetime.date.today()), 'daily')

s = r[ticker]['prices']
listOfDates = []
listOfCloseValues = []
for i in range(0, len(s), 1):
    listOfDates.append(s[i]['formatted_date'])
    listOfCloseValues.append(s[i]['close'])
listYahoo = [listOfDates, listOfCloseValues]

print(len(listYahoo[0]), 'dates and', len(listYahoo[1]), "close values")

k = 0
for i in listYahoo[1]:
    if i == None:
        print(listYahoo[0][k])
        print(k)
        print('None value found')
    k += 1

It slowed down a bit recently

It used to be much faster to crawl data such as historical daily prices but recently, it slowed down a bit.

For ticker in ticker_list :  # ticker_list has 445 tickers in total
    yahoo_financials = YahooFinancials(ticker)
    historical_data = yahoo_financials.get_historical_price_data('2019-02-01','2019-02-25, 'daily')

For example, the code above took me about 20 minutes to complete.
Why did it slow down so much and can you handle this issue please?

who to contact if there is some missing data

First of all I would like to thank you @JECSand for such a wonderful library.

I have recently noticed that there are some missing data for my counters, and I tried to feedback to yahoo but I was told that yahoo finance API has been disabled (https://forums.yahoo.net/t5/Known-issues-and-updates/Yahoo-Finance-API-disabled/m-p/392760#M14)

From where actually these data are pulled from? Is there any channel to feedback about the missing data? It is quite a thorn for me at the moment.

bug using get_financial_stmts: ValueError: read of closed file

I am using a demo code on the front page:

from yahoofinancials import YahooFinancials

yahoo_financials = YahooFinancials('AAPL')
print(yahoo_financials.get_financial_stmts('annual', 'income'))

and here is the error:
Traceback (most recent call last): File "/Users/eric/Desktop/demp.py", line 6, in <module> print(yahoo_financials.get_financial_stmts('annual', 'income')) File "/usr/local/lib/python3.7/site-packages/yahoofinancials/__init__.py", line 502, in get_financial_stmts data = self._run_financial_stmt(statement_type, report_num, reformat) File "/usr/local/lib/python3.7/site-packages/yahoofinancials/__init__.py", line 492, in _run_financial_stmt raw_data = self.get_stock_data(statement_type, report_name=report_name) File "/usr/local/lib/python3.7/site-packages/yahoofinancials/__init__.py", line 424, in get_stock_data dict_ent = self._create_dict_ent(self.ticker, statement_type, tech_type, report_name, hist_obj) File "/usr/local/lib/python3.7/site-packages/yahoofinancials/__init__.py", line 348, in _create_dict_ent re_data = self._scrape_data(YAHOO_URL, tech_type, statement_type) File "/usr/local/lib/python3.7/site-packages/yahoofinancials/__init__.py", line 135, in _scrape_data response_content = response.read() File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tempfile.py", line 481, in func_wrapper return func(*args, **kwargs) ValueError: read of closed file

SystemExit: 1

<ipython-input-5-5c0deba095ce> in get_financial_stats(ticker)
     30 
     31     # Get balance sheet related info
---> 32     financial_statements = yahoo_financials.get_financial_stmts('annual', ['balance', 'income'])
     33     for year in financial_statements['balanceSheetHistory'][ticker]:
     34         df1 = pd.concat([df1, pd.DataFrame(year).T], sort=True)

/anaconda3/lib/python3.6/site-packages/yahoofinancials/__init__.py in get_financial_stmts(self, frequency, statement_type, reformat)
    403             data = {}
    404             for stmt_type in statement_type:
--> 405                 re_data = self._run_financial_stmt(stmt_type, report_num, reformat)
    406                 data.update(re_data)
    407         return data

/anaconda3/lib/python3.6/site-packages/yahoofinancials/__init__.py in _run_financial_stmt(self, statement_type, report_num, reformat)
    390         if reformat:
    391             raw_data = self.get_stock_data(statement_type, report_name=report_name)
--> 392             data = self.get_reformatted_stmt_data(raw_data, statement_type)
    393         else:
    394             data = self.get_stock_data(statement_type, report_name=report_name)

/anaconda3/lib/python3.6/site-packages/yahoofinancials/__init__.py in get_reformatted_stmt_data(self, raw_data, statement_type)
    347         dataType = raw_data['dataType']
    348         if isinstance(self.ticker, str):
--> 349             sub_dict_ent = self._get_sub_dict_ent(self.ticker, raw_data, statement_type)
    350             sub_dict.update(sub_dict_ent)
    351             dict_ent = {dataType: sub_dict}

/anaconda3/lib/python3.6/site-packages/yahoofinancials/__init__.py in _get_sub_dict_ent(self, ticker, raw_data, statement_type)
    317     # Private Method to return subdict entry for the statement reformat process
    318     def _get_sub_dict_ent(self, ticker, raw_data, statement_type):
--> 319         form_data_list = self._reformat_stmt_data_process(raw_data[ticker], statement_type)
    320         return {ticker: form_data_list}
    321 

/anaconda3/lib/python3.6/site-packages/yahoofinancials/__init__.py in _reformat_stmt_data_process(self, raw_data, statement_type)
    299     def _reformat_stmt_data_process(self, raw_data, statement_type):
    300         final_data_list = []
--> 301         stmt_id = self._get_stmt_id(statement_type, raw_data)
    302         hashed_data_list = raw_data[stmt_id]
    303         for data_item in hashed_data_list:

/anaconda3/lib/python3.6/site-packages/yahoofinancials/__init__.py in _get_stmt_id(self, statement_type, raw_data)
    293                 i += 1
    294         if i != 1:
--> 295             sys.exit(1)
    296         return stmt_id
    297 

SystemExit: 1

I haven't seen this one before, any ideas? I'm right now trying to scrape the financial data (in a loop) for all NASDAQ and SP500 tickers, by the way.

Get list of yahoo ticker symbols from "foreign" IDs, such as ISIN

Yahoo ticker symbols are quite uncommon. In order to find securities systematically one usually uses IDs like ISIN / SEDOL or Bloomberg Tickers. Since yahoo made up it's own representations, it would be more than useful to translate a list of "foreign" IDs (e.g. the ones I mentioned) into yahoo tickers. Does anayone know of an approach to achieve this using this repo or an alternative workaround?

can I get the price change in a day?

I want to get a stock data in a day and the interval would be a minute.
Do this package support this function now?
I haven't found a way to do it.

KeyError: 'QuoteSummaryStore'

First of all, thanks so much for your work creating yahoofinancials!

So, I just started getting this error tonight:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)

<ipython-input-4-0cb5fdcc700b> in get_financial_stats(ticker)
     31 
     32     # Get balance sheet related info
---> 33     financial_statements = yahoo_financials.get_financial_stmts('annual', ['balance', 'income'])
     34     for year in financial_statements['balanceSheetHistory'][ticker]:
     35         df1 = pd.concat([df1, pd.DataFrame(year).T], sort=False)

/anaconda3/lib/python3.6/site-packages/yahoofinancials/__init__.py in get_financial_stmts(self, frequency, statement_type, reformat)
    403             data = {}
    404             for stmt_type in statement_type:
--> 405                 re_data = self._run_financial_stmt(stmt_type, report_num, reformat)
    406                 data.update(re_data)
    407         return data

/anaconda3/lib/python3.6/site-packages/yahoofinancials/__init__.py in _run_financial_stmt(self, statement_type, report_num, reformat)
    389         report_name = self.YAHOO_FINANCIAL_TYPES[statement_type][report_num]
    390         if reformat:
--> 391             raw_data = self.get_stock_data(statement_type, report_name=report_name)
    392             data = self.get_reformatted_stmt_data(raw_data, statement_type)
    393         else:

/anaconda3/lib/python3.6/site-packages/yahoofinancials/__init__.py in get_stock_data(self, statement_type, tech_type, report_name, hist_obj)
    333         else:
    334             for tick in self.ticker:
--> 335                 dict_ent = self._create_dict_ent(tick, statement_type, tech_type, report_name, hist_obj)
    336                 data.update(dict_ent)
    337         return data

/anaconda3/lib/python3.6/site-packages/yahoofinancials/__init__.py in _create_dict_ent(self, ticker, statement_type, tech_type, report_name, hist_obj)
    272                     up_ticker
    273         if tech_type == '' and statement_type != 'history':
--> 274             re_data = self._scrape_data(YAHOO_URL, tech_type, statement_type)
    275             dict_ent = {up_ticker: re_data[u'' + report_name], 'dataType': report_name}
    276         elif tech_type != '' and statement_type != 'history':

/anaconda3/lib/python3.6/site-packages/yahoofinancials/__init__.py in _scrape_data(url, tech_type, statement_type)
    115         data = loads(re.search("root.App.main\s+=\s+(\{.*\})", script).group(1))
    116         if tech_type == '' and statement_type != 'history':
--> 117             stores = data["context"]["dispatcher"]["stores"]["QuoteSummaryStore"]
    118         elif tech_type != '' and statement_type != 'history':
    119             stores = data["context"]["dispatcher"]["stores"]["QuoteSummaryStore"][tech_type]

KeyError: 'QuoteSummaryStore'

Thanks for any ideas of how it might be fixed!

.get_stock_outstanding

Hey JECSand,
could you also write a function to retrieve the number of shares outstanding? - or am I missing that it already exists? -
No. of shares is necessary for me to link the statement data to the price data .. I already tried to work around with EPS but this seems error-prone.

Btw. thanks for the help in stackoverflow ;-)

Missing intangibles in returned data set

Looking at the Cash Flow statement I note the following -
Operating activities -
Depreciation is understated by the amount of intangibles/or intangible amortization not included.
Investing Activities -
Investments in intangible assets not included in any shown categories.

OSError: [Errno socket error] [SSL: UNKNOWN_PROTOCOL] unknown protocol (_ssl.c:841)

Ive got this error when trying extract data

python demo.py WFC C BAC


Traceback (most recent call last):
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\urllib\request.py", line 1757, in open
return getattr(self, name)(url)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\urllib\request.py", line 1967, in open_https
return self._open_generic_http(self._https_connection, url, data)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\urllib\request.py", line 1916, in _open_generic_http
http_conn.request("GET", selector, headers=headers)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\http\client.py", line 1239, in request
self._send_request(method, url, body, headers, encode_chunked)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\http\client.py", line 1285, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\http\client.py", line 1234, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\http\client.py", line 1026, in _send_output
self.send(msg)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\http\client.py", line 964, in send
self.connect()
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\http\client.py", line 1400, in connect
server_hostname=server_hostname)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\ssl.py", line 407, in wrap_socket
_context=self, _session=session)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\ssl.py", line 814, in init
self.do_handshake()
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\ssl.py", line 1068, in do_handshake
self._sslobj.do_handshake()
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\ssl.py", line 689, in do_handshake
self._sslobj.do_handshake()
ssl.SSLError: [SSL: UNKNOWN_PROTOCOL] unknown protocol (_ssl.c:841)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "demo.py", line 87, in
timeit(defaultapi, ts[0] if 1 == len(ts) else ts)
File "demo.py", line 68, in timeit
f(*args)
File "demo.py", line 28, in defaultapi
print(tick.get_summary_data())
File "C:\Users\XXXX\Documents\Projekty\Financials\yahoofinancials\yahoofinancials_init_.py", line 624, in get_summary_data
return self.get_clean_data(self.get_stock_tech_data('summaryDetail'), 'summaryDetail')
File "C:\Users\XXXX\Documents\Projekty\Financials\yahoofinancials\yahoofinancials_init_.py", line 491, in get_stock_tech_data
return self.get_stock_data(tech_type=tech_type)
File "C:\Users\XXXX\Documents\Projekty\Financials\yahoofinancials\yahoofinancials_init_.py", line 478, in get_stock_data
dict_ent = self.create_dict_ent(tick, statement_type, tech_type, report_name, hist_obj)
File "C:\Users\XXXX\Documents\Projekty\Financials\yahoofinancials\yahoofinancials_init
.py", line 406, in _create_dict_ent
re_data = self.scrape_data(YAHOO_URL, tech_type, statement_type)
File "C:\Users\XXXX\Documents\Projekty\Financials\yahoofinancials\yahoofinancials_init
.py", line 144, in _scrape_data
response = urlopener.open(url)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\urllib\request.py", line 1763, in open
raise OSError('socket error', msg).with_traceback(sys.exc_info()[2])
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\urllib\request.py", line 1757, in open
return getattr(self, name)(url)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\urllib\request.py", line 1967, in open_https
return self._open_generic_http(self._https_connection, url, data)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\urllib\request.py", line 1916, in _open_generic_http
http_conn.request("GET", selector, headers=headers)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\http\client.py", line 1239, in request
self._send_request(method, url, body, headers, encode_chunked)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\http\client.py", line 1285, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\http\client.py", line 1234, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\http\client.py", line 1026, in _send_output
self.send(msg)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\http\client.py", line 964, in send
self.connect()
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\http\client.py", line 1400, in connect
server_hostname=server_hostname)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\ssl.py", line 407, in wrap_socket
_context=self, _session=session)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\ssl.py", line 814, in init
self.do_handshake()
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\ssl.py", line 1068, in do_handshake
self._sslobj.do_handshake()
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\ssl.py", line 689, in do_handshake
self._sslobj.do_handshake()
OSError: [Errno socket error] [SSL: UNKNOWN_PROTOCOL] unknown protocol (_ssl.c:841)

Using stock symbols with '&' characters

Some of the stocks traded on NSE have '&' sign for example:
GET&D.NS
IL&FSTRANS.NS
J&KBANK.NS
L&TFH.NS
M&MFIN.NS
M&M.NS
I have tried escaping the character using a '' but this has not worked.

Is there a workaround for such symbols?

Thanks

ImportError: cannot import name 'YahooFinancials'

I have been importing values from YahooFinancials via from yahoofinancials import YahooFinancials. Up until today, when this error showed up - ImportError: cannot import name 'YahooFinancials'. Not sure if there's any updates being done on the backend thats causing this error? Thanks!

Summary data for ETF

I noticed that the data returned by get_summary_data() is incomplete for ETF.

For example: the yield and the expense ratio for VHYL.MI arepresent on yahoo finance, but don't get returned by get_summary_data(); other methods like get_dividend_rate() or get_annual_avg_div_rate() or get_exdividend_date() are affected too.

get currency denomination for statements

Currently the get_financial_stmts() function has no way to let someone know what currency is being used for reporting. Yahoo finance sometimes reports financial statements in USD, CNY, RUB, and other currencies.

It would be a nice feature to also return what currency the income/cash/balance statements are reported in.

image

Relevant html snippet:

<span data-reactid="27">
<!-- react-text: 28 -->Currency in CNY. <!-- /react-text -->
<span data-reactid="29">All numbers in thousands</span>
</span>

It would be nice to include the denomination in the returned results.

e.g.,

{
'incomeStatementHistoryQuarterly': {
    'TICKER':  [ 
       {'2019-03-31': {
          'researchDevelopment': 30383000000,
          'effectOfAccountingCharges': None,
          'incomeBeforeTax': 1146941338000,
          'minorityInterest': 3299552533000,
            ....,
          'reportingCurrency': CNY }
       {'2018-12-31': {  ... } },
        ...
],
    'TICKER2': ...
 ...
}

Mapping of balance sheet fields

Hi,

I was wondering whether you have an exact mapping of balance sheet / cashflow statement / income statement fields to variable names. Most of them are easy to deduce, like "Total Assets" to "totalAssets" but some are less and would like to be exact.

Thanks for any advice!

ESG data

Hello, great module! Is it possible to get data from Sustainability tab, please? Thank you

ETF's stopped working

VXUS and VTI do not retrieve, and are reported failing with a 502.

edit: They worked on Friday. Also fails for mutual funds such as VIIIX

Attempting the same URL using 'curl' from the commandline gets a 302 redirect, and following that redirect returns the data.

and FYI, the retries are excessive. It takes a very, very long time. I put a commit on the top of my master branch that seems to help by differentiating on and caching the failure. (You can take it if you are interested, but I'm not ready to do a PR yet for it.)

Add Sector and Industry to get_stock_quote_type_data?

I am really enjoying the package and it works as designed.
This is a request for a feature - I would like to suggest that having a sector and industry for a stock would be a great addition to the method - say get_stock_quote_type_data?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.