How to use question
See original GitHub issueHi 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!
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (2 by maintainers)
Found a way to do what I wanted. Thanks for your help 😃
I’ve been hiring a developper to do it, but it doesn’t seem possible to retrieve all the key_stats data… this API seems to be a dead end. If you find a solution, please let me know as well 😃