Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Ovid (2709)

Ovid
  (email not shown publicly)
http://publius-ovidius.livejournal.com/
AOL IM: ovidperl (Add Buddy, Send Message)

Stuff with the Perl Foundation. A couple of patches in the Perl core. A few CPAN modules. That about sums it up.

Journal of Ovid (2709)

Saturday February 07, 2009
07:12 PM

Calculating The Dow Jones Industrial Average

[ #38427 ]

Just in case you've ever wondered how it's done:

#!/usr/bin/env perl

use strict;
use warnings;

use LWP::Simple 'get';
use XML::XPath;

use Readonly;
Readonly my $stock_api   => 'http://judstephenson.com/api/Quotes/Realtime/';
Readonly my $dow_divisor => 0.1255527090;

my %dow = (
    AA   => "Alcoa",
    AXP  => "American Express",
    BA   => "Boeing",
    BAC  => "Bank of America",
    C    => "Citigroup",
    CAT  => "Caterpillar",
    CVX  => "Chevron Corporation",
    DD   => "DuPont",
    DIS  => "Walt Disney",
    GE   => "General Electric",
    GM   => "General Motors",
    HD   => "Home Depot",
    HPQ  => "Hewlett-Packard",
    IBM  => "IBM",
    INTC => "Intel",
    JNJ  => "Johnson & Johnson",
    JPM  => "JPMorgan Chase",
    KFT  => "Kraft Foods",
    KO   => "Coca-Cola",
    MCD  => "McDonald's",
    MMM  => "3M",
    MRK  => "Merck",
    MSFT => "Microsoft",
    PFE  => "Pfizer",
    PG   => "Procter & Gamble",
    T    => "AT&T",
    UTX  => "United Technologies Corporation",
    VZ   => "Verizon Communications",
    WMT  => "Walmart",
    XOM  => "ExxonMobil",
);

my $sum = 0;
for my $symbol ( keys %dow ) {
    my $xml = get("$stock_api$symbol")
      or die "Coudn't fetch $stock_api$symbol";
    my $xpath = XML::XPath->new( xml => $xml );
    my $value = $xpath->findvalue('/quote/stock/price/current')->value;
    my $high  = $xpath->findvalue('/quote/stock/price/high/year')->value;
    my $low   = $xpath->findvalue('/quote/stock/price/low/year')->value;
    $sum += $value;
    printf "%32s - Value %3.2f - Year High: %3.2f - Year Low: %3.2f\n" =>
      $dow{$symbol},
      $value,
      $high,
      $low;
}
my $djia = $sum / $dow_divisor;
print "The current Dow Jones industrial average is: $djia\n";

And the output:

          Verizon Communications - Value 31.72 - Year High: 39.94 - Year Low: 23.07
                 Hewlett-Packard - Value 36.85 - Year High: 49.97 - Year Low: 28.23
                            AT&T - Value 26.08 - Year High: 40.70 - Year Low: 20.90
                General Electric - Value 11.10 - Year High: 38.52 - Year Low: 10.66
                         Walmart - Value 49.63 - Year High: 63.85 - Year Low: 46.25
                Procter & Gamble - Value 54.00 - Year High: 73.57 - Year Low: 52.13
                      McDonald's - Value 58.46 - Year High: 67.00 - Year Low: 45.79
                  General Motors - Value 2.84 - Year High: 27.84 - Year Low: 1.70
                     Walt Disney - Value 19.45 - Year High: 35.02 - Year Low: 18.17
                     Kraft Foods - Value 26.34 - Year High: 34.97 - Year Low: 24.75
                 Bank of America - Value 6.13 - Year High: 43.81 - Year Low: 3.77
                     Caterpillar - Value 33.28 - Year High: 85.96 - Year Low: 29.60
                           Alcoa - Value 8.40 - Year High: 44.77 - Year Low: 6.80
                       Microsoft - Value 19.66 - Year High: 32.10 - Year Low: 16.75
                           Intel - Value 14.73 - Year High: 25.29 - Year Low: 12.06
                       Citigroup - Value 3.91 - Year High: 27.71 - Year Low: 2.80
                              3M - Value 52.39 - Year High: 83.22 - Year Low: 50.01
                           Merck - Value 30.77 - Year High: 48.23 - Year Low: 22.82
                  JPMorgan Chase - Value 27.63 - Year High: 50.63 - Year Low: 17.70
                American Express - Value 17.93 - Year High: 52.63 - Year Low: 14.72
                       Coca-Cola - Value 43.55 - Year High: 61.90 - Year Low: 40.29
               Johnson & Johnson - Value 58.51 - Year High: 72.76 - Year Low: 52.06
                          DuPont - Value 24.57 - Year High: 52.49 - Year Low: 21.32
United Technologies Corporation - Value 49.03 - Year High: 75.86 - Year Low: 41.76
                          Boeing - Value 42.92 - Year High: 88.29 - Year Low: 36.17
                      Home Depot - Value 23.53 - Year High: 30.74 - Year Low: 17.05
                             IBM - Value 96.14 - Year High: 130.93 - Year Low: 69.50
                      ExxonMobil - Value 80.34 - Year High: 96.12 - Year Low: 56.51
                          Pfizer - Value 14.84 - Year High: 23.03 - Year Low: 14.26
             Chevron Corporation - Value 74.90 - Year High: 104.63 - Year Low: 55.50
The current Dow Jones industrial average is: 8280.42666924853

Just taking a look at how it's calculated tells you one thing: the djia is a load of crap in terms of guaging how the country is really doing economically.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • "Just taking a look at how it's calculated tells you one thing: the djia is a load of crap in terms of guaging how the country is really doing economically."

    How about the S&P 500 then?

    • Never paid attention to how they're calculated. I should.

      • Basically, 500 large cap stocks that a committee considers representative of overall industry. Much better than the DJIA.
  • When AIG was tanking, they replaced it with Kraft. Now, if you took out GM and replaced it with Family Dollar (FDO), you'd get a 200 point jump in the average (right now, at least). If you also replace Citigroup with Google, jumps to 11400. With just a few good replacements, the economy would be doing much better!! Just keep replacing bad companies with ones which are doing well. It's much cheaper than the "stimulus"!

    (yes, I know there is some way in choosing the companies... but most don't know what it i

    • Part of the reason it's so awful is that it doesn't take into account the size or actual market price of each. For example, raise one stock a dollar and lower another one a dollar and there's no net effect, even if one stock started at two dollars and the other one started at two hundred. Also, if one company is 20 times larger than another, a one dollar change in stock price is considered to have the same net impact for either. This is why many consider the DJIA to be a lousy index, but since it's the m