dpguthrie / yahooquery

Python wrapper for an unofficial Yahoo Finance API
https://yahooquery.dpguthrie.com
MIT License
786 stars 139 forks source link

balance_sheet ignoring 'trailing=True' (while cash_flow and income_statement do take it into account). #150

Open galashour opened 1 year ago

galashour commented 1 year ago

Describe the bug I assume this is a bug rather than a feature request.

It seems that while when adding 'trailing=True' for cash_flow and income_statement at the TTM row, for balance_sheet it pretty much seems to ignore it (and the alternative is to have 2 calls, the second one being balance_sheet with frequency='q' in order to calculate manually the TTM line. Not sure if this is a bug (ignoring the 'trailing=True') or a feature request, but I think it would be good (if not too hard) to make it consistent with the other reports and 'save' the need for the manual calculation after issuing a second call for the quarterly data.

yq = Ticker(ticker)
yq_cashflow = yq.cash_flow(trailing=True)
yq_income = yq.income_statement(trailing=True)
yq_balance = yq.balance_sheet(trailing=True)

Desktop (please complete the following information): I used Windws11, Python 3.8, yahooquery.version = 2.2.15

dpguthrie commented 1 year ago

Thanks for raising this. This is a bug in the sense that trailing shouldn't be accepted as an argument for balance_sheet - there's no meaning for a trailing twelve month (TTM) number for a balance sheet as it's simply a point-in-time reference. Take a look at YF for some examples:

image

dpguthrie commented 1 year ago

The fix (I think) should be something like this:

def balance_sheet(self, frequency='a'):
    return self._financials('balance_sheet', frequency)
galashour commented 1 year ago

Absolutely agree. In fact I indeed verified in my code that I was just extracting the relevant value from the last quarter and append it to the data I get from the annual balance sheet (to visualize longerterm trend but including the most recent value in case the last annual one is 'older' than the recent quarter).

Thanks !

kushalnl7 commented 1 year ago

Hi, I am interested to do this. Can I try this?

galashour commented 1 year ago

Hi, I am interested to do this. Can I try this?

Please clarify the question (was not sure who the question is aimed at, and what you want to try).

If the question was for me, and you wanted to look at the trend of a specific 'aspect/attribute', you can do it already like the below which will create a dict for the specific attribute and later you can visualize as applicable.

    yq = Ticker(ticker)

    yq_balance = yq.balance_sheet()
    yq_balance.reset_index(level=[0], inplace=True)
    yq_balance_q = yq.balance_sheet(frequency='q')
    yq_balance_q.reset_index(level=[0], inplace=True)

    # total_liabilities trend data
    data_field = 'TotalLiabilitiesNetMinorityInterest'
    yq_data_df = yq_balance[['asOfDate', data_field]]
    yq_data_df.rename(columns={'asOfDate': 'date', data_field: 'yq_data'}, inplace=True)
    yq_data_df['date'] = yq_data_df['date'].dt.strftime('%Y-%m-%d')
    yq_data_df.set_index('date', inplace=True)
    last_date = yq_data_df.index[-1]
    trend_data = yq_data_df.to_dict()['yq_data']

For attributes that are not in the balance_sheet, it is even slightly easier, since you don't need the quarterly report and you can just add the 'trailing=True' as parameter for the annual report to get also the data for the most recent 12 months.

The original thread here was just to raise the question whether 'trailing=True' should be reported as a warning, or instead be allowed, resulting with a row from the most recent quarter added (i.e., mrq instead of TTM, in the case mrq is not included in the last raw of the annual report).

dpguthrie commented 1 year ago

@kushalnl7 It's all yours! And, just to be clear, the fix is really just removing the trailing argument from the method as it doesn't actually do anything.

galashour commented 1 year ago

btw, not sure if to open a different issue (not a real issue for me, but could be helpful to others):

when doing pip install yahooquery

It still stays on 2.2.15

In order to update to 2.3.0, I did the following: pip install yahooquery --upgrade --no-cache-dir

Maybe it would be good to edit the 'installation' section in https://yahooquery.dpguthrie.com/