using System; using System.Drawing; using System.Drawing.Imaging; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.IO; using System.Net; using System.Text; namespace NYAReader { /// /// Data market data off the internet and creates /// web pages and graphs based on that data. /// public class Form1 : System.Windows.Forms.Form { /// /// Required designer variable. /// private System.ComponentModel.Container components = null; private System.Windows.Forms.NumericUpDown numericUpDown1; private System.Windows.Forms.Button btnDownload; private System.Windows.Forms.Button btnGraph; private System.Windows.Forms.NumericUpDown numericUpDown2; private System.Windows.Forms.Label label2; private System.Windows.Forms.CheckBox cbUseAllData; private System.Windows.Forms.ListBox lbStockList; private System.Windows.Forms.Label label3; private System.Windows.Forms.Button btnGenSPPage; private System.Windows.Forms.ProgressBar progressBar1; private System.Windows.Forms.Button btnDownloadQuotes; private System.Windows.Forms.Button btnDownloadSP; private System.Windows.Forms.NumericUpDown numericUpDown3; private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label4; private System.Windows.Forms.Button btnMakePage; public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); //Load settings from last use of program, or use default settings //if there is no record of old settings. if (File.Exists( @"C:\Documents and Settings\All Users\Documents\NYAReader\options.txt")) { FileStream fs=new FileStream( @"C:\Documents and Settings\All Users\Documents\NYAReader\options.txt", FileMode.Open); StreamReader sr=new StreamReader(fs); numericUpDown1.Value=int.Parse(sr.ReadLine()); numericUpDown2.Value=int.Parse(sr.ReadLine()); numericUpDown3.Value=int.Parse(sr.ReadLine()); cbUseAllData.Checked=bool.Parse(sr.ReadLine()); lbStockList.SelectedIndex=int.Parse(sr.ReadLine()); sr.Close(); } else { numericUpDown1.Value=5; numericUpDown2.Value=253; numericUpDown3.Value=5; cbUseAllData.Checked=false; lbStockList.SelectedIndex=1; } } /// /// Clean up any resources being used. /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.numericUpDown1 = new System.Windows.Forms.NumericUpDown(); this.btnDownload = new System.Windows.Forms.Button(); this.btnGraph = new System.Windows.Forms.Button(); this.numericUpDown2 = new System.Windows.Forms.NumericUpDown(); this.label2 = new System.Windows.Forms.Label(); this.cbUseAllData = new System.Windows.Forms.CheckBox(); this.lbStockList = new System.Windows.Forms.ListBox(); this.label3 = new System.Windows.Forms.Label(); this.btnGenSPPage = new System.Windows.Forms.Button(); this.btnMakePage = new System.Windows.Forms.Button(); this.progressBar1 = new System.Windows.Forms.ProgressBar(); this.btnDownloadQuotes = new System.Windows.Forms.Button(); this.btnDownloadSP = new System.Windows.Forms.Button(); this.numericUpDown3 = new System.Windows.Forms.NumericUpDown(); this.label1 = new System.Windows.Forms.Label(); this.label4 = new System.Windows.Forms.Label(); ((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.numericUpDown2)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.numericUpDown3)).BeginInit(); this.SuspendLayout(); // // numericUpDown1 // this.numericUpDown1.Location = new System.Drawing.Point(208, 216); this.numericUpDown1.Name = "numericUpDown1"; this.numericUpDown1.Size = new System.Drawing.Size(48, 20); this.numericUpDown1.TabIndex = 10; this.numericUpDown1.Value = new System.Decimal(new int[] { 2, 0, 0, 0}); // // btnDownload // this.btnDownload.Location = new System.Drawing.Point(32, 96); this.btnDownload.Name = "btnDownload"; this.btnDownload.Size = new System.Drawing.Size(88, 32); this.btnDownload.TabIndex = 12; this.btnDownload.Text = "Download Data"; this.btnDownload.Click += new System.EventHandler(this.btnDownload_Click); // // btnGraph // this.btnGraph.Location = new System.Drawing.Point(32, 8); this.btnGraph.Name = "btnGraph"; this.btnGraph.Size = new System.Drawing.Size(88, 40); this.btnGraph.TabIndex = 13; this.btnGraph.Text = "Generate Graphs"; this.btnGraph.Click += new System.EventHandler(this.btnGraph_Click); // // numericUpDown2 // this.numericUpDown2.Enabled = false; this.numericUpDown2.Location = new System.Drawing.Point(208, 240); this.numericUpDown2.Maximum = new System.Decimal(new int[] { 10000, 0, 0, 0}); this.numericUpDown2.Minimum = new System.Decimal(new int[] { 2, 0, 0, 0}); this.numericUpDown2.Name = "numericUpDown2"; this.numericUpDown2.Size = new System.Drawing.Size(48, 20); this.numericUpDown2.TabIndex = 16; this.numericUpDown2.Value = new System.Decimal(new int[] { 20, 0, 0, 0}); // // label2 // this.label2.ImageAlign = System.Drawing.ContentAlignment.MiddleRight; this.label2.Location = new System.Drawing.Point(24, 240); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(176, 24); this.label2.TabIndex = 17; this.label2.Text = "Number of days to plot"; this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight; // // cbUseAllData // this.cbUseAllData.CheckAlign = System.Drawing.ContentAlignment.MiddleRight; this.cbUseAllData.Checked = true; this.cbUseAllData.CheckState = System.Windows.Forms.CheckState.Checked; this.cbUseAllData.Location = new System.Drawing.Point(152, 264); this.cbUseAllData.Name = "cbUseAllData"; this.cbUseAllData.TabIndex = 18; this.cbUseAllData.Text = "Use all data"; this.cbUseAllData.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; this.cbUseAllData.CheckedChanged += new System.EventHandler(this.cbUseAllData_CheckedChanged); // // lbStockList // this.lbStockList.Items.AddRange(new object[] { "StockList.txt", "SP500.txt"}); this.lbStockList.Location = new System.Drawing.Point(136, 296); this.lbStockList.Name = "lbStockList"; this.lbStockList.Size = new System.Drawing.Size(120, 30); this.lbStockList.TabIndex = 19; // // label3 // this.label3.Location = new System.Drawing.Point(32, 296); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(100, 32); this.label3.TabIndex = 21; this.label3.Text = "Stock list"; this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight; // // btnGenSPPage // this.btnGenSPPage.Location = new System.Drawing.Point(144, 56); this.btnGenSPPage.Name = "btnGenSPPage"; this.btnGenSPPage.Size = new System.Drawing.Size(88, 32); this.btnGenSPPage.TabIndex = 22; this.btnGenSPPage.Text = "Generate S&&P Page"; this.btnGenSPPage.Click += new System.EventHandler(this.btnGenSPPage_Click); // // btnMakePage // this.btnMakePage.Location = new System.Drawing.Point(144, 8); this.btnMakePage.Name = "btnMakePage"; this.btnMakePage.Size = new System.Drawing.Size(88, 40); this.btnMakePage.TabIndex = 23; this.btnMakePage.Text = "Generate Web Pages"; this.btnMakePage.Click += new System.EventHandler(this.btnMakePage_Click); // // progressBar1 // this.progressBar1.Location = new System.Drawing.Point(8, 336); this.progressBar1.Name = "progressBar1"; this.progressBar1.Size = new System.Drawing.Size(248, 23); this.progressBar1.TabIndex = 24; // // btnDownloadQuotes // this.btnDownloadQuotes.Location = new System.Drawing.Point(144, 96); this.btnDownloadQuotes.Name = "btnDownloadQuotes"; this.btnDownloadQuotes.Size = new System.Drawing.Size(88, 32); this.btnDownloadQuotes.TabIndex = 25; this.btnDownloadQuotes.Text = "Download Quotes"; this.btnDownloadQuotes.Click += new System.EventHandler(this.btnDownloadQuotes_Click); // // btnDownloadSP // this.btnDownloadSP.Location = new System.Drawing.Point(32, 56); this.btnDownloadSP.Name = "btnDownloadSP"; this.btnDownloadSP.Size = new System.Drawing.Size(88, 32); this.btnDownloadSP.TabIndex = 26; this.btnDownloadSP.Text = "Download S&&P 500"; this.btnDownloadSP.Click += new System.EventHandler(this.btnDownloadSP_Click); // // numericUpDown3 // this.numericUpDown3.Location = new System.Drawing.Point(208, 192); this.numericUpDown3.Name = "numericUpDown3"; this.numericUpDown3.Size = new System.Drawing.Size(48, 20); this.numericUpDown3.TabIndex = 27; // // label1 // this.label1.Location = new System.Drawing.Point(24, 216); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(176, 16); this.label1.TabIndex = 28; this.label1.Text = "Number of days for volatility"; this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight; // // label4 // this.label4.Location = new System.Drawing.Point(0, 192); this.label4.Name = "label4"; this.label4.Size = new System.Drawing.Size(200, 16); this.label4.TabIndex = 29; this.label4.Text = "Number of days for strength of trend"; this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleRight; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(264, 366); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.label4, this.label1, this.numericUpDown3, this.btnDownloadSP, this.btnDownloadQuotes, this.progressBar1, this.btnMakePage, this.btnGenSPPage, this.label3, this.lbStockList, this.cbUseAllData, this.label2, this.numericUpDown2, this.btnGraph, this.btnDownload, this.numericUpDown1}); this.Name = "Form1"; this.Text = "NYA Reader"; this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing); ((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.numericUpDown2)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.numericUpDown3)).EndInit(); this.ResumeLayout(false); } #endregion /// /// The main entry point for the application. /// [STAThread] static void Main() { Application.Run(new Form1()); } /// /// Reads a .csv file and stores the data in an array. /// /// The path of the file /// to be read. /// Returns a two-dimensional array /// of the .csv data in string format. private string[,] readFile(string fileName) { Stream dataStream=File.OpenRead(fileName); BinaryReader br=new BinaryReader(dataStream); //Find out how many lines of data there are. int rows=br.ReadInt32(); DateTime[] date=new DateTime[rows]; float[] open=new float[rows]; float[] high=new float[rows]; float[] low=new float[rows]; float[] close=new float[rows]; int[] volume=new int[rows]; //Read file. int month,day,year; for(int i=0; i /// Calculates statistics on the data sent by the /// readFile method. /// /// A two-dimensional array /// of market data in string form. /// Returns an array of arrays /// containing the high, low, and average close and volume, /// volatility,gain/loss,and private float[][] calculateStats(string[,] data) { float[] closeHigh=new float[1], closeAve=new float[1], volHigh=new float[1]; float volSum=0,closeSum=0; float[] volLow=new float[1], closeLow=new float[1],volAve=new float[1];; float val1,val2; float[][] stats=new float[9][]; float[] rVol=new float[data.GetLength(0)]; //check if there was an error reading the file. if (data[0,0]=="error") { stats[0][0]=-1; return stats; } /**calculate high, low, and sum close *and volume data**/ closeLow[0]=float.Parse(data[0,4]); volLow[0]=float.Parse(data[0,5]); closeHigh[0]=0; volHigh[0]=0; volAve[0]=0; closeAve[0]=0; for (int i=0; i float.Parse(data[i,4])) closeLow[0] = float.Parse(data[i,4]); if (volHigh[0]float.Parse(data[i,5])) volLow[0]=float.Parse(data[i,5]); closeSum+=float.Parse(data[i,4]); volSum+=float.Parse(data[i,5]); } closeAve[0]=closeSum/data.GetLength(0); volAve[0]=volSum/data.GetLength(0); //Calculate volatility. int n=(int)numericUpDown1.Value; float[] closeData=new float[data.GetLength(0)]; for (int i=0; i=0; i--) { rVol[i]=-999; } Array.Reverse(rVol); //Calculate gain/loss. int rows=data.GetLength(0),lossDays=0,gainDays=0; bool[] loss=new bool[rows]; float open,close,afterLoss=0,afterGain=0; float[] change=new float[rows],changePercent=new float[rows]; for(int i=0; i0) sign1='+'; else sign1='-'; if(percentChange[i-1]>0) sign2='+'; else sign2='-'; if(sign1==sign2) index[i]=Math.Abs(percentChange[i]); else index[i]= -Math.Abs(percentChange[i]); } float[] average=new float[index.Length]; float[] sum=new float[index.Length]; int maxDays=(int)numericUpDown3.Value; for(int i=maxDays; i /// Draws a line graph of close data and saves it as a .gif file. /// /// Array of DateTimes that will be used for /// the x-axis. /// Array of floats used for the y-axis. /// Indicates where the image /// will be saved and the filename of the image. /// Number of yData points between big ticks. /// Number of yData points between small ticks. /// Interval between tick marks on x-axis. If 0, one /// tick every month. If 1, one tick every year. /// The color of the graph. /// If true, values will be made smaller by dividing /// by 1 million. If this method causes an infinite loop, set this /// parameter to true. /// The label of the y-axis. /// The title of the graph. /// The number of decimal places in the /// ySmallInterval parameter. The precision parameter must /// be set correctly to avoid an infinite loop. private void drawGraph(DateTime[] dates,float[] yData,string fileName, float yBigInterval,float ySmallInterval,int xInterval,Color color,bool reduce, string yAxisLabel,string title,int precision) { TimeSpan dateRange,fromStart; int numberOfDays; int yHighInt,yLowInt; decimal yLabel; decimal yHigh=0,yLow,yRange; decimal scaleX,scaleY; decimal tickInterval,posit; DateTime dateLabel,date; Pen axisPen=new Pen(Color.Black,2); Pen graphPen=new Pen(color); Pen tickPen=new Pen(Color.Black,1); Point origin,xEnd,yEnd,newPoint,oldPoint,tickPoint,textPoint; Font font=new Font("Times New Roman",7); Font titleFont=new Font("Times New Roman",30,FontStyle.Bold); Font labelFont=new Font("Times New Roman",12,FontStyle.Bold); SolidBrush brush=new SolidBrush(Color.Black); SolidBrush titleBrush=new SolidBrush(Color.FromArgb(75,Color.Black)); string dateLabelString; Bitmap graph; Graphics g; int n=(int)numericUpDown1.Value; //Remove data that should not be graphed. if(cbUseAllData.Checked==false) { dates=removeData(dates); yData=removeData(yData); } int rows=dates.Length; DateTime latest=dates[0]; DateTime earliest=dates[rows-1]; decimal[,] pointCoord=new decimal[rows,2]; //Make values smaller if bool reduce is true if(reduce) { for(int i=0; i(decimal)yData[i] && yData[i]!=-999) yLow=(decimal)yData[i]; } yLowInt=roundToInt(yLow); yHighInt=roundToInt(yHigh); yRange=yHigh-yLow; dateRange=latest-earliest; numberOfDays=dateRange.Days; scaleY=400/yRange; scaleX=500/(decimal)numberOfDays; //Find how many pixels above and to the right of //the origin each point should go. for(int i=0; i=yEnd.Y) { j=Math.Round(j,precision); if (((yLowInt+j)%yBigInterval)==0) { drawTick(g,tickPen,tickPoint,false,6); drawTick(g,tickPen,tickPoint2,false,6); textPoint.X=tickPoint.X-27; textPoint.Y=tickPoint.Y-5; textPoint2.X=tickPoint2.X+10; textPoint2.Y=tickPoint2.Y-5; yLabel=(decimal)(yLowInt+j); g.DrawString(yLabel.ToString(),font,brush,textPoint); g.DrawString(yLabel.ToString(),font,brush,textPoint2); } else { drawTick(g,tickPen,tickPoint,false,3); drawTick(g,tickPen,tickPoint2,false,3); } j+=ySmallInterval; posit+=tickInterval; tickPoint.Y=origin.Y-roundToInt(posit); tickPoint2.Y=tickPoint.Y; tickPoint2.X=tickPoint.X + 500; } tickPoint.X=yEnd.X; tickPoint.Y=yEnd.Y; tickPoint2.X=yEnd2.X; tickPoint2.Y=yEnd2.Y; drawTick(g,tickPen,tickPoint,false,6); drawTick(g,tickPen,tickPoint2,false,6); g.DrawString(yHigh.ToString(),font,brush,10,5); g.DrawString(yHigh.ToString(),font,brush,yEnd2.X+30,yEnd2.Y-5); //Draw x-axis tick marks and labels. //One tick every month. posit=0; j=0; tickPoint.X=origin.X; tickPoint.Y=origin.Y; tickInterval=1.0M*scaleX; while(origin.X+posit<=xEnd.X) { dateLabel=earliest.AddDays(j); if(dateLabel.Day==1) { if(dateLabel.Month==1) { this.drawTick(g,tickPen,tickPoint,true,6); textPoint.X=tickPoint.X-15; textPoint.Y=tickPoint.Y+25; dateLabelString=dateLabel.Month.ToString()+"/"+ dateLabel.Day.ToString()+"/"+ dateLabel.Year.ToString(); g.DrawString(dateLabelString,font,brush,textPoint); } else { if(xInterval==0) { drawTick(g,tickPen,tickPoint,true,3); textPoint.X=tickPoint.X-7; textPoint.Y=tickPoint.Y+10; dateLabelString=dateLabel.Month.ToString()+"/"+ dateLabel.Day.ToString(); g.DrawString(dateLabelString,font,brush,textPoint); } } } j++; posit+=tickInterval; tickPoint.X=origin.X+roundToInt(posit); } //Draw graph. for(int i=0; i /// Generates an html file showing statistics on market data from /// a .csv file. /// /// The path of the .csv file to be used /// in making the web page. /// Indicates where the web page will be /// saved and its filename. private void generateWebPage(string path,string fileName,string symbol) { StreamWriter sw=new StreamWriter(fileName,false); string[,]data=readFile(path); float[][] stats=calculateStats(data); //Handle error opening file. if(data[0,0]=="error") return; //Find out what the next and previous stock symbols are. StreamReader sr=new StreamReader(@"C:\daddy\MathCom\SP500.txt"); string lineString="",previous="",next=""; int lineNumber=0; for(int i=0; i<500; i++) { lineString=sr.ReadLine(); if(lineString==symbol) { next=sr.ReadLine(); break; } lineNumber++; } sr.BaseStream.Seek(0,SeekOrigin.Begin); sr.BaseStream.Position=0; sr.DiscardBufferedData(); for(int i=0; i"); sw.WriteLine(""); sw.WriteLine("mathematicalanalysis.com"); sw.WriteLine("S&P 500 Components"); if(previous!="") sw.WriteLine("Previous"); if(next!="") sw.WriteLine("Next"); sw.WriteLine("
"); //Title sw.WriteLine("Market Data for " +symbol+ ""); string title="",industry=""; string[][] info = getSPData(false); for (int i=0; i<500; i++) { if (info[i][0]==symbol) { title=info[i][1]; industry=info[i][9]; break; } } sw.WriteLine("

" + symbol + " - " + title + "

"); sw.WriteLine("

[" + industry + "]

"); //Table of stats sw.WriteLine(""); sw.WriteLine(" "); sw.WriteLine(""); sw.WriteLine(""); sw.WriteLine(""); sw.WriteLine(""); float change=0,volAvg=0,volSum=0; if(data.GetLength(0)>5) { change=(float.Parse(data[0,4])-float.Parse(data[4,4])) / float.Parse(data[4,4]) * 100; change=(float)Math.Round(change,1); for(int i=0; i<5; i++) { volSum+=float.Parse(data[i,5]); } volAvg=volSum / 5; sw.WriteLine(""); } else sw.WriteLine(""); sw.WriteLine(""); if(data.GetLength(0)>21) { change=(float.Parse(data[0,4])-float.Parse(data[20,4])) / float.Parse(data[20,4]) * 100; change=(float)Math.Round(change,1); volSum=0; for(int i=0; i<21; i++) { volSum+=float.Parse(data[i,5]); } volAvg=volSum / 21; sw.WriteLine(""); } else sw.WriteLine(""); sw.WriteLine(""); if(data.GetLength(0)>63) { change=(float.Parse(data[0,4])-float.Parse(data[62,4])) / float.Parse(data[62,4]) * 100; change=(float)Math.Round(change,1); volSum=0; for(int i=0; i<63; i++) { volSum+=float.Parse(data[i,5]); } volAvg=volSum / 63; sw.WriteLine(""); } else sw.WriteLine(""); sw.WriteLine(""); if(data.GetLength(0)>253) { change=(float.Parse(data[0,4])-float.Parse(data[252,4])) / float.Parse(data[252,4]) * 100; change=(float)Math.Round(change,1); volSum=0; for(int i=0; i<253; i++) { volSum+=float.Parse(data[i,5]); } volAvg=volSum / 253; sw.WriteLine(""); } else sw.WriteLine(""); sw.WriteLine(""); if(data.GetLength(0)>758) { change=(float.Parse(data[0,4])-float.Parse(data[757,4])) / float.Parse(data[757,4]) * 100; change=(float)Math.Round(change,1); volSum=0; for(int i=0; i<758; i++) { volSum+=float.Parse(data[i,5]); } volAvg=volSum / 758; sw.WriteLine(""); } else sw.WriteLine(""); sw.WriteLine(""); if(data.GetLength(0)>2525) { change=(float.Parse(data[0,4])-float.Parse(data[2524,4])) / float.Parse(data[2524,4]) * 100; change=(float)Math.Round(change,1); volSum=0; for(int i=0; i<2525; i++) { volSum+=float.Parse(data[i,5]); } volAvg=volSum / 2525; sw.WriteLine(""); } else sw.WriteLine(""); sw.WriteLine("
As of " + DateTime.Parse(data[0,0]).ToShortDateString() + "
PerformanceAverage Volume
Current Price" + data[0,4] + "
Past Week"+change.ToString()+"%"+volAvg+"
N/AN/A
Past Month"+change.ToString()+"%"+volAvg.ToString() +"
N/AN/A
Past Quarter"+change.ToString()+"%"+volAvg.ToString() +"
N/AN/A
Past Year"+change.ToString()+"%"+volAvg.ToString() +"
N/AN/A
Past 3 Years"+change.ToString()+"%"+volAvg.ToString() +"
N/AN/A
Past 10 Years"+change.ToString()+"%"+volAvg.ToString() +"
N/AN/A
"); //Graphs sw.WriteLine( @""); sw.WriteLine( @""); sw.WriteLine(""); sw.Close(); } /// /// Downloads a group of market data files from the Internet /// and saves them to a file. /// private void downloadWebPage() { StringBuilder URL,path; WebRequest request; Stream responseStream; string currentLine,symbol; StreamReader sr; bool atEnd=false; int numberOfLines=0; string stockList=""; //Select stock list. switch(lbStockList.SelectedIndex) { case 0: stockList=@"C:\daddy\MathCom\StockList.txt"; break; case 1: stockList=@"C:\daddy\MathCom\SP500.txt"; break; } //Find out how many lines are in the stock list file. sr=new StreamReader(stockList); do { currentLine=sr.ReadLine(); if(currentLine==""||currentLine==null) atEnd=true; else numberOfLines+=1; } while(!atEnd); progressBar1.Maximum=numberOfLines; //Reset file pointer to the beginning. sr.BaseStream.Seek(0, SeekOrigin.Begin); sr.BaseStream.Position = 0; //Download one web page for each stock symbol in the StockList. path=new StringBuilder(); URL=new StringBuilder(); for(int i=0; i /// Draws a vertical or horizontal tick mark (short line) /// centered on a certain point. /// /// Graphics object used for drawing. /// Pen used to draw the line. /// Point that the tick is /// centered on. /// Indicates whether the /// tick should be drawn vertically or horizontally. If verticalTick is true, /// the tick will be drawn vertically. /// The length of one half of /// the tick mark. private void drawTick(Graphics g, Pen pen,Point ctrPoint, bool verticalTick,int length) { Point p1,p2; if(verticalTick==true) { p1=new Point(ctrPoint.X,ctrPoint.Y-length); p2=new Point(ctrPoint.X,ctrPoint.Y+length); } else { p1=new Point(ctrPoint.X-length,ctrPoint.Y); p2=new Point(ctrPoint.X+length,ctrPoint.Y); } g.DrawLine(pen,p1,p2); } /// /// Rounds a double to the nearest int. /// /// The double to be rounded /// The double rounded to an int. private int roundToInt(double d) { return((int)(d+0.5)); } private int roundToInt(decimal d) { return((int)(d+0.5M)); } /// /// Writes a binary file from a Stream of market data. /// /// Stream of market data from the Internet. private void writeBinary(Stream dataStream,string path) { //Create array of strings from data Stream. StreamReader sr=new StreamReader(dataStream); bool atEnd=false; string[][] data; string[] line=new string [10000]; int numberOfLines=0; int x=0; sr.ReadLine(); //First line not needed. while(!atEnd) { line[x]=sr.ReadLine(); if(line[x]==null) atEnd=true; else { numberOfLines++; x++; } } sr.Close(); data=new String [numberOfLines][]; for(int i=0; i /// Calculates the percentile for a value within an array /// of values /// /// An array of values. /// The value to calculate the percentile of. /// The percentile of the value within the array. private float percentile(float[] array,float val) { int greaterThan=0; foreach(int i in array) if(val>i) greaterThan++; float percentile=(float)greaterThan/array.Length*100F; return percentile; } /// /// Finds the value for a certain percentile in an array. /// /// The array to be used in the calculation /// The percentile that the returned value /// should be. /// A value that is the given percentile in the array. private float percentileReverse(float[] array,float percentile) { Array.Sort(array,0,array.Length); //Find how many values the number must be greater than. int greater=(int)(Math.Round((decimal)(percentile/100*array.Length),0)); float a=array[greater-1]; float b=array[greater]; float val=(a+b)/2F; return val; } private void btnGraph_Click(object sender, System.EventArgs e) { StreamReader sr; string currentLine; int numberOfLines=0; bool atEnd=false; string symbol="",stockList=""; StringBuilder path=new StringBuilder(),fileName=new StringBuilder(); //Select stock list. switch(lbStockList.SelectedIndex) { case 0: stockList=@"C:\daddy\MathCom\StockList.txt"; break; case 1: stockList=@"C:\daddy\MathCom\SP500.txt"; break; } //Find out how many lines are in the stock list file. sr=new StreamReader(stockList); do { currentLine=sr.ReadLine(); if(currentLine==""||currentLine==null) atEnd=true; else numberOfLines+=1; } while(!atEnd); progressBar1.Maximum=numberOfLines; //Reset file pointer to the beginning. sr.BaseStream.Seek(0, SeekOrigin.Begin); sr.BaseStream.Position = 0; //Generate three graphs for each stock symbol. for(int i=0; i /// Removes data from an array of x-axis DateTimes for a graph /// so that only the most recent days are graphed. Number of days not removed /// is shown in numericUpDown2. /// /// An array of x-axis DateTimes to have data removed from. /// /// The same array of DateTimes, but with data removed. private DateTime[] removeData(DateTime[] data) { if(numericUpDown2.Value>data.GetLength(0)) return data; else { DateTime[] data2=new DateTime [(int)numericUpDown2.Value]; for(int i=0; i /// Removes data from an array of y-axis values for a graph /// so that only the most recent days are graphed. Number of days not removed /// is shown in numericUpDown2. /// /// An array of y-axis values to have data removed from. /// The same array of values, but with data removed. private float[] removeData(float[] data) { if (numericUpDown2.Value>data.GetLength(0)) return data; else { float[] data2=new float [(int)numericUpDown2.Value]; for(int i=0; i=0.25 && Math.Abs(gain-(int)gain)<0.75) { if(gain>0) gain=(int)gain+0.5F; if(gain<0) gain=(int)gain-0.5F; } if(Math.Abs(gain-(int)gain)>=0.75) { if(gain>0) gain=(int)gain+1; if(gain<0) gain=(int)gain-1; } if(gain>100) bgColors[i]=colors[0]; if(gain < -100) bgColors[i]=colors[400]; if(gain >= -100 && gain <= 100) bgColors[i]=colors[200-(int)(gain*2)]; } sr.Close(); //Web page. sr=new StreamReader(@"C:\daddy\MathCom\SP500.txt"); StreamWriter sw=new StreamWriter (@"C:\Documents and Settings\All Users\Documents\NYAReader\Generated Pages\SP500.html"); string symbol; sw.WriteLine("S&P 500 Components"); sw.WriteLine("

S&P 500 Components

"); sw.WriteLine(""); for(int j=0; j<10; j++) { symbol=sr.ReadLine(); sw.WriteLine(""); colorIndex++; } sw.WriteLine(""); } sw.WriteLine("
" +symbol+ "
"); sw.WriteLine(""); sw.WriteLine(""); sw.Close(); sr.Close(); } private void btnGenSPPage_Click(object sender, System.EventArgs e) { generateSPPage(); } private string[][] getSPData(bool download) { //Download and save page. if(download==true) { WebRequest request=WebRequest.Create("http://www.spglobal.com/sp500.csv"); Stream responseStream=request.GetResponse().GetResponseStream(); StreamReader sr2=new StreamReader(responseStream); string responseString=sr2.ReadToEnd(); sr2.Close(); StreamWriter sw=new StreamWriter (@"C:\Documents and Settings\All Users\Documents\NYAReader\Data\SPData.csv"); sw.Write(responseString); sw.Close(); } //Get stock symbols. String[][] symbols=new String[600][]; bool atEnd=false; string line; int i=0; StreamReader sr=new StreamReader(@"C:\Documents and Settings\All Users\Documents\NYAReader\Data\SPData.csv"); for(int j=0; j<5; j++) //First 5 lines are not needed. { sr.ReadLine(); } while(atEnd==false) { line=sr.ReadLine(); if(line==null) atEnd=true; else symbols[i]=csvSplit(line); i++; } sr.Close(); return symbols; } private void btnMakePage_Click(object sender, System.EventArgs e) { string stockList=""; bool atEnd=false; string currentLine=""; int numberOfLines=0; string symbol=""; StringBuilder path=new StringBuilder(), fileName=new StringBuilder(); //Select stock list. switch(lbStockList.SelectedIndex) { case 0: stockList=@"C:\daddy\MathCom\StockList.txt"; break; case 1: stockList=@"C:\daddy\MathCom\SP500.txt"; break; } //Find out how many lines are in the stock list file. StreamReader sr=new StreamReader(stockList); do { currentLine=sr.ReadLine(); if(currentLine==""||currentLine==null) atEnd=true; else numberOfLines+=1; } while(!atEnd); progressBar1.Maximum=numberOfLines; //Reset file pointer to the beginning. sr.BaseStream.Seek(0, SeekOrigin.Begin); sr.BaseStream.Position = 0; //Generate one web page for each stock symbol. for(int i=0; i=16 && a[i]<32) { x=a[i]-16; if(x<10) b[i]="1" + x.ToString(); if(x==10) b[i]="1A"; if(x==11) b[i]="1B"; if(x==12) b[i]="1C"; if(x==13) b[i]="1D"; if(x==14) b[i]="1E"; if(x==15) b[i]="1F"; } if(a[i]>=32 && a[i]<48) { x=a[i]-32; if(x<10) b[i]="2" + x.ToString(); if(x==10) b[i]="2A"; if(x==11) b[i]="2B"; if(x==12) b[i]="2C"; if(x==13) b[i]="2D"; if(x==14) b[i]="2E"; if(x==15) b[i]="2F"; } if(a[i]>=48 && a[i]<64) { x=a[i]-48; if(x<10) b[i]="3" + x.ToString(); if(x==10) b[i]="3A"; if(x==11) b[i]="3B"; if(x==12) b[i]="3C"; if(x==13) b[i]="3D"; if(x==14) b[i]="3E"; if(x==15) b[i]="3F"; } if(a[i]>=64 && a[i]<80) { x=a[i]-64; if(x<10) b[i]="4" + x.ToString(); if(x==10) b[i]="4A"; if(x==11) b[i]="4B"; if(x==12) b[i]="4C"; if(x==13) b[i]="4D"; if(x==14) b[i]="4E"; if(x==15) b[i]="4F"; } if(a[i]>=80 && a[i]<96) { x=a[i]-80; if(x<10) b[i]="5" + x.ToString(); if(x==10) b[i]="5A"; if(x==11) b[i]="5B"; if(x==12) b[i]="5C"; if(x==13) b[i]="5D"; if(x==14) b[i]="5E"; if(x==15) b[i]="5F"; } if(a[i]>=96 && a[i]<112) { x=a[i]-96; if(x<10) b[i]="6" + x.ToString(); if(x==10) b[i]="6A"; if(x==11) b[i]="6B"; if(x==12) b[i]="6C"; if(x==13) b[i]="6D"; if(x==14) b[i]="6E"; if(x==15) b[i]="6F"; } if(a[i]>=112 && a[i]<128) { x=a[i]-112; if(x<10) b[i]="7" + x.ToString(); if(x==10) b[i]="7A"; if(x==11) b[i]="7B"; if(x==12) b[i]="7C"; if(x==13) b[i]="7D"; if(x==14) b[i]="7E"; if(x==15) b[i]="7F"; } if(a[i]>=128 && a[i]<144) { x=a[i]-128; if(x<10) b[i]="8" + x.ToString(); if(x==10) b[i]="8A"; if(x==11) b[i]="8B"; if(x==12) b[i]="8C"; if(x==13) b[i]="8D"; if(x==14) b[i]="8E"; if(x==15) b[i]="8F"; } if(a[i]>=144 && a[i]<160) { x=a[i]-144; if(x<10) b[i]="9" + x.ToString(); if(x==10) b[i]="9A"; if(x==11) b[i]="9B"; if(x==12) b[i]="9C"; if(x==13) b[i]="9D"; if(x==14) b[i]="9E"; if(x==15) b[i]="9F"; } if(a[i]>=160 && a[i]<176) { x=a[i]-160; if(x<10) b[i]="A" + x.ToString(); if(x==10) b[i]="AA"; if(x==11) b[i]="AB"; if(x==12) b[i]="AC"; if(x==13) b[i]="AD"; if(x==14) b[i]="AE"; if(x==15) b[i]="AF"; } if(a[i]>=176 && a[i]<192) { x=a[i]-176; if(x<10) b[i]="B" + x.ToString(); if(x==10) b[i]="BA"; if(x==11) b[i]="BB"; if(x==12) b[i]="BC"; if(x==13) b[i]="BD"; if(x==14) b[i]="BE"; if(x==15) b[i]="BF"; } if(a[i]>=192 && a[i]<208) { x=a[i]-192; if(x<10) b[i]="C" + x.ToString(); if(x==10) b[i]="CA"; if(x==11) b[i]="CB"; if(x==12) b[i]="CC"; if(x==13) b[i]="CD"; if(x==14) b[i]="CE"; if(x==15) b[i]="CF"; } if(a[i]>=208 && a[i]<224) { x=a[i]-208; if(x<10) b[i]="D" + x.ToString(); if(x==10) b[i]="DA"; if(x==11) b[i]="DB"; if(x==12) b[i]="DC"; if(x==13) b[i]="DD"; if(x==14) b[i]="DE"; if(x==15) b[i]="DF"; } if(a[i]>=224 && a[i]<240) { x=a[i]-224; if(x<10) b[i]="E" + x.ToString(); if(x==10) b[i]="EA"; if(x==11) b[i]="EB"; if(x==12) b[i]="EC"; if(x==13) b[i]="ED"; if(x==14) b[i]="EE"; if(x==15) b[i]="EF"; } if(a[i]>=240) { x=a[i]-240; if(x<10) b[i]="F" + x.ToString(); if(x==10) b[i]="FA"; if(x==11) b[i]="FB"; if(x==12) b[i]="FC"; if(x==13) b[i]="FD"; if(x==14) b[i]="FE"; if(x==15) b[i]="FF"; } } string hexString=b[0]+b[1]+b[2]; return hexString; } } }