API Reference
This section details the classes and methods available in finfetcher.
DataFetcher
The core class of the library.
Main class for fetching and cleaning financial data from Yahoo Finance.
This class handles: - Connecting to Yahoo Finance. - Fetching historical data. - Cleaning data based on market hours (removing incomplete candles). - Calculating the next valid trading date (target_date).
Initialize the DataFetcher with a ticker symbol.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
The ticker symbol (e.g., 'AAPL', 'BTC-USD'). |
required |
custom_cutoffs
|
dict
|
Dictionary to merge with/override default MARKET_CUTOFFS. Structure & Example:The dictionary must use string keys for asset types (e.g., "EQUITY") and dictionaries for values. Time settings must contain integer "hour" and "minute". custom_cutoffs = { "EQUITY": { # Option A: Override specific timezones "timezones": { "America/New_York": {"hour": 13, "minute": 0} # Early close }, # Fallback for other timezones "default": {"hour": 18, "minute": 0} }, "MY_NEW_ASSET": { # Option B: Force a specific timezone (e.g., for Crypto/Futures) "force_tz": "UTC", "default": {"hour": 23, "minute": 59} } } |
None
|
Raises:
| Type | Description |
|---|---|
TickerNotFoundError
|
If the ticker initialization fails. |
TypeError
|
If custom_cutoffs or its internal structure has invalid types. |
ValueError
|
If custom_cutoffs has missing required keys or invalid values. |
Source code in src/finfetcher/core.py
Functions
get_data(period='4y', interval='1d')
Fetch historical data for the initialized symbol.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
period
|
str
|
Data period to download (default: "4y"). |
'4y'
|
interval
|
str
|
Data interval (default: "1d"). |
'1d'
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: A pandas DataFrame with the cleaned historical data. The DataFrame will have date index and columns like Open, High, Low, Close, Volume. |
Raises:
| Type | Description |
|---|---|
TickerNotFoundError
|
If the ticker does not exist. |
DataEmptyError
|
If no data is returned for the given period. |
YFinanceConnectionError
|
If connection to Yahoo Finance fails. |
FinFetcherError
|
Base exception for other library errors. |
Source code in src/finfetcher/core.py
Exceptions
Custom exceptions raised by the library to help you handle errors gracefully.
finfetcher.exceptions
DataEmptyError
FinFetcherError
TickerNotFoundError
Bases: FinFetcherError
Raised when the ticker symbol does not exist or cannot be found.
YFinanceConnectionError
Bases: FinFetcherError
Raised when there are network or API issues with yfinance.