File Exchange

image thumbnail


version (10.3 KB) by Michael Boldin
Pulls stock prices for a given ticker symbol from the YAHOO web server.


Updated 20 Feb 2007

No License

SQQ & STOCKQUOTEQUERY pull stock prices for a given ticker symbol
from the YAHOO web server using the MATLAB Java URL interface.

STOCKQUOTEQUERY has the main code. SQQ calls STOCKQUOTEQUERY and allows for either interactive (keyboard) inputs or as a function call with numerous options for setting dates.

DATE: Date for the quote
CLOSE: Closing market price
OPEN: Opening price
HIGH: High price (during trading day or frequency period)
LOW: Low price (during trading day or frequency period)
VOLUME: Volume (during trading day or frequency period)
CLOSEADJ: Close adjusted for splits

Use HELP SQQ for the full set of input and output options.

STOCKQUOTEQUERY is based on GETSTOCKDATA by Peter Webb of Mathworks, as explained in the October 2002 MATLAB News and Notes article "Internet Enabled Data Analysis and Visualization with MATLAB"

--Enter 'sqq' at the comand line for an interactive run.
--For a simple example that pulls recent data (over the past 7 days) for IBM,
try sqq('ibm',today,today-7).
--SQQEXAMPLE.M has more examples, including a demonstration of two different types
of stock quote queries and also produces three charts.

How SQQ differs from GETSTOCKDATA
--verifies YAHOO server tables have 7 elements on a line before parsing the line.
The server tables sometimes show extra lines for dividend payments and splits.
--close(breader); close(ireader); close(stream); commented out-- caused problems.
--adjusts query parameters to refer to January as month '0', not '1', and February
as '1', and so on. (This is a feature of the YAHOO system. STOCKQUOTEQUERY uses
1 to 12 numbering for months in the input-output parameter translation.)
--flips data vectors to normal date order (increasing, oldest to most recent).
--does not require month(), day() or year() functions, uses datevec.
--keeps VOLUME and CLOSEADJ data.
--SQQ has flexible input options, especially for dates.
--R12 compatible.

Comments and Ratings (30)

Tom Saam

I'm getting an error message, "??? Error using ==> StockQuoteQuery at 106
Could not connect to server. It may be unavailable. Try again later." After a few searches, I see that it's possible that Yahoo Finance has changed or eliminated the stock quote download API. Does anyone know if the URL format "" still works? I get a "server not found" when I try "" Any plan to update SQQ for a new API or perhaps Google Finance?

Devon Snell

Hello everyone,
does anyone know how to call this function more than once, to compare multiple stocks for instance. I get The server unavailable error on the second iteration.

Thank you


Hello everyone!I have been using this function to download data for stocks and it works pretty fine. However, i am trying to download currency data(exchange rates) from yahoo finance using this function and it is not possible.

Does anyone knows why is this and whether there is another function that could help me on this task?


"??? Error using ==> StockQuoteQuery at 104
Could not connect to server. It may be unavailable. Try again later."

in StockQuoteQuery.m go to the bottom of the code and change the yahooUrlString function as follow:

server = '';

change also query:

query = ['?s=' num2str(symbol) '&a=' num2str(startMonth-1) '&b=' num2str(startDay) '&c=' num2str(startYear) ...
'&d=' num2str(endMonth-1) '&e=' num2str(endDay) '&f=' num2str(endYear) ...
'&g=' num2str(freq) '&ignore=.csv'];

it worked fine for me.

Amit K

Great m-file! However, lately I have been getting the following message:
"??? Error using ==> StockQuoteQuery at 104
Could not connect to server. It may be unavailable. Try again later."

Does anyone know what is the reason for this and how to fix it? I will appreciate the help.



Works great. BUT. It does not permit you to start at the IPO date of each stock, and end at today's date.

if you visit Yahoo Finance, the default values given to start date and end date are the IPO and today's date respectively.

I would rate this program a clear five star that function would be added.

Hi, how to pass parameters through URL in matlab?

I am trying to pass the parameters (g = ) to the following link ",-117.226085" but wasn't successful. Could any one guide me please!?


Michael, good code. Works great.
Say, Is there any way to access, download or otherwise account for stock spits? Using 'LMT' as an example, dates 01/01/1979 to present, for example: Code dutifully downloads historical data but is radically different from that displayed on Yahoo. This is due, largely, to the stock splits. Any way to include these details?

Henry Leung

Simple interface. A handy tool for stock data processing.

Stefan Roth

Works nicely!
There's a little error popping up in the sqqexample.m line 17 though. It requires the year behind the dates or otherwise returns an error within StockQuoteQuery.m.

Sarah P

One can use Matlab Datafeed toolbox in the latest version.

Satisfied User

Excellent script, works perfectly

Michael Boldin

It see Yahoo changed their date format and the fix date=datenum(dates,'yyyy-mm-dd') worked for me, but I will post new code with a more robust solution

James Cadzow


I tried your recommendation of line 188 change and still unable to to download data.


Greg Wdowiak

Change line 188 if StockQuoteQuery.m from

date = datenum(dates);


date = datenum( dates, 'yyyy-mm-dd' );

(or use whatever date format /i.e. the 'yyyy-mm-dd' string/ may look like in your locale - I doubt that yahoo data would be locale-specific but who knows).

Fred Schlereth


get the same error message as mentioed by Mr Cadzow, above. Is there a fix?

James Cadzow

I have had problems with StockQuoteQuery over the last 2 or 3 days. I am unable to download historical stock data and get the message "2007 is too large a number for month". Is there a fix for this problem?

Larry Z

Wonderful tool!

james subbu

Good work!!

Do have one problem, does not work on the new Intetl Macs,
keeps crashing on,

is my Java messed up or does the pragoram need modification for the new macs??


Marcelo Scherer Perlin

Excellent. Too bad that yahoo server doesnt help much for commom stocks.

Gert Van TOnder


wang bryne

Not good performance for other symbols, such as etc. It can only extract at most 200 observations for such symbols.

LK Lee

Brian Edwards

Yahoo seemed to have changed its query request syntax. I added/changed this code in StockQuoteQuery.m.


%changed query string to work with new yahoo format. The month,variable a,
%is now represented by a 2 digit value, e.g. january=00, march=02,december=11 instead
%of older 0=jan, 2=march,december=11 format.
if((startMonth-1) < 10)
a = ['0' num2str(startMonth-1)];
else a = num2str(startMonth-1);

query = ['?s=' num2str(symbol) '&a=' a '&b=' num2str(startDay) '&c=' num2str(startYear) ...
'&d=' num2str(endMonth-1) '&e=' num2str(endDay) '&f=' num2str(endYear) ...

Brian Edwards

Very useful. Saved me a lot of time!

Kam Moud

Fantastic function, very useful, good job

James Cadzow

A very useful program for obtaining stock market data.

Jim Cadzow

Does someone have a version of this program that works under MATLAB R12? If so, would you please send me a copy. Thanks

Jim Cadzow

I am using MATLAB R12 and it does not have the command "strfind". I tried to replace this command with "findstr" which is in MATLA R12. Unfortunately, this does not seem to work. Could someone give me help in this matter. Thank You.

- -

Why a 0 for January and not 1? Where are zero based months used? That doesn't seem very common. Looks like some nice improvements otherwise.


Fixed problem caused by YAHOO changing their date format.

Modified function to be R12 compatible

MATLAB Release Compatibility
Created with R13
Compatible with any release
Platform Compatibility
Windows macOS Linux

Inspired by: Display Stock Data

Inspired: GUI Technical Analysis Tool