JECSand / yahoofinancials

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.
https://pypi.python.org/pypi/yahoofinancials
MIT License
896 stars 214 forks source link

Annual Balance Sheet - Date issue #136

Closed promexio closed 1 year ago

promexio commented 1 year ago

Hi: My script has a bug on this part: The casflow statement works with the following code, but the balancesheet ends with an error.

 try:
        cashflowStatement = yahoo_financials.get_financial_stmts('annual', 'cash')['cashflowStatementHistory'][ticker][4][last_quarter_reported]
    except:
        cashflowStatement = ("Error on " + ticker)
        print(cashflowStatement)
        quarter_dates.remove(last_quarter_reported)
        ticker_name.remove(tickerName)
        ticker_codes.remove(ticker)
        continue
    json_cashFlow.append(cashflowStatement)

    balanceSheet = yahoo_financials.get_financial_stmts('annual', 'balance')['balanceSheetHistory'][ticker][0][last_quarter_reported]
    json_balanceSheet.append(balanceSheet)

Error: line 126, in balanceSheet = yahoo_financials.get_financial_stmts('annual', 'balance')['balanceSheetHistory'][ticker][0][last_quarter_reported] KeyError: '2022-09-30'

Do you have any idea what might be my mistake? Thx a lot in advance.

JECSand commented 1 year ago

@promexio I don’t think it’s a mistake on your end. We recently refactored to get around changes on yahoo’s end. I’ll get this fixed by tomorrow. Thanks for reporting the issue!

JECSand commented 1 year ago

I'm running some tests and here's what I found:

When I run:

from yahoofinancials import YahooFinancials
ticker = 'AAPL'
yahoo_financials = YahooFinancials(ticker)
balanceSheet = yahoo_financials.get_financial_stmts('annual', 'balance')['balanceSheetHistory'][ticker][0]
print(balanceSheet)

I get:

{'2019-09-30': {'cashAndCashEquivalents': 48844000000.0, 'stockholdersEquity': 90488000000.0, 'machineryFurnitureEquipment': 69797000000.0, 'properties': 0.0, 'commonStockEquity': 90488000000.0, 'investmentsAndAdvances': 105341000000.0, 'availableForSaleSecurities': 105341000000.0, 'landAndImprovements': 17085000000.0, 'investmentinFinancialAssets': 105341000000.0, 'currentDeferredRevenue': 5522000000.0, 'totalCapitalization': 182295000000.0, 'capitalStock': 45174000000.0, 'otherInvestments': 105341000000.0, 'accountsReceivable': 22926000000.0, 'shareIssued': 17772944000.0, 'payablesAndAccruedExpenses': 46236000000.0, 'totalNonCurrentLiabilitiesNetMinorityInterest': 142310000000.0, 'otherCurrentBorrowings': 10260000000.0, 'cashFinancial': 12204000000.0, 'longTermDebtAndCapitalLeaseObligation': 91807000000.0, 'otherShortTermInvestments': 51713000000.0, 'cashCashEquivalentsAndShortTermInvestments': 100557000000.0, 'receivables': 45804000000.0, 'inventory': 4106000000.0, 'accumulatedDepreciation': -58579000000.0, 'currentLiabilities': 105718000000.0, 'totalDebt': 108047000000.0, 'netPPE': 37378000000.0, 'tangibleBookValue': 90488000000.0, 'leases': 9075000000.0, 'otherNonCurrentAssets': 32978000000.0, 'currentAssets': 162819000000.0, 'otherNonCurrentLiabilities': 20958000000.0, 'otherCurrentLiabilities': 37720000000.0, 'currentDeferredLiabilities': 5522000000.0, 'workingCapital': 57101000000.0, 'totalLiabilitiesNetMinorityInterest': 248028000000.0, 'tradeandOtherPayablesNonCurrent': 29545000000.0, 'currentDebtAndCapitalLeaseObligation': 16240000000.0, 'retainedEarnings': 45898000000.0, 'commonStock': 45174000000.0, 'gainsLossesNotAffectingRetainedEarnings': -584000000.0, 'totalEquityGrossMinorityInterest': 90488000000.0, 'otherCurrentAssets': 12352000000.0, 'payables': 46236000000.0, 'accountsPayable': 46236000000.0, 'commercialPaper': 5980000000.0, 'longTermDebt': 91807000000.0, 'investedCapital': 198535000000.0, 'currentDebt': 16240000000.0, 'netTangibleAssets': 90488000000.0, 'totalAssets': 338516000000.0, 'ordinarySharesNumber': 17772944000.0, 'otherReceivables': 22878000000.0, 'cashEquivalents': 36640000000.0, 'grossPPE': 95957000000.0, 'totalNonCurrentAssets': 175697000000.0, 'netDebt': 59203000000.0}}

