Package: yahoo-fin-api
Yahoo Financials APILightweight SDK for interacting with the Yahoo Financials API. This is more geared on towards the three main financial records:balance sheetincome statementcash flowHow to install?pip3installyahoo-fin-apiExamples1. Get the data from the Yahoo Financials APIfromyahoo_apiimportClientdefmain():client=Client(cache_response=True,# Should cache the response after fetching from APIinput_csv_file="./examples/freetrade_universe.csv",# Load the universe from this filedownload_folder_path="./data")# Cache the data from the API in this folderclient.get_symbols()if__name__=="__main__":main()2. Load the balance sheet modelsfromyahoo_apiimportYahooFinApi,Clientfrompprintimportpprintdefmain():yf=YahooFinApi(Client(cache_response=True,input_csv_file="./examples/freetrade_universe.csv",download_folder_path="./data"))bs=yf.get_balance_sheets(["AAPL","TSLA"])pprint(bs)if__name__=="__main__":main()3. Load the cash flow modelsfromyahoo_apiimportYahooFinApi,Clientfrompprintimportpprintdefmain():yf=YahooFinApi(Client(cache_response=True,input_csv_file="./examples/freetrade_universe.csv",download_folder_path="./data"))cf=yf.get_cashflow_statements(["AAPL","TSLA"])pprint(cf)if__name__=="__main__":main()4. Load the income statement modelfromyahoo_apiimportYahooFinApi,Clientfrompprintimportpprintdefmain():yf=YahooFinApi(Client(cache_response=True,input_csv_file="./examples/freetrade_universe.csv",download_folder_path="./data"))i_stmts=yf.get_income_statements(["AAPL","TSLA"])pprint(i_stmts)if__name__=="__main__":main()5. Load financial data modelfromyahoo_apiimportYahooFinApi,Clientfrompprintimportpprintdefmain():yf=YahooFinApi(Client(cache_response=True,input_csv_file="./examples/freetrade_universe.csv",download_folder_path="./data"))cf=yf.get_financial_data(["AAPL","TSLA"])pprint(cf)if__name__=="__main__":main()6. Load summary detail modelfromyahoo_apiimportYahooFinApi,Clientfrompprintimportpprintdefmain():yf=YahooFinApi(Client(cache_response=True,input_csv_file="./examples/freetrade_universe.csv",download_folder_path="./data"))sd=yf.get_summary_detail(["AAPL","TSLA"])pprint(sd)if__name__=="__main__":main()7. Get historic quotesfromyahoo_fin_apiimportYahooFinApi,Client,FileCachefrompprintimportpprintdefmain():yf=YahooFinApi(Client(quote_cache=FileCache("./data/quotes","quote")))quotes=yf.get_quote("AAPL","max")pprint(quotes)if__name__=="__main__":main()Example of stock valuation based on 10 capfromyahoo_apiimportYahooFinApi,Clientfromyahoo_api.universeimportsymbolsasget_symbolsfrompprintimportpprintBILLION=1000000000defmain():yf=YahooFinApi(Client(cache_response=True,download_folder_path="./data"))symbols=[s.symbolforsinget_symbols("./examples/freetrade_universe.csv")ifs.isa_eligible==Trueands.plus_only==False]tickers=yf.get_all(symbols)res=[]fortintickers:fin_data=t.financial_datasummary=t.summary_detailiffin_dataisNoneorsummaryisNone:continueiffin_data.free_cash_flowisNoneorsummary.market_capisNone:continueiffin_data.free_cash_flow<0:continueifsummary.market_cap<5*BILLION:continuecap_rate=fin_data.free_cash_flow/summary.market_cap*100ifcap_rate>10:res.append({"symbol":t.symbol,"cap_rate":cap_rate,"market_cap":summary.market_cap,"fcf":fin_data.free_cash_flow,"current_price":fin_data.current_price})res=sorted(res,key=lambdar:r["cap_rate"],reverse=True)pprint(res)if__name__=="__main__":main()Build your own cache systemTo implement your own cache system, just create a class that implements this interface and pass it to the client.You can use your custom cache as eithersymbol_cacheorquote_cache, by passing it as param to the init method of theClientclassBaseCache:defis_cached(self,symbol:str)->bool:raiseException("method 'is_cached' not implemented")deffrom_cache(self,symbol:str)->dict:raiseException("method 'from_cache' not implemented")defto_cache(self,symbol:str,body:dict)->None:raiseException("method 'to_cache' not implemented")defclear_cache(self,symbol:str)->bool:raiseException("method 'clear_cache' not implemented")Have a look at theFileCache(/yahoo_fin_api/cache/file_cache.py) as an example.