Yahoo Finance API (2025)
版本 1.0.0 (2.5 KB) 作者:
Darin Koblick
This is a simple way to use the Yahoo web API in MATLAB.
yfinanceAPI — Download daily stock prices from Yahoo Finance into MATLAB
Pull historical daily OHLCV data for one or more tickers using Yahoo Finance’s public chart endpoint, returned as a MATLAB struct array. Includes an optional demo that renders a candlestick chart.
What this does
- Fetches Open, High, Low, Close, Volume for each requested symbol at 1-day interval.
- Returns a struct array (one element per symbol) with fields:
- o (open), h (high), l (low), c (close), v (volume)
- s (symbol), t (timestamp as MATLAB datenum)
- Works for multiple tickers in one call (e.g., {'AAPL','GOOGL'}).
- Adds realistic HTTP headers to reduce blocked requests.
- Includes a self-demo: run with no inputs to see a candlestick plot.
Syntax
data = yfinanceAPI(symbol, start_date, end_date)
Inputs
- symbol — cell array of char, e.g. {'AAPL','GOOGL'}
- start_date — char/ string, 'YYYY-MM-DD' (inclusive)
- end_date — char/ string, 'YYYY-MM-DD' (inclusive)
Output
- data — struct array (length = numel(symbol)) with fields:
- o, h, l, c, v, s, t (see above)
Example
symbols = {'AAPL','MSFT'};
start = '2025-01-01';
finish = '2025-03-01';
data = yfinanceAPI(symbols, start, finish);
% Convert first symbol’s timestamps to datetime and plot close
t = datetime(datestr(data(1).t));
plot(t, data(1).c, 'LineWidth', 1.2);
grid on; xlabel('Date'); ylabel('Close ($)');
title(sprintf('%s Close', data(1).s));
Candlestick demo
Calling with no arguments downloads AAPL by default and renders a candlestick chart (requires Financial Toolbox for candle).
yfinanceAPI();
Notes & caveats
- Unofficial endpoint. Yahoo Finance’s chart API is undocumented and may change or throttle. This code adds common headers and a timeout, but availability isn’t guaranteed.
- Time zone & trading days. Timestamps are daily bars from Yahoo; holidays/weekends are omitted.
- Financial Toolbox (optional). Only needed for the candlestick demo; data download works without it.
- Networking. Requires internet access and permission for webread.
Tested with
- MATLAB R2021a (should work back to R2018b+ with datetime/posixtime).
- Windows 11; should be OS-agnostic.
引用格式
Darin Koblick (2025). Yahoo Finance API (2025) (https://www.mathworks.com/matlabcentral/fileexchange/181747-yahoo-finance-api-2025), MATLAB Central File Exchange. 检索时间: .
MATLAB 版本兼容性
创建方式
R2025a
兼容任何版本
平台兼容性
Windows macOS Linux标签
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!版本 | 已发布 | 发行说明 | |
---|---|---|---|
1.0.0 |