MathGroup Archive 1997

[Date Index] [Thread Index] [Author Index]

Search the Archive

Charting the Stock Market with Mma.

  • To: mathgroup at
  • Subject: [mg6493] Charting the Stock Market with Mma.
  • From: penny at (Des Penny)
  • Date: Mon, 24 Mar 1997 21:38:49 -0500 (EST)
  • Sender: owner-wri-mathgroup at


So many people asked me for a copy of my stock market program that I
decided to write it up properly and submit it to the group for general use.

Overview of the program:
The program reads raw html data files of a particular web page that the
user has saved on his local hard drive.  It pulls out the data for the
various indices on that web page.  It adds this data to the matrix
containing all the other indices for previous versions of the web page.  It
then plots all the indices.  The resulting plots show how the markets
behaved during the given day.

Getting ready for the program.
In order to execute the program you must first access the following web page:

If you look at this page you will see that the financial data consists of
the many indices available to track the US stock market. Using Netscape
3.0, you must now save multiple editions of this page as raw HTML text.
Each edition has different values of the financial indices.  If you want to
track the behavior of the market for a particular day, you must save
editions of this web page periodically throughout the day.

Using Netscape 3.0 the procedure for saving the raw web pages is:

1. Go to the above web page.
2. Choose View/Document Source. The HTML source document should open in
3. Choose File/Save As
4. Choose the default name and save this file in a suitable folder in your
hard drive.
5. Press the reload button to get a new edition of the web page and repeat
steps 2 through 5 until you have enough files saved at suitable times
throughout the day.

For illustration purposes lets suppose that all the above files are saved
in a folder with the following path:

D Penny PB:Market Data:US Market for 1 Day

Since I am using a Mac, the above is the path naming convention for the
Mac.  "D Penny PB" is the name of my hard drive.  "Market Data" is a folder
on my hard drive, and "US Market for 1 Day" is a sub folder.  If you are
using an IBM you will need to change to its path naming conventions for

We now need to manually change the names of all the saved files so that
they can be read by Mma.  Choose a convenient file name e.g. "Markets".

Rename all the saved files as "Markets1", "Markets2", etc.  It does not
matter what sequence you assign file names.  The program will pick up the
date and time from within each file and use this to correctly sequence the
data.  Let us suppose that there are 24 files.  The last file name will
thus be "Markets24".

Everything is now ready for reading by the Mma program.

Start Mma and execute the following two cells:

Cell 1: Contains the definition of the function plotIndices:


          labelNames={"DJIA","NYSE Composite","NASDAQ
Composite","AMEX","S&P 500",
          "Dow Utilities","Dow Transports","Dow Composite","Russell 2000",
          "S&P 100 Options"},



(* findData Module finds all the indices data in each file. *)

        positions=StringPosition[strings[[ dataPlace[[1]] ]],dataPlace[[2]] ];
            strings[[ dataPlace[[1]] ]],{positions[[1,2]] +2,
              positions[[2,1]] -3}]

                (* Reading first file in order to get the date *)



                (* Setting Plot Labels *)

                labels=Map[(# <> " - " <> date)&,labelNames];
                PrependTo[labels," "]; (* Makes labels match data *)

(* Reading each file and assembling the data *)


        (* Finding Time from day beginning- in Seconds *)



        (* Pulling Indices *)



        (* Plotting Data *)


        (* Setting Tick marks for time axis *)

        Print["% Change from first value = ",
                ((trans[[i,imax]]-trans[[i,1]])/trans[[i,1]])100," %" ],

(End of Cell 1)

Cell 2: sets up the inputs for plotIndices and calls it.
prefix="D Penny PB:Market Data:US Market for 1 Day:Markets";
(* Gives the path pointing to the folder containing the Markets files.*)

(* Gives the number of Markets files to be read. *)

(End of Cell 2)

The plotIndices function will take the prefix given and append the digits
1, 2, 3 ..., imax in order to construct the correct Path and filename to
read each of the Markets files.

I've documented the program so that you can follow the major steps.

The program is quite robust in that it uses a flexible subroutine
"findData".  This subroutine finds the data by finding the occurrance of
the data labels in the raw HTML code e.g.  it finds the Dow Jones Index
value by searching record 28 of the file for the string "DJV".  It then
pulls out the number from between the two occurrances of the "DJV" string.
Thus, even if the Dow Jones Index goes above 10,000, the program will still
work even though the data is now in a physically different location in the

I hope you have fun with the program and can tailor it to your own needs.
I must issue the standard warning that I take no responsibility for any
consequences, financial or otherwise, of running this program, and of
course I will not hold myself responsible for correcting any bugs that may
exist in the code.

Having said the above, the code does not have the abilty to do any computer
damage since it contains no write statements.  Some obvious extensions of
the program are the following:

1. Save end of day files (from the same web site) in a folder.
Tailor the program to read these files and plot the data over many days.
You could also calculate moving averages to generate buy/sell prompts.

2. Save World market data from the web site:
and tailor the program to plot end of day data over an extended period.

Have fun.


Des Penny

Des Penny
Physical Science Dept.
Southern Utah University
Cedar City, UT 84720

VOICE: (Office): (801) 586-7708
       (Home)  : (801) 586-2286
FAX:    (801) 865-8051
e-mail: penny at

  • Prev by Date: Re: Solving Underdamped Equation of motion
  • Next by Date: Stock Price Charting
  • Previous by thread: Re: How can I handle Operator Algebra ?
  • Next by thread: Stock Price Charting