history() retrieves a security’s historical market data through a yfinance Ticker object. Supply start and end dates in ISO 8601 YYYY-MM-DD format and choose an interval such as daily, hourly, or weekly; the default is '1d'. section 7.2.1
Use the method within a reusable fetching function:
- Create a Ticker for the requested symbol. section 7.2.1
- Pass start, end, and interval parameters to history(). section 7.2.1
- Reuse the function for different securities, periods, and sampling frequencies. section 7.2.1
Daily AAPL data
Fetch AAPL data from 2023-10-01 through 2024-04-30, then select the Close column for analysis. Resetting the index produces a simple sequential index. 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 Python function that constructs a yfinance Ticker and calls history() with configurable dates and interval. section 7.2.1
