YahooDataClient.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. namespace Easychart.Finance.DataClient
  2. {
  3. using Easychart.Finance;
  4. using Easychart.Finance.DataProvider;
  5. using System;
  6. using System.Collections;
  7. using System.Runtime.CompilerServices;
  8. using System.Text.RegularExpressions;
  9. using System.Threading;
  10. public class YahooDataClient : DataClientBase
  11. {
  12. public event DataProgress OnProgress;
  13. public event StreamingDataChanged OnStreamingData;
  14. public event EventHandler OnStreamingStopped;
  15. public override void DownloadStreaming()
  16. {
  17. try
  18. {
  19. while (true)
  20. {
  21. foreach (DataPacket packet in DataPacket.DownloadMultiFromYahoo(base.GetStreamingSymbol(",")))
  22. {
  23. if ((packet != null) && !packet.IsZeroValue)
  24. {
  25. if (base.UtcStreamingTime)
  26. {
  27. packet.Date = packet.Date.Date + packet.Date.AddHours(-packet.TimeZone).TimeOfDay;
  28. }
  29. if (this.OnStreamingData != null)
  30. {
  31. this.OnStreamingData(this, packet);
  32. }
  33. }
  34. }
  35. Thread.Sleep(0x1388);
  36. }
  37. }
  38. finally
  39. {
  40. if (this.OnStreamingStopped != null)
  41. {
  42. this.OnStreamingStopped(this, new EventArgs());
  43. }
  44. }
  45. }
  46. public override CommonDataProvider GetData(string symbols, DataCycle dataCycle, DateTime startTime, DateTime endTime)
  47. {
  48. string format = "http://table.finance.yahoo.com/table.csv?s={0}&d={4}&e={5}&f={6}&g=d&a={1}&b={2}&c={3}&ignore=.csv";
  49. string uRL = string.Format(format, new object[] { symbols, startTime.Month - 1, startTime.Day, startTime.Year, endTime.Month - 1, endTime.Day, endTime.Year });
  50. byte[] data = base.DownloadBinary(symbols, uRL);
  51. return new YahooCSVDataManager().LoadYahooCSV(data);
  52. }
  53. public override DataPacket[] GetEodData(string Exchanges, string[] symbols, DateTime time)
  54. {
  55. string format = "http://table.finance.yahoo.com/table.csv?s={0}&d={4}&e={5}&f={6}&g=d&a={1}&b={2}&c={3}&ignore=.csv";
  56. DataPacket[] packetArray = new DataPacket[symbols.Length];
  57. DateTime time2 = time.AddDays(-1.0);
  58. for (int i = 0; i < symbols.Length; i++)
  59. {
  60. string symbol = symbols[i];
  61. string uRL = string.Format(format, new object[] { symbol, time2.Month - 1, time2.Day, time2.Year, time.Month - 1, time.Day, time.Year });
  62. byte[] data = base.DownloadBinary(symbol, uRL);
  63. packetArray[i] = new YahooCSVDataManager().LoadYahooCSV(data).GetLastPackage();
  64. if (this.OnProgress != null)
  65. {
  66. this.OnProgress(this, new DataProgressArgs(symbol, i, symbols.Length));
  67. }
  68. }
  69. return packetArray;
  70. }
  71. public override string[] GetMarket()
  72. {
  73. return new string[] { "US=U.S. & Canada", "=World Markets" };
  74. }
  75. public override string[] GetStockType()
  76. {
  77. return new string[] { "S=Stocks", "E=ETF", "I=Indices", "M=Mutual Fund", "O=Options" };
  78. }
  79. public override string[] LookupSymbols(string Key, string Exchanges, string StockType, string Market)
  80. {
  81. if (StockType == null)
  82. {
  83. StockType = "";
  84. }
  85. if (Market == null)
  86. {
  87. Market = "";
  88. }
  89. MatchCollection matchs = Regex.Matches(base.DownloadString("http://finance.yahoo.com/l?s=" + Key + "&t=" + StockType + "&m=" + Market), "<TR bgcolor=#ffffff><TD><font face=arial size=-1><a href=(?<href>[^>]+)>(?<symbol>[^<]+)</a></font></TD><TD><font face=arial size=-1>(?<name>[^<]+)</font></TD><TD><font face=arial size=-1>(?<exchange>[^<]+)</font>", RegexOptions.IgnoreCase);
  90. ArrayList list = new ArrayList();
  91. foreach (Match match in matchs)
  92. {
  93. list.Add(match.Groups["symbol"].Value + ";" + match.Groups["name"].Value + ";" + match.Groups["exchange"].Value);
  94. }
  95. return (string[]) list.ToArray(typeof(string));
  96. }
  97. public override string DataFeedName
  98. {
  99. get
  100. {
  101. return "Yahoo finance";
  102. }
  103. }
  104. public override string Description
  105. {
  106. get
  107. {
  108. return "Provided by yahoo finance";
  109. }
  110. }
  111. public override string HomePage
  112. {
  113. get
  114. {
  115. return "http://finance.yahoo.com";
  116. }
  117. }
  118. public override bool IsFree
  119. {
  120. get
  121. {
  122. return true;
  123. }
  124. }
  125. public override int MaxSymbolsForStreaming
  126. {
  127. get
  128. {
  129. return 100;
  130. }
  131. }
  132. public override bool NeedLogin
  133. {
  134. get
  135. {
  136. return false;
  137. }
  138. }
  139. public override bool SupportEod
  140. {
  141. get
  142. {
  143. return false;
  144. }
  145. }
  146. public override bool SupportIntraday
  147. {
  148. get
  149. {
  150. return false;
  151. }
  152. }
  153. }
  154. }