Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How to import data for multiple stocks from the web?

Subject: How to import data for multiple stocks from the web?

From: Ryan

Date: 22 Jun, 2014 04:52:13

Message: 1 of 6

How to import data for multiple stocks from the web?

I have imported the entire S&P 500 from a text file. The object is named sp500, the value is 501x1 (right now there is one more than 500 securities in the S&P 500; there can be slightly less or more than 500 constituents at any given point in time).

How can I change the code below to loop through 501 constituents and import historical data for all, and write that to a variable/object?

Thanks!


Also, I have this code which works great for one single stock.
hist_stock_data('23012003','15042008','GOOG','C')
plot(ans.Close,'DisplayName','ans.Close','YDataSource','ans.Close');figure(gcf)
plot(y)

So, I want to import historical data for:
'ABT '
'ABBV '
'ACE '
etc . . .


One thing that doesn’t work, which I think should work, is this line of code.
What’s wrong with the line of code below?
y=diff(log(price.Close(end:-1:1)))


That line generates this error.
Undefined variable "price" or function "price.Close".

What am I doing wrong here?

Subject: How to import data for multiple stocks from the web?

From: Ryan

Date: 22 Jun, 2014 14:07:10

Message: 2 of 6

"Ryan" wrote in message <lo5nds$j1m$1@newscl01ah.mathworks.com>...
> How to import data for multiple stocks from the web?
>
> I have imported the entire S&P 500 from a text file. The object is named sp500, the value is 501x1 (right now there is one more than 500 securities in the S&P 500; there can be slightly less or more than 500 constituents at any given point in time).
>
> How can I change the code below to loop through 501 constituents and import historical data for all, and write that to a variable/object?
>
> Thanks!
>
>
> Also, I have this code which works great for one single stock.
> hist_stock_data('23012003','15042008','GOOG','C')
> plot(ans.Close,'DisplayName','ans.Close','YDataSource','ans.Close');figure(gcf)
> plot(y)
>
> So, I want to import historical data for:
> 'ABT '
> 'ABBV '
> 'ACE '
> etc . . .
>
>
> One thing that doesn’t work, which I think should work, is this line of code.
> What’s wrong with the line of code below?
> y=diff(log(price.Close(end:-1:1)))
>
>
> That line generates this error.
> Undefined variable "price" or function "price.Close".
>
> What am I doing wrong here?



So, I imported all constituents of the S&P500 into a vector.

Then, I run this code:
elm = sp500
for i = 1:length(elm)
hist_stock_data('23012003','15042008','GOOG',elm)
plot(ans.Close,'DisplayName','ans.Close','YDataSource','ans.Close');figure(gcf)
plot(y)
end


I get this error:
Error in Untitled (line 3)
hist_stock_data('23012003','15042008','GOOG',elm)

I guess there's something wrong with the loop. It's like the variables in the loop aren't being converted to single elements, or some such thing.

Subject: How to import data for multiple stocks from the web?

From: Ryan

Date: 22 Jun, 2014 15:29:13

Message: 3 of 6

"Ryan" wrote in message <lo5nds$j1m$1@newscl01ah.mathworks.com>...
> How to import data for multiple stocks from the web?
>
> I have imported the entire S&P 500 from a text file. The object is named sp500, the value is 501x1 (right now there is one more than 500 securities in the S&P 500; there can be slightly less or more than 500 constituents at any given point in time).
>
> How can I change the code below to loop through 501 constituents and import historical data for all, and write that to a variable/object?
>
> Thanks!
>
>
> Also, I have this code which works great for one single stock.
> hist_stock_data('23012003','15042008','GOOG','C')
> plot(ans.Close,'DisplayName','ans.Close','YDataSource','ans.Close');figure(gcf)
> plot(y)
>
> So, I want to import historical data for:
> 'ABT '
> 'ABBV '
> 'ACE '
> etc . . .
>
>
> One thing that doesn’t work, which I think should work, is this line of code.
> What’s wrong with the line of code below?
> y=diff(log(price.Close(end:-1:1)))
>
>
> That line generates this error.
> Undefined variable "price" or function "price.Close".
>
> What am I doing wrong here?


Oh! So dumb!! It's the 'i'

elm = sp500
for i = 1:length(elm)
hist_stock_data('23012003','15042008','GOOG',i)
y=diff(log(price.Close(end:-1:1)))
%plot(Close,'DisplayName','ans.Close','YDataSource','ans.Close');figure(gcf)
%plot(y)
end

Now, I keep getting errors with the length of the array.
Error in Untitled (line 4)
y=diff(log(price.Close(end:-1:1)))

Can someone please share a link, or tutorial, or whatever, that explains how this stuff works?

Like, what does this mean?
end:-1:1

Subject: How to import data for multiple stocks from the web?

From: dpb

Date: 22 Jun, 2014 16:02:08

Message: 4 of 6

On 6/22/2014 10:29 AM, Ryan wrote:
...

> Can someone please share a link, or tutorial, or whatever, that explains
> how this stuff works?
> Like, what does this mean?
> end:-1:1

step from last entry in array ("end") by -1 to first

   doc colon
   doc end

   doc

Click on "Getting Started" and read the tutorial stuffs...

--

Subject: How to import data for multiple stocks from the web?

From: Ryan

Date: 22 Jun, 2014 16:50:19

Message: 5 of 6

dpb <none@non.net> wrote in message <lo6ult$2i5$1@speranza.aioe.org>...
> On 6/22/2014 10:29 AM, Ryan wrote:
> ...
>
> > Can someone please share a link, or tutorial, or whatever, that explains
> > how this stuff works?
> > Like, what does this mean?
> > end:-1:1
>
> step from last entry in array ("end") by -1 to first
>
> doc colon
> doc end
>
> doc
>
> Click on "Getting Started" and read the tutorial stuffs...
>
> --
>


Yes, yes, yes. Now it makes sense. Thanks for the help. For the benefit of others, this link is pretty useful.
http://www.mathworks.com/help/matlab/learn_matlab/array-indexing.html

Subject: How to import data for multiple stocks from the web?

From: Steven Lord

Date: 23 Jun, 2014 14:13:25

Message: 6 of 6


"Ryan " <ryanshuell@gmail.com> wrote in message
news:lo5nds$j1m$1@newscl01ah.mathworks.com...
> How to import data for multiple stocks from the web?
>
> I have imported the entire S&P 500 from a text file. The object is named
> sp500, the value is 501x1 (right now there is one more than 500 securities
> in the S&P 500; there can be slightly less or more than 500 constituents
> at any given point in time).
> How can I change the code below to loop through 501 constituents and
> import historical data for all, and write that to a variable/object?

For future reference, this issue continued in a different thread and was
resolved there:

http://www.mathworks.com/matlabcentral/newsreader/view_thread/336489

The root cause was that some of the stocks for which Ryan tried to query
historical data didn't exist during the time period over which Ryan queried;
they went public and/or were added to the S&P 500 between the end of that
query period and now.

*snip*

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us