
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! 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: 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" for stock in $stocks 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.
Categorized:
CGI Scripts and Web Site Programming
, Shell Script Programming
(Article 7378)
Tagged: amazon, apple, cisco, ebay, google, microsoft, s&p500, yahoo Previous: Can Hacker Safe, TRUSTe, BBB, and Trust Guard Seals Actually Improve My Sales? Next: How do I change my Hotmail password in Windows Live Hotmail? Subscribe!
Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader. 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 AMI have a lot to say, but ...
I do have a comment, now that you mention it!
|
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!
Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.
Articles and Reviews
Auctions and Online Shopping Blogs and RSS Feeds Building Web site traffic Business and Management Cell Phones and Mobile Phones CGI Scripts and Web Site Programming Computer and Internet Basics d) None of the Above HTML and CSS Mac OS X Help MySpace, Facebook, Twitter and Social Network Help Pay Per Click (PPC) Search Engine Optimization Shell Script Programming Sony PSP, MP3 Players, Etc. The Writing Business Unix and Linux Help Video Game Tips and Help Windows Help
Recent Entries
Join the List!
Book Links
|