How do I create a custom stock market index Ask Dave Taylor

Post on: 27 Май, 2015 No Comment

How do I create a custom stock market index Ask Dave Taylor

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:

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

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

$ sh calculate-index.sh

CSCO is currently trading at: 26.39

MSFT is currently trading at: 31.00

AAPL is currently trading at: 109.76

AMZN is currently trading at: 61.68

YHOO is currently trading at: 29.14

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:


Categories
Stocks  
Tags
Here your chance to leave a comment!