yf_docs = """ # YFinance Documentation ## Single Ticker Usage ```python import yfinance as yf ticker = yf.Ticker('MSFT') ``` ## Multi-Ticker Usage ### Initialization ```python import yfinance as yf tickers = yf.Tickers('MSFT AAPL GOOG') ``` ### Download Historical Data Get historical market data for multiple tickers: ```python # Method 1: Using Tickers object tickers = yf.Tickers('MSFT AAPL GOOG') data = tickers.download(period='1mo') # Method 2: Using download function directly data = yf.download(['MSFT', 'AAPL', 'GOOG'], period='1mo') ``` Returns: ```python DataFrame with columns: [('Close', 'AAPL'), ('Close', 'GOOG'), ('Close', 'MSFT'), ('Dividends', 'AAPL'), ('Dividends', 'GOOG'), ('Dividends', 'MSFT'), ('High', 'AAPL'), ('High', 'GOOG'), ('High', 'MSFT'), ('Low', 'AAPL'), ('Low', 'GOOG'), ('Low', 'MSFT'), ('Open', 'AAPL'), ('Open', 'GOOG'), ('Open', 'MSFT'), ('Stock Splits', 'AAPL'), ('Stock Splits', 'GOOG'), ('Stock Splits', 'MSFT'), ('Volume', 'AAPL'), ('Volume', 'GOOG'), ('Volume', 'MSFT')] Shape: (21, 21) ``` ### News Get news for multiple tickers: ```python tickers = yf.Tickers('MSFT AAPL GOOG') news = tickers.news() ``` Returns: ```python - MSFT: list - AAPL: list - GOOG: list ``` ### Accessing Individual Tickers Access individual ticker data from a multi-ticker object: ```python tickers = yf.Tickers('MSFT AAPL GOOG') msft_info = tickers.tickers['MSFT'].info aapl_history = tickers.tickers['AAPL'].history(period='1mo') ``` ## Single Ticker Methods ### Info ```python ticker.info ``` Returns: ```python - address1: str - city: str - state: str - zip: str - country: str - phone: str - website: str - industry: str - industryKey: str - industryDisp: str - sector: str - sectorKey: str - sectorDisp: str - longBusinessSummary: str - fullTimeEmployees: int - companyOfficers: list - auditRisk: int - boardRisk: int - compensationRisk: int - shareHolderRightsRisk: int - overallRisk: int - governanceEpochDate: int - compensationAsOfEpochDate: int - irWebsite: str - maxAge: int - priceHint: int - previousClose: float - open: float - dayLow: float - dayHigh: float - regularMarketPreviousClose: float - regularMarketOpen: float - regularMarketDayLow: float - regularMarketDayHigh: float - dividendRate: float - dividendYield: float - exDividendDate: int - payoutRatio: float - fiveYearAvgDividendYield: float - beta: float - trailingPE: float - forwardPE: float - volume: int - regularMarketVolume: int - averageVolume: int - averageVolume10days: int - averageDailyVolume10Day: int - bid: float - ask: float - bidSize: int - askSize: int - marketCap: int - fiftyTwoWeekLow: float - fiftyTwoWeekHigh: float - priceToSalesTrailing12Months: float - fiftyDayAverage: float - twoHundredDayAverage: float - currency: str - enterpriseValue: int - profitMargins: float - floatShares: int - sharesOutstanding: int - sharesShort: int - sharesShortPriorMonth: int - sharesShortPreviousMonthDate: int - dateShortInterest: int - sharesPercentSharesOut: float - heldPercentInsiders: float - heldPercentInstitutions: float - shortRatio: float - shortPercentOfFloat: float - impliedSharesOutstanding: int - bookValue: float - priceToBook: float - lastFiscalYearEnd: int - nextFiscalYearEnd: int - mostRecentQuarter: int - earningsQuarterlyGrowth: float - netIncomeToCommon: int - trailingEps: float - forwardEps: float - lastSplitFactor: str - lastSplitDate: int - enterpriseToRevenue: float - enterpriseToEbitda: float - 52WeekChange: float - SandP52WeekChange: float - lastDividendValue: float - lastDividendDate: int - exchange: str - quoteType: str - symbol: str - underlyingSymbol: str - shortName: str - longName: str - firstTradeDateEpochUtc: int - timeZoneFullName: str - timeZoneShortName: str - uuid: str - messageBoardId: str - gmtOffSetMilliseconds: int - currentPrice: float - targetHighPrice: float - targetLowPrice: float - targetMeanPrice: float - targetMedianPrice: float - recommendationMean: float - recommendationKey: str - numberOfAnalystOpinions: int - totalCash: int - totalCashPerShare: float - ebitda: int - totalDebt: int - quickRatio: float - currentRatio: float - totalRevenue: int - debtToEquity: float - revenuePerShare: float - returnOnAssets: float - returnOnEquity: float - grossProfits: int - freeCashflow: int - operatingCashflow: int - earningsGrowth: float - revenueGrowth: float - grossMargins: float - ebitdaMargins: float - operatingMargins: float - financialCurrency: str - trailingPegRatio: float ``` ### History ```python ticker.history(period='1mo') ``` Returns: ```python DataFrame with columns: ['Open', 'High', 'Low', 'Close', 'Volume', 'Dividends', 'Stock Splits'] Shape: (21, 7) ``` ### Financial Statements #### Income Statement ```python ticker.income_stmt ``` Returns: ```python DataFrame with columns: [Timestamp('2024-06-30 00:00:00'), Timestamp('2023-06-30 00:00:00'), Timestamp('2022-06-30 00:00:00'), Timestamp('2021-06-30 00:00:00')] Shape: (47, 4) ``` ## Common Parameters ### Period Options - `1d`: 1 day - `5d`: 5 days - `1mo`: 1 month - `3mo`: 3 months - `6mo`: 6 months - `1y`: 1 year - `2y`: 2 years - `5y`: 5 years - `10y`: 10 years - `ytd`: Year to date - `max`: Maximum available data ### Interval Options - `1m`: 1 minute - `2m`: 2 minutes - `5m`: 5 minutes - `15m`: 15 minutes - `30m`: 30 minutes - `60m`: 60 minutes - `90m`: 90 minutes - `1h`: 1 hour - `1d`: 1 day - `5d`: 5 days - `1wk`: 1 week - `1mo`: 1 month - `3mo`: 3 months #### In dealing with financial data from multiple tickers, specifically using yfinance and pandas, the process can be broken down into a few key steps: downloading the data, organizing it in a structured format, and accessing it in a way that aligns with the user's needs. Below, the answer is organized into clear, actionable segments. Downloading Data for Multiple Tickers Direct Download and DataFrame Creation Single Ticker, Single DataFrame Approach: For individual tickers, the DataFrame downloaded directly from yfinance comes with single-level column names but lacks a ticker column. By iterating over each ticker, adding a ticker column, and then combining these into a single DataFrame, a clear structure for each ticker's data is maintained. import yfinance as yf import pandas as pd tickerStrings = ['AAPL', 'MSFT'] df_list = [] for ticker in tickerStrings: data = yf.download(ticker, group_by="Ticker", period='2d') data['ticker'] = ticker # Add ticker column df_list.append(data) # Combine all dataframes into a single dataframe df = pd.concat(df_list) df.to_csv('ticker.csv') Condensed Single DataFrame Approach: Achieve the same result as above with a one-liner using list comprehension, streamlining the process of fetching and combining data. # Download 2 days of data for each ticker in tickerStrings, add a 'ticker' column for identification, and concatenate into a single DataFrame with continuous indexing. df = pd.concat([yf.download(ticker, group_by="Ticker", period='2d').assign(ticker=ticker) for ticker in tickerStrings], ignore_index=True) Multi-Ticker, Structured DataFrame Approach When downloading data for multiple tickers simultaneously, yfinance groups data by ticker, resulting in a DataFrame with multi-level column headers. This structure can be reorganized for easier access. Unstacking Column Levels: # Define a list of ticker symbols to download tickerStrings = ['AAPL', 'MSFT'] # Download 2 days of data for each ticker, grouping by 'Ticker' to structure the DataFrame with multi-level columns df = yf.download(tickerStrings, group_by='Ticker', period='2d') # Transform the DataFrame: stack the ticker symbols to create a multi-index (Date, Ticker), then reset the 'Ticker' level to turn it into a column df = df.stack(level=0).rename_axis(['Date', 'Ticker']).reset_index(level=1) Handling CSV Files with Multi-Level Column Names To read a CSV file that has been saved with yfinance data (which often includes multi-level column headers), adjustments are necessary to ensure the DataFrame is accessible in the desired format. Reading and Adjusting Multi-Level Columns: # Read the CSV file. The file has multi-level headers, hence header=[0, 1]. df = pd.read_csv('test.csv', header=[0, 1]) # Drop the first row as it contains only the Date information in one column, which is redundant after setting the index. df.drop(index=0, inplace=True) # Convert the 'Unnamed: 0_level_0', 'Unnamed: 0_level_1' column (which represents dates) to datetime format. # This assumes the dates are in the 'YYYY-MM-DD' format. df[('Unnamed: 0_level_0', 'Unnamed: 0_level_1')] = pd.to_datetime(df[('Unnamed: 0_level_0', 'Unnamed: 0_level_1')]) # Set the datetime column as the index of the DataFrame. This makes time series analysis more straightforward. df.set_index(('Unnamed: 0_level_0', 'Unnamed: 0_level_1'), inplace=True) # Clear the name of the index to avoid confusion, as it previously referred to the multi-level column names. df.index.name = None Flattening Multi-Level Columns for Easier Access Depending on the initial structure of the DataFrame, multi-level columns many need to be flattened to a single level, adding clarity and simplicity to the dataset. Flattening and Reorganizing Based on Ticker Level: For DataFrames where the ticker symbol is at the top level of the column headers: df.stack(level=0).rename_axis(['Date', 'Ticker']).reset_index(level=1) If the ticker symbol is at the bottom level: df.stack(level=1).rename_axis(['Date', 'Ticker']).reset_index(level=1) Individual Ticker File Management For those preferring to manage each ticker's data separately, downloading and saving each ticker's data to individual files can be a straightforward approach. Downloading and Saving Individual Ticker Data: for ticker in tickerStrings: # Downloads historical market data from Yahoo Finance for the specified ticker. # The period ('prd') and interval ('intv') for the data are specified as string variables. data = yf.download(ticker, group_by="Ticker", period='prd', interval='intv') # Adds a new column named 'ticker' to the DataFrame. This column is filled with the ticker symbol. # This step is helpful for identifying the source ticker when multiple DataFrames are combined or analyzed separately. data['ticker'] = ticker # Saves the DataFrame to a CSV file. The file name is dynamically generated using the ticker symbol, # allowing each ticker's data to be saved in a separate file for easy access and identification. # For example, if the ticker symbol is 'AAPL', the file will be named 'ticker_AAPL.csv'. data.tocsv(f'ticker{ticker}.csv') """