yfinance retrieves historical Yahoo Finance data directly from Python. Given a ticker symbol and date range, it can provide prices, trading volumes, dividends, and related data for financial analysis and time-series modeling. section 7.2.1
A typical retrieval workflow is:
- Import yfinance using the yf alias. section 7.2.1
- Identify the security with a ticker symbol such as AAPL. section 7.2.1
- Create a Ticker object and call history with start, end, and interval parameters. section 7.2.1
- Select the Close column and reset its index before further analysis. section 7.2.1
Fetch daily AAPL history
Define fetch_stock_data to retrieve AAPL history from 2023-10-01 through 2024-04-30, then retain only closing prices for analysis. section 7.2.1
>>> def fetch_stock_data(ticker_symbol, start_date, end_date,
>>> interval = '1d'):
>>> ticker_data = yf.Ticker(ticker_symbol)
>>> stock_data = ticker_data.history(start = start_date,
end = end_date,
interval = interval)
>>> return stock_data
Shows the reusable function that creates a ticker object and retrieves historical data with configurable dates and interval. section 7.2.1

