Friday 8 January 2010

Stocks Displayer c#

I decided i wanted to be able to know what the stocks are for the yen to my local currency(shekel), but didn't like the bank`s graph(they put days that do not have trade on the graph), so i took the raw data, and made a graph.
Had to create a list for easy checking of the parsed data.

It might be interesting to someone that wants to know how to make a graph on c# or how to parse data without regex.
public partial class Form1 : Form
{
List<data> lst=new List<data>();

public Form1()
{

InitializeComponent();
string url="http://www.bankisrael.gov.il/eng.shearim/select_cur/cur4sdh.php?curname%5B%5D=_31%2B1977-10-31%2B%2BYen+-+Japan%A0&numselect=1&curfile=htm&fyear=2008&fmonth=12&fday=31&lyear=2009&lmonth=12&lday=31&cboday=01&cboyear_from=2008&cboyear_to=2009&lastwom=93&whosend=last";
string shitload;
System.Net.WebClient wc=new System.Net.WebClient();
shitload=wc.DownloadString(url);

//parsing
shitload=shitload.Remove(0,shitload.IndexOf("Exchange rates - Last 3 months"));
shitload=shitload.Remove(0,shitload.IndexOf("<TABLE width"));
shitload=shitload.Remove(shitload.IndexOf("</table>"));
string date;
try{
while (shitload.Length>100)
{
shitload=shitload.Remove(0,shitload.IndexOf("nowrap>")+7);
date=shitload.Substring(0,10);

shitload=shitload.Remove(0,shitload.IndexOf("nowrap>")+7);

lst.Add(new data(date,shitload.Substring(0,8)));

}
}
catch{}

label1.Text=shitload;

foreach (data dta in lst)
listBox1.Items.Add(dta.date+" "+dta.value);


this.Paint += new PaintEventHandler(paintgraph);


}
private void paintgraph(object sender,PaintEventArgs e)
{
Graphics g=e.Graphics;
g.Clear(Color.Wheat);
Pen pn=new Pen(new SolidBrush(Color.Red),2);
float min,max;
int mini=0,maxi=0;
min=100;max=0;
for (int i=0;i<lst.Count;i++)
{
if (lst[i].value<min) {min=lst[i].value;mini=i;}
if (lst[i].value>max) {max=lst[i].value;maxi=i;}
}
int inc=this.Width/lst.Count;
float dif=max-min;
float incy=(this.Height-40)/dif;
for(int i=0;i<lst.Count-1;i++)
{
g.DrawLine(pn,i*inc,(-lst[i].value+max)*incy,(i+1)*inc,(-lst[i+1].value+max)*incy);
Application.DoEvents();
}
if (lst.Count-1!=maxi)
g.DrawString(lst[maxi].date+" "+lst[maxi].value,new Font("Verdana",10),new SolidBrush(Color.Tomato),maxi*inc,(-lst[maxi].value+max)*incy);
else
g.DrawString(lst[lst.Count-1].date+" "+lst[lst.Count-1].value,new Font("Verdana",10),new SolidBrush(Color.Tomato),(lst.Count-1)*inc-130,(-lst[lst.Count-1].value+max)*incy);
if (lst.Count-1!=mini)
g.DrawString(lst[mini].date+" "+lst[mini].value,new Font("Verdana",10),new SolidBrush(Color.Tomato),mini*inc,(-lst[mini].value+max)*incy-20);
else
g.DrawString(lst[lst.Count-1].date+" "+lst[lst.Count-1].value,new Font("Verdana",10),new SolidBrush(Color.Tomato),(lst.Count-1)*inc-130,(-lst[lst.Count-1].value+max)*incy-20);
//if ((lst.Count-1!=maxi)&&(lst.Count-1!=mini))


}


void Form1ResizeEnd(object sender, EventArgs e)
{
this.Refresh();
}
}

public class data
{
public string date;
public float value;
public data(string d,string v)
{
date=d;
value=(float)System.Convert.ToDouble(v);
}
}

1 comment:

  1. btw, to save yourself lots of troube, if you need to use <, just write &lt‎;

    ReplyDelete