Data Fetching¶
fetch
¶
Data fetching utilities using yfinance.
Function
fetch_ohlc( ticker, interval="1d", lookback: str | None = None, start: str | None = None, end: str | None = None, auto_adjust: bool = False, )
Parameter semantics
- interval: Aggregation interval for candles ('1d', '1wk', '1mo').
- lookback: Relative period for history breadth ('5d','1mo','3mo','6mo','1y','2y','5y','10y','ytd','max').
- start/end: Explicit date range (YYYY-MM-DD). If both provided they override lookback. Passing values like '3mo' to start/end is invalid and will be ignored.
We guard against accidentally sending a lookback string where a date is required by validating format.
Returns a pandas DataFrame with columns: Open, High, Low, Close, Volume
fetch_ohlc(ticker, interval='1d', lookback=None, start=None, end=None, auto_adjust=False)
¶
Fetch OHLC data for a single ticker.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ticker
|
str
|
Stock symbol to download. |
required |
interval
|
str
|
Aggregation interval ('1d', '1wk', '1mo'). |
'1d'
|
lookback
|
str | None
|
Relative period for history ('1y', '5y', 'max', etc.). |
None
|
start
|
str | None
|
Start date YYYY-MM-DD (overrides lookback if end also provided). |
None
|
end
|
str | None
|
End date YYYY-MM-DD. |
None
|
auto_adjust
|
bool
|
Whether to adjust OHLC for splits/dividends. |
False
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with columns [Open, High, Low, Close, Volume]. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If data is empty or missing required columns. |
Note
- If both start and end are valid dates they override lookback.
- If either start/end is invalid (e.g. '3mo'), it is ignored.
- If nothing specified, default lookback = '1y'.
Source code in src/stockcharts/data/fetch.py
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | |
fetch_ohlc_batch(tickers, interval='1d', lookback=None, start=None, end=None, auto_adjust=False, threads=True, progress=False)
¶
Fetch OHLC data for multiple tickers in a single batch request.
Uses yfinance's built-in threading for parallel downloads, which is significantly faster than sequential single-ticker downloads.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tickers
|
list[str]
|
List of stock symbols to download. |
required |
interval
|
str
|
Aggregation interval ('1d', '1wk', '1mo'). |
'1d'
|
lookback
|
str | None
|
Relative period for history ('1y', '5y', 'max', etc.). |
None
|
start
|
str | None
|
Start date YYYY-MM-DD (overrides lookback if end also provided). |
None
|
end
|
str | None
|
End date YYYY-MM-DD. |
None
|
auto_adjust
|
bool
|
Whether to adjust OHLC for splits/dividends. |
False
|
threads
|
bool
|
Use multi-threading for faster downloads (default: True). |
True
|
progress
|
bool
|
Show download progress bar (default: False). |
False
|
Returns:
| Type | Description |
|---|---|
dict[str, DataFrame]
|
Dictionary mapping ticker symbols to their OHLC DataFrames. |
dict[str, DataFrame]
|
Failed downloads are silently omitted from results. |
Source code in src/stockcharts/data/fetch.py
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 | |