However when I run:

from yahoofinancials import YahooFinancials
ticker = 'AAPL'
yahoo_financials = YahooFinancials(ticker)
balanceSheet = yahoo_financials.get_financial_stmts('annual', 'balance')['balanceSheetHistory'][ticker][3]
print(balanceSheet)

I get: {'2022-09-30': {'cashAndCashEquivalents': 23646000000.0, 'stockholdersEquity': 50672000000.0, 'machineryFurnitureEquipment': 81060000000.0, 'properties': 0.0, 'commonStockEquity': 50672000000.0, 'investmentsAndAdvances': 120805000000.0, 'availableForSaleSecurities': 120805000000.0, 'landAndImprovements': 22126000000.0, 'investmentinFinancialAssets': 120805000000.0, 'currentDeferredRevenue': 7912000000.0, 'totalCapitalization': 149631000000.0, 'capitalStock': 64849000000.0, 'otherInvestments': 120805000000.0, 'accountsReceivable': 28184000000.0, 'shareIssued': 15943425000.0, 'payablesAndAccruedExpenses': 64115000000.0, 'totalNonCurrentLiabilitiesNetMinorityInterest': 148101000000.0, 'otherCurrentBorrowings': 11128000000.0, 'cashFinancial': 18546000000.0, 'longTermDebtAndCapitalLeaseObligation': 98959000000.0, 'otherShortTermInvestments': 24658000000.0, 'cashCashEquivalentsAndShortTermInvestments': 48304000000.0, 'receivables': 60932000000.0, 'inventory': 4946000000.0, 'accumulatedDepreciation': -72340000000.0, 'currentLiabilities': 153982000000.0, 'totalDebt': 120069000000.0, 'netPPE': 42117000000.0, 'tangibleBookValue': 50672000000.0, 'leases': 11271000000.0, 'currentAssets': 135405000000.0, 'otherNonCurrentAssets': 54428000000.0, 'otherNonCurrentLiabilities': 32485000000.0, 'otherCurrentLiabilities': 60845000000.0, 'currentDeferredLiabilities': 7912000000.0, 'workingCapital': -18577000000.0, 'totalLiabilitiesNetMinorityInterest': 302083000000.0, 'tradeandOtherPayablesNonCurrent': 16657000000.0, 'currentDebtAndCapitalLeaseObligation': 21110000000.0, 'retainedEarnings': -3068000000.0, 'commonStock': 64849000000.0, 'gainsLossesNotAffectingRetainedEarnings': -11109000000.0, 'totalEquityGrossMinorityInterest': 50672000000.0, 'otherCurrentAssets': 21223000000.0, 'payables': 64115000000.0, 'accountsPayable': 64115000000.0, 'commercialPaper': 9982000000.0, 'longTermDebt': 98959000000.0, 'investedCapital': 170741000000.0, 'currentDebt': 21110000000.0, 'netTangibleAssets': 50672000000.0, 'totalAssets': 352755000000.0, 'ordinarySharesNumber': 15943425000.0, 'otherReceivables': 32748000000.0, 'cashEquivalents': 5100000000.0, 'grossPPE': 114457000000.0, 'totalNonCurrentAssets': 217350000000.0, 'netDebt': 96423000000.0}}

So I'm thinking the issue is: balanceSheet = yahoo_financials.get_financial_stmts('annual', 'balance')['balanceSheetHistory'][ticker][0] Should be: balanceSheet = yahoo_financials.get_financial_stmts('annual', 'balance')['balanceSheetHistory'][ticker][3]

promexio commented 1 year ago

Thx for your decent tests. You're right. Only, I don't understand, why it's working for the P&L and the CF ... also, if I look to the datasheets on the yahoo finance website, the data presentation seems to be slightly different for the balance sheet, like very bizarre. I'll adapt my script.

promexio commented 1 year ago

on the CF-statement I need to put 4: cashflowStatement = yahoo_financials.get_financial_stmts('annual', 'cash')['cashflowStatementHistory'][ticker][4][last_quarter_reported]

And the big question is: is that always true or do I need more "intelligence code" in the background...?