Skip to content

Instantly share code, notes, and snippets.

@janusson
Created December 8, 2024 03:53
Show Gist options
  • Save janusson/f2425a6f1966a607e5a1731925d215e6 to your computer and use it in GitHub Desktop.
Save janusson/f2425a6f1966a607e5a1731925d215e6 to your computer and use it in GitHub Desktop.
This Python script utilizes the yfinance library to collect and manage various types of financial and stock market data for a given ticker symbol from Yahoo Finance. The script provides functionalities to fetch and store company information, news, options, earnings dates, recommendations, holders, financial statements, and historical price data.…
import logging
import yfinance as yf
import json
import csv
import os
import pandas as pd
# Setup logging configuration
logging.basicConfig(filename='security_data_collector.log', level=logging.ERROR,
format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger()
# Security data collector class definition
class SecurityDataCollector:
"""SecurityDataCollector collects information from a provided Ticker symbol from Yahoo Finance.
"""
def __init__(self, ticker):
self.ticker = ticker # provided ticker symbol
self.yf_ticker = yf.Ticker(ticker) # create ticker object
def get_company_name(self) -> str:
"""Fetch company name from Yahoo Finance info.
Returns short name by default and long name if no short name available.
Returns:
str: Name of the company.
"""
try:
self.company_name = self.yf_ticker.info['shortName']
except KeyError as e:
try:
logger.error(f'No company shortName for {self.ticker}: {e}')
self.company_name = self.yf_ticker.info['longName']
except KeyError as e:
logger.error(f'No company longName for {self.ticker}: {e}')
return self.company_name
def get_company_info(self) -> dict:
"""Fetch company information from Yahoo Finance.
Returns:
dict: Dictionary of company information.
"""
self.company_info = self.yf_ticker.info
return self.company_info
def download_info(self):
"""Download company info as a JSON file.
"""
os.makedirs('./data/info', exist_ok=True)
with open(f'./data/info/{self.get_company_name()} summary.json', 'w') as outfile:
json.dump(self.get_company_info(), outfile, indent=4)
def get_news(self) -> list:
"""Return list of company news from Yahoo Finance.
Returns:
list: List of dicts of company news.
"""
try:
self.news = self.yf_ticker.news
except Exception as e:
logger.error(f'Error retrieving news for {self.ticker}: {e}')
return self.news
def get_options(self):
"""Return options information.
"""
try:
self.options = self.yf_ticker.options
except Exception as e:
logger.error(f'Error retrieving options for {self.ticker}: {e}')
def get_earnings_dates(self):
"""Return earnings release dates.
"""
try:
self.earnings_dates = self.yf_ticker.earnings_dates
except Exception as e:
logger.error(f'Error retrieving earnings for {self.ticker}: {e}')
def get_reccomendations(self):
"""Return analyst reccomendations.
"""
try:
self.reccomendations = self.yf_ticker.recommendations
self.reccomendations_summary = self.yf_ticker.recommendations_summary
self.upgrades_downgrades = self.yf_ticker.upgrades_downgrades
except Exception as e:
logger.error(
f'Error retrieving recomendations for {self.ticker}: {e}')
def get_holders(self):
"""Return institutional, mutual fund, and insider holders.
"""
try:
self.majour_holders = self.yf_ticker.major_holders
self.inst_holders = self.yf_ticker.institutional_holders
self.mutualfund_holders = self.yf_ticker.mutualfund_holders
self.insider_purchases = self.yf_ticker.insider_purchases
self.insider_roster_holders = self.yf_ticker.insider_roster_holders
except Exception as e:
logger.error(f'Error retrieving holders of {self.ticker}: {e}')
def get_financials(self) -> pd.DataFrame:
"""Fetch financial data from Yahoo Finance and return as a DataFrame.
Returns:
pd.DataFrame: Financial statements of the company.
"""
try:
self.financial_data = {
'Income Statement': self.yf_ticker.financials,
'Quarterly Income Statement': self.yf_ticker.quarterly_financials,
'Balance Sheet': self.yf_ticker.balance_sheet,
'Quarterly Balance Sheet': self.yf_ticker.quarterly_balance_sheet,
'Cash Flow': self.yf_ticker.cashflow,
'Quarterly Cash Flow': self.yf_ticker.quarterly_cashflow
}
except Exception as e:
logger.error(f'Error retrieving financials for {self.ticker}: {e}')
self.financial_data = {}
return pd.concat(self.financial_data, axis=1) if self.financial_data else pd.DataFrame()
def get_actions(self):
"""Return security actions such as dividends and splits.
"""
try:
self.actions = self.yf_ticker.actions
self.dividends = self.yf_ticker.dividends
self.splits = self.yf_ticker.splits
self.capital_gains = self.yf_ticker.capital_gains # funds only
self.shares_hist = self.yf_ticker.get_shares_full() # historical share count
except Exception as e:
logger.error(f'Error retrieving actions for {self.ticker}: {e}')
def get_summary(self) -> dict:
"""Retrieves and returns a dictionary of key company information.
Returns:
dict: Summary of key company information.
"""
# Get summary of key company information
summary_keys = ['longName', 'country', 'industry', 'sector', 'overallRisk', 'dividendYield', 'previousClose', 'payoutRatio', 'currency', 'forwardPE', 'volume', 'marketCap', 'priceToBook', 'forwardEps', 'pegRatio', 'symbol', 'currentPrice',
'recommendationMean', 'debtToEquity', 'revenuePerShare', 'returnOnAssets', 'returnOnEquity', 'freeCashflow', 'operatingCashflow', 'earningsGrowth', 'revenueGrowth', 'grossMargins', 'ebitdaMargins', 'operatingMargins', 'financialCurrency', 'trailingPegRatio']
summary_dict = {key: self.company_info.get(
key) for key in summary_keys}
return summary_dict
def download_price_hist(self):
"""Download ticker closing price history as a CSV file.
"""
self.yf_ticker.history(period="max").to_csv(
f'./data/price_hist/{self.ticker}_hist.csv')
# Example usage
if __name__ == "__main__":
ticker_symbol = "MSFT" # Microsoft Corporation
collector = SecurityDataCollector(ticker_symbol)
# Fetch and print company name
company_name = collector.get_company_name()
print(f"Company Name: {company_name}")
# Fetch and print company info
company_info = collector.get_company_info()
print("Company Info:", company_info)
# Download company info to JSON
collector.download_info()
# Fetch and print news
news = collector.get_news()
print("News:", news)
# Fetch and print financials
financials = collector.get_financials()
print("Financials:", financials)
# Download price history
collector.download_price_hist()
@janusson
Copy link
Author

janusson commented Dec 8, 2024

Security Data Collector Description

This Python script utilizes the yfinance library to collect and manage various types of financial and stock market data for a given ticker symbol from Yahoo Finance. The script provides functionalities to fetch and store company information, news, options, earnings dates, recommendations, holders, financial statements, and historical price data. It also includes error handling for robust data retrieval. The collected data can be exported in JSON and CSV formats for further analysis.

security_data_collector.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment