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:
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:
stocks=”CSCO MSFT EBAY AAPL AMZN YHOO”
for stock in $stocks
valuex100=”$(echo $value \* 100 | bc | cut -d. -f1)”
echo “$stock is currently trading at: $value”
sumvalue=$(( $sumvalue + $valuex100 ))
indexvalue=”$(echo “scale=2; $sumvalue / 100″ | bc)”
echo “ADT Tech Six Index: $indexvalue”
When run, the output is clean and interesting:
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:
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:
You could drop this into a nice little CSS formatted container too:
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. 🙂