944,085 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 558
  • C# RSS
Nov 5th, 2009
0

Need Suggestion for Live Data

Expand Post »
Hi,

I want to write a small desktop application which will fetch Live stock price from NSE/BSE or Yahoo finance website. In order to create such application I need the guidance that how shoudl I proceed with?

The major problem I am facing is connecting a desktop application with intenet and fetching records.

Any help or guidenace is appreciated
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
vksingh24 is offline Offline
9 posts
since Nov 2009
Nov 5th, 2009
3
Re: Need Suggestion for Live Data
This should get you started:
C# Syntax (Toggle Plain Text)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Web;
  10. using System.Xml;
  11. using System.Net;
  12. using System.IO;
  13.  
  14.  
  15. namespace daniweb
  16. {
  17. public partial class frmStockRSS : Form
  18. {
  19. public frmStockRSS()
  20. {
  21. InitializeComponent();
  22. }
  23.  
  24. private static StockInfo GetStockInfo(string StockTicker)
  25. {
  26. if (string.IsNullOrEmpty(StockTicker))
  27. return null;
  28.  
  29. const string yhoo_url = @"http://download.finance.yahoo.com/d/quotes.csv?s={0}&f=sl1d1t1c1ohgv&e=.csv";
  30. string url = string.Format(yhoo_url, HttpUtility.UrlEncode(StockTicker.ToUpper()));
  31. HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
  32. req.Method = "GET";
  33. req.Referer = string.Empty;
  34.  
  35. HttpWebResponse objResponse = (HttpWebResponse)req.GetResponse();
  36. string resp = default(string);
  37. try
  38. {
  39. using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
  40. {
  41. resp = sr.ReadToEnd();
  42. sr.Close();
  43. }
  44. }
  45. catch (HttpException)
  46. {
  47. return null;
  48. }
  49. catch (IOException)
  50. {
  51. return null;
  52. }
  53.  
  54. StockInfo result = StockInfo.FromYahoo(resp);
  55. return result;
  56. }
  57.  
  58. private void button1_Click(object sender, EventArgs e)
  59. {
  60. StockInfo info = GetStockInfo("GOOG");
  61. Console.WriteLine(info.Ticker + ": " + info.LastTrade);
  62. info = GetStockInfo("YHOO");
  63. Console.WriteLine(info.Ticker + ": " + info.LastTrade);
  64. }
  65. }
  66. public class StockInfo
  67. {
  68. private string _ticker;
  69. private decimal _lastTrade;
  70. private DateTime _tickerTime;
  71. private decimal _change;
  72. private decimal _open;
  73. private decimal _high;
  74. private decimal _low;
  75. private int _unknown;
  76.  
  77. public string Ticker
  78. {
  79. get { return _ticker; }
  80. set { _ticker = value; }
  81. }
  82. public decimal LastTrade
  83. {
  84. get { return _lastTrade; }
  85. set { _lastTrade = value; }
  86. }
  87. public DateTime TickerTime
  88. {
  89. get { return _tickerTime; }
  90. set { _tickerTime = value; }
  91. }
  92. public decimal Change
  93. {
  94. get { return _change; }
  95. set { _change = value; }
  96. }
  97. public decimal Open
  98. {
  99. get { return _open; }
  100. set { _open = value; }
  101. }
  102. public decimal High
  103. {
  104. get { return _high; }
  105. set { _high = value; }
  106. }
  107. public decimal Low
  108. {
  109. get { return _low; }
  110. set { _low = value; }
  111. }
  112. public int Unknown
  113. {
  114. get { return _unknown; }
  115. set { _unknown = value; }
  116. }
  117. private StockInfo()
  118. {
  119. }
  120. public static StockInfo FromYahoo(string s)
  121. {
  122. if (string.IsNullOrEmpty(s))
  123. return null;
  124. s = s.Replace("\"", string.Empty);
  125.  
  126. string[] vals = s.Split(new char[] { ',' }, StringSplitOptions.None);
  127. if (vals.Length != 9)
  128. return null;
  129.  
  130. StockInfo result = new StockInfo();
  131. result.Ticker = vals[0];
  132. result.LastTrade = Convert.ToDecimal(vals[1]);
  133. result.TickerTime = Convert.ToDateTime(vals[2] + " " + vals[3]); //This is very unsafe.
  134. result.Change = Convert.ToDecimal(vals[4]);
  135. result.Open = Convert.ToDecimal(vals[5]);
  136. result.High = Convert.ToDecimal(vals[6]);
  137. result.Low = Convert.ToDecimal(vals[7]);
  138. result.Unknown = Convert.ToInt32(vals[8]); //dont know what this is
  139.  
  140. return result;
  141. }
  142. }
  143. }

Results in:
text Syntax (Toggle Plain Text)
  1. GOOG: 547.96
  2. YHOO: 15.90
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: How to display values from database into datagridview
Next Thread in C# Forum Timeline: From Javascript calling a Class in CS





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC