Friday, September 30, 2011

Online Stock streaming In Asp.net


Online Stock Streaming using ASP.Net

 Download Source Code




Online Stock Streaming

Introduction

It always made me wonder how sites like http://finance.yahoo.com and http://finance.google.com display stock quotes online so quickly and without refreshing entire page. So I decided to implement something similar. It looks very difficult to implement these kind of streaming, but ASP.Net and it's feature rich AJAX controls makes it really simple to implement this. This is my first attempt to build similar streaming web page. This sample may sound fairly easy to you and some of you might think that everyone knows this. But it might provide a good base for those who wants to build similar web sites and still have no idea where to start.

Using Code

Sample code provided in this article includes a web page Default.aspx and Default.aspx.cs code behind.
Default.aspx (UI)
UI code is fairly simple. First component is Script Manager. This component is necessary for AJAX functionality. After Script Manager, next component is UpdatePanel. This ajax control stops ASP.Net page from posting back. Instead of posting back page, it sends control inputs to server using client side javascripts. When it receives response from server, it refreshes content inside it on client side. Next control is an AJAX timer control. It rebinds grid with new quotes every 2 seconds.
Default.aspx.cs (Code Behind)
01.DataTable GetGridData()
02.{
03.DataTable DT = new DataTable();
04.DT.Columns.Add("Symbol",typeof(string));
05.DT.Columns.Add("Company Name",typeof(string));
06.DT.Columns.Add("LastTrade", typeof(float));
07.DT.Columns.Add("Bid", typeof(float));
08.DT.Columns.Add("Ask", typeof(float));
09.DT.Columns.Add("Volume", typeof(int));
10.DT.Columns.Add("Market Capital", typeof(string));
11.DT.Columns.Add("EPS", typeof(float));
12.DT.Columns.Add("P/E", typeof(float));
13. 
14.Random r = new Random();
15. 
16.//Apple
17. 
18.//Get a random value of last trade, bid, ask and volume.
19. 
20.float LastTrade = ((float)r.Next(17222,17434))/100;
21.float Bid = LastTrade - (float)0.34;
22.float Ask = LastTrade + (float)0.43;
23.int Volume = r.Next(23000000, 23600000);
24.DT.Rows.Add("AAPL", "Apple Inc.", LastTrade, Bid, Ask,
25.Volume, "153.88B", 3.93, 44.71);
26. 
27.//Microsoft
28. 
29.LastTrade = ((float)r.Next(3378, 3487)) / 100;
30.Bid = LastTrade - (float)0.34;
31.Ask = LastTrade + (float)0.43;
32.Volume = r.Next(38064903, 40075689);
33.DT.Rows.Add("MSFT", "Microsoft Corporation", LastTrade,
34.Bid, Ask, Volume, "153.88B", 2.13, 21.2);
35. 
36.//Yahoo
37. 
38.LastTrade = ((float)r.Next(2520, 2834)) / 100;
39.Bid = LastTrade - (float)0.34;
40.Ask = LastTrade + (float)0.43;
41.Volume = r.Next(14400000, 15500000);
42.DT.Rows.Add("YHOO", "Yahoo Inc.", LastTrade, Bid, Ask,
43.Volume, "34.48B", 1.42, 10.23);
44. 
45.//Google
46. 
47.LastTrade = ((float)r.Next(67025, 69000)) / 100;
48.Bid = LastTrade - (float)5;
49.Ask = LastTrade + (float)5;
50.Volume = r.Next(4500000, 5000000);
51.DT.Rows.Add("GOOG", "Google Inc.", LastTrade, Bid, Ask,
52.Volume, "213.73B", 12.42, 53.23);
53. 
54.//WYNN
55. 
56.LastTrade = ((float)r.Next(13249, 14022)) / 100;
57.Bid = LastTrade - (float)0.90;
58.Ask = LastTrade + (float)0.89;
59.Volume = r.Next(1222785, 1400000);
60.DT.Rows.Add("WYNN", "Wynn Resorts Ltd.", LastTrade, Bid, Ask,
61.Volume, "15.13B",1.81, 73.15);
62. 
63.return DT;
64.}
65. 
GetGridData() function simulates quotes for different stock symbols. Instead of simulated data you can replace this function to fetch real time quotes from other sources. This function builds a data table using random quotes and returns it.
1.protected void Timer1_Tick(object sender, EventArgs e)
2.{
3.//Bind grid view
4.GridView1.DataSource = GetGridData();
5. 
6.GridView1.DataBind();
7.}
Timer1_Tick() function is a timer function which executes every 2 seconds. It rebinds grid with simulated stock data.