Charting the Stock Market with Mma.
- To: mathgroup at smc.vnet.net
- Subject: [mg6493] Charting the Stock Market with Mma.
- From: penny at suu.edu (Des Penny)
- Date: Mon, 24 Mar 1997 21:38:49 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
Hi: 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: http://cnnfn.com/markets/us_markets.html 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 "SimpleText". 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 directories. ---------------------------------------------------------------------------- ---------- 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: ---------------------------------------------------------------------------- ---------- Clear[plotIndices]; plotIndices[filePrefix_String,imax_Integer]:= Module[{findData,values,s,fileNames,data,labels,date,trans,times, labelNames={"DJIA","NYSE Composite","NASDAQ Composite","AMEX","S&P 500", "Dow Utilities","Dow Transports","Dow Composite","Russell 2000", "S&P 100 Options"}, dataLoc={{28,"DJV"},{32,"NYSEV"},{47,"NASV"},{62,"AMEXV"},{77,"SPV"},{90, "DUV"},{96,"DTV"},{102,"DCV"},{108,"R2V"},{114,"SP1V"}}, totalData={},string,hour,minute,second,seconds,test,plotTimes,xticks}, (* findData Module finds all the indices data in each file. *) findData[strings_List,dataPlace_List]:= Module[{positions,data}, positions=StringPosition[strings[[ dataPlace[[1]] ]],dataPlace[[2]] ]; data=StringTake[ strings[[ dataPlace[[1]] ]],{positions[[1,2]] +2, positions[[2,1]] -3}] ]; (* Reading first file in order to get the date *) values=Table[ToString[i],{i,1,imax}]; fileNames=Map[(prefix<>#)&,values]; string=ReadList[fileNames[[1]],Record]; date=StringTake[string[[27]],{141,150}]; (* Setting Plot Labels *) labels=Map[(# <> " - " <> date)&,labelNames]; PrependTo[labels," "]; (* Makes labels match data *) (* Reading each file and assembling the data *) Do[ string=ReadList[fileNames[[i]],Record]; (* Finding Time from day beginning- in Seconds *) s=findData[string,{27,"DJT"}]; pos=StringPosition[s,":"]; hour=ToExpression[StringTake[s,{1,pos[[1,2]]-1}]]; minute=ToExpression[StringTake[s,{pos[[1,2]]+1,pos[[2,1]]-1}]]; second=ToExpression[StringTake[s,{pos[[2,1]]+1,pos[[2,1]]+2}]]; seconds=hour*60^2+minute*60+second; (* Pulling Indices *) data=ToExpression[Map[findData[string,#]&,dataLoc]]; PrependTo[data,seconds]; PrependTo[totalData,data], {i,1,imax}]; totalData=Sort[totalData]; (* Plotting Data *) trans=Transpose[totalData]; times=trans[[1]]; (* Setting Tick marks for time axis *) test[{x_,y_}]:=x>(times[[1]]-60^2); plotTimes=Table[{i*60^2,ToString[i]},{i,9,16}]; xticks=Cases[plotTimes,{x_,y_}?test ]; Do[ ListPlot[Transpose[{times,trans[[i]]}],PlotJoined->True, PlotLabel->labels[[i]],Ticks->{xticks,Automatic}, AxesLabel->{"EDT",""}]; Print["% Change from first value = ", ((trans[[i,imax]]-trans[[i,1]])/trans[[i,1]])100," %" ], {i,2,11} ] ] ---------------------------------------------------------------------------- ---------- (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.*) imax=24; (* Gives the number of Markets files to be read. *) plotIndices[prefix,imax]; ---------------------------------------------------------------------------- ---------- (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 file. 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: http://www.cnnfn.com/markets/world_markets.html and tailor the program to plot end of day data over an extended period. Have fun. Cheers, 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 suu.edu -------------------------------