Industry guru Dave Taylor answers free tech support questions about a wide variety of business and technical topics, including blogging, Google AdSense, MySpace, Sony PSP, Apple iPod, Mp3 players, management, Linux, SEO, Mac OS X, Facebook, Twitter, LinkedIn and Microsoft Windows.

How do I create a custom stock market index?

My boss had a great suggestion for our local business web site: create a stock index like the S&P 500 that's just comprised of companies in our area. He said we should add it to our site but I honestly have no idea how to do that. Help!


Dave's Answer:

That is a cool idea and I have seen lots of interesting personalized stock indices, certainly more interesting than the Standard & Poor's 500 listing. But let's start by talking about what a stock index is, because it's worth trying to figure out if it's really of any value to your visitors before you delve into the project.

Individual stocks essentially reflect investor expectations of the future value of a company. If you believe that a particular company is going to grow bigger, to earn more next year than it does this year, you therefore also believe that the company is going to be worth more as an entity. Its current value is essentially the stock price * total number of shares of stock outstanding (e.g., issued and in the marketplace). This is what analysts call "market cap" and it can be calculated for any publicly traded firm quite easily.

For example, let's look at Cisco Systems (Nasdaq: CSCO) [financials]. A quick glance at Google finance shows that Cisco is trading at $26.24/share and that the market cap is an impressive 158.48 billion dollars. To find out the number of outstanding shares, we could divide the market cap by the current trading value, but suffice to say that the key indicator we want for a market index is the change in stock value over the desired period of time.

What period of time should we use? How about the usual 15-minute delayed ticker information of the value against its opening this morning? Currently Google Finance reports that CSCO is down 0.04 or 0.15% off its opening price.

For illustrative purposes, let's grab a half-dozen additional tech stocks and see how they're doing right now, as I write this blog entry: Microsoft (Nasdaq: MSFT) is +0.11, Apple (Nasdaq: AAPL) is +0.34, eBay (Nasdaq: EBAY) is +0.13, Yahoo (Nasdaq: YHOO) is -0.19 and Amazon (Nasdaq: AMZN) is -0.20.

A "favorite tech stocks index" can then be calculated by simply adding everything up: -0.04 + 0.11 + 0.34 + 0.13 + -0.19 + -0.20, giving us +0.15. Our little market indicator shows that overall our stocks are up, which is good, but not very glamorous. Then again, if you look at the S&P 500, it's currently showing +8.19 (0.54%). That's across 500 stocks, a total gain of $8.19 / share in the aggregate. Not a very active day in the market overall.

However, truth be told, the S&P 500 is far more complicated than I'm suggesting, with a weighted floating value and all sorts of abstruse calculations to get its neat little number (see, for example, Index Mathematics Methodology [pdf]). For our purposes, however, let's just create our own index methodology based on the simplistic ideas presented herein. We'll just add one more: a numeric index that's actually the sum value of one share of each of the companies we're tracking. (The real S&P 500 uses the combined market cap of the companies, actually).

If we go back to our tech stocks, here's what we find out: Cisco is trading at $26.29, Microsoft at $31.02, Apple at $109.38, eBay at $33.99, Yahoo at $29.21 and Amazon at $61.49. Add 'em up and our base index number is 291.38.

Cool. Now it's like a real index, isn't it? Let's call this the ADT Tech Six Index:

ADT Tech Six Index: 291.38 (+0.15)

Now I hope you can see how you can just pick a set of stocks and aggregate their individual data to come up with a sector or geographic stock index.

The question, though, is how the heck you implement it, right? :-)

That's a bit more tricky, but I'm going to borrow a script from my popular book Wicked Cool Shell Scripts, script #67: getstock.sh. That small program lets you script grabbing current stock value from a financial site, thusly:

$ sh ./067-getstock.sh MSFT
31.00

Not glamorous, but just what we need for a script to calculate an index. Here's a script that calculates the base index for the ADT Tech Six:

#!/bin/sh

stocks="CSCO MSFT EBAY AAPL AMZN YHOO"

getvalue="/home/taylor/scripts/067-getstock.sh"
sumvalue=0

for stock in $stocks
do
value="$($getvalue $stock)"
valuex100="$(echo $value \* 100 | bc | cut -d. -f1)"
echo "$stock is currently trading at: $value"
sumvalue=$(( $sumvalue + $valuex100 ))
done

indexvalue="$(echo "scale=2; $sumvalue / 100" | bc)"

echo "ADT Tech Six Index: $indexvalue"

exit 0

When run, the output is clean and interesting:

$ sh calculate-index.sh
CSCO is currently trading at: 26.39
MSFT is currently trading at: 31.00
EBAY is currently trading at: 34.01
AAPL is currently trading at: 109.76
AMZN is currently trading at: 61.68
YHOO is currently trading at: 29.14
ADT Tech Six Index: 291.98

Imagine now that you modify the script to only output that last value by itself, and that you now recalculate this every sixty minutes with a cron job (I assume that you're running a Linux server for your Web site, as most people are). It'd look like this:

sh build-index > $webhome/current.index.value

Now all you need to do in your actual Web page is include that value where you want it displayed. I suggest that a server-side include is the easiest solution:

<!--#include file="current.index.value"-->

You could drop this into a nice little CSS formatted container too:

<div style="font-size:200%;font-weight:bold;font-family:sans-serif;"
<!--#include file="current.index.value"-->
</div>

That should get you started in the right direction. There's still work to do including modifying the original getstock script to also output the change in value for the current trading day and summing those values up too, but hopefully this is a lot better than staring at a blank screen. :-)



Help others find this article at Del.icio.us, Digg, Netscape, Reddit, and Simpy.

Subscribe!

Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader.

Comments

i would like the understand better how index is constructed.am in Nigeria i would like to creaate an index for the nigeria stock market.

Posted by: kpide Anthony at January 17, 2008 12:37 AM

I have a lot to say, but ...
Starbucks coffee cup I have a lot to say, and questions of my own for that matter, but most of all I'd like to say thank you for all your efforts on this Web site by buying you a chai!

I do have a comment, now that you mention it!









Remember personal info?


Please note that I will never send you any unsolicited commercial email. Ever.

While I'm at it, please note that by submitting a question or comment you're agreeing to my terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site.









Search
Find just the answers you seek from among our 1700+ free tech support articles by using our Lijit search engine.


Help!





Subscribe to
Ask Dave Taylor!

Add to Google Reader
Add to My Yahoo!
Subscribe in NewsGator Online

RDF   XML

Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.


Recent Entries
Join the List!
Join my author info mailing list, where you'll learn about my upcoming books, speaking gigs, and more!


Book Links
© 2002 - 2008 by Dave Taylor. All Rights Reserved.

Note: This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this web site or on any linked site.

[whiteboard marker tray]