YahooDataManager.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. namespace Easychart.Finance.DataProvider
  2. {
  3. using Easychart.Finance;
  4. using System;
  5. using System.Net;
  6. using System.Text;
  7. public class YahooDataManager : YahooCSVDataManager
  8. {
  9. public static string DownloadRealtimeFromYahoo(string Code, string Param)
  10. {
  11. string str = "";
  12. string str2 = Code.ToLower();
  13. Encoding encoding = Encoding.ASCII;
  14. byte[] bytes = FormulaHelper.DownloadData(string.Format("http://" + str + "finance.yahoo.com/d/quotes.csv?s=" + Code + "&f=" + Param, Code));
  15. return encoding.GetString(bytes);
  16. }
  17. public override IDataProvider GetData(string Code, int Count)
  18. {
  19. DateTime date;
  20. IDataProvider provider2;
  21. if (Count > 0x6815)
  22. {
  23. Count = 0x6815;
  24. date = new DateTime(0x78a, 1, 1);
  25. }
  26. else
  27. {
  28. date = DateTime.Now.AddDays((double) -Count).Date;
  29. }
  30. DateTime now = DateTime.Now;
  31. if (base.EndTime != DateTime.MinValue)
  32. {
  33. now = base.EndTime.AddDays(1.0);
  34. }
  35. if (base.StartTime != DateTime.MinValue)
  36. {
  37. date = base.StartTime;
  38. }
  39. string format = "http://table.finance.yahoo.com/table.csv?s={0}&a={1}&b={2}&c={3}&d={4}&e={5}&f={6}&g=d&ignore=.csv";
  40. string uRL = string.Format(format, new object[] { Code, date.Month - 1, date.Day, date.Year, now.Month - 1, now.Day, now.Year });
  41. try
  42. {
  43. byte[] data = FormulaHelper.DownloadData(uRL);
  44. CommonDataProvider cdp = base.LoadYahooCSV(data);
  45. this.SetStrings(cdp, Code);
  46. provider2 = cdp;
  47. }
  48. catch (WebException)
  49. {
  50. throw new Exception("Symbol " + Code + " not found!");
  51. }
  52. catch (Exception exception)
  53. {
  54. throw new Exception("Invalid data format!" + Code + ";" + exception.Message);
  55. }
  56. return provider2;
  57. }
  58. public static string[] GetStockName(string Code)
  59. {
  60. string[] strArray = new string[0];
  61. string str = DownloadRealtimeFromYahoo(Code, "snx");
  62. if (str != null)
  63. {
  64. strArray = str.Split(new char[] { ',' });
  65. for (int i = 0; i < 3; i++)
  66. {
  67. strArray[i] = strArray[i].Trim();
  68. if (strArray[i].Length > 2)
  69. {
  70. strArray[i] = strArray[i].Substring(1, strArray[i].Length - 2);
  71. strArray[i] = strArray[i].Trim();
  72. }
  73. }
  74. }
  75. return strArray;
  76. }
  77. public override void SetStrings(CommonDataProvider cdp, string Code)
  78. {
  79. cdp.SetStringData("Code", Code);
  80. }
  81. public static string TryDownloadRealtimeFromYahoo(string Code, string Param)
  82. {
  83. for (int i = 0; i < 5; i++)
  84. {
  85. try
  86. {
  87. return DownloadRealtimeFromYahoo(Code, Param);
  88. }
  89. catch
  90. {
  91. }
  92. }
  93. return null;
  94. }
  95. }
  96. }