YahooCSVDataManager.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. namespace Easychart.Finance.DataProvider
  2. {
  3. using System;
  4. using System.Collections;
  5. using System.Globalization;
  6. using System.IO;
  7. public class YahooCSVDataManager : CacheDataManagerBase
  8. {
  9. private string Ext;
  10. private string Root;
  11. public YahooCSVDataManager()
  12. {
  13. base.CacheTimeSpan = TimeSpan.FromHours(12.0);
  14. }
  15. public YahooCSVDataManager(string Root, string Ext)
  16. {
  17. if (!Root.EndsWith(@"\"))
  18. {
  19. Root = Root + @"\";
  20. }
  21. this.Root = Root;
  22. if (!Ext.StartsWith("."))
  23. {
  24. Ext = "." + Ext;
  25. }
  26. this.Ext = Ext;
  27. }
  28. public override IDataProvider GetData(string Code, int Count)
  29. {
  30. string path = this.Root + Code + this.Ext;
  31. if (File.Exists(path))
  32. {
  33. using (StreamReader reader = new StreamReader(path))
  34. {
  35. CommonDataProvider provider = this.LoadYahooCSV(reader);
  36. provider.SetStringData("Code", Code);
  37. return provider;
  38. }
  39. }
  40. return CommonDataProvider.Empty;
  41. }
  42. public CommonDataProvider LoadYahooCSV(Stream stream)
  43. {
  44. using (StreamReader reader = new StreamReader(stream))
  45. {
  46. return this.LoadYahooCSV(reader);
  47. }
  48. }
  49. public CommonDataProvider LoadYahooCSV(StreamReader sr)
  50. {
  51. string[] strArray = sr.ReadToEnd().Trim().Split(new char[] { '\n' });
  52. ArrayList list = new ArrayList();
  53. for (int i = 1; i < strArray.Length; i++)
  54. {
  55. strArray[i] = strArray[i].Trim();
  56. if (!strArray[i].StartsWith("<!--"))
  57. {
  58. list.Add(strArray[i]);
  59. }
  60. }
  61. int count = list.Count;
  62. double[] numArray = new double[count];
  63. double[] numArray2 = new double[count];
  64. double[] numArray3 = new double[count];
  65. double[] numArray4 = new double[count];
  66. double[] numArray5 = new double[count];
  67. double[] numArray6 = new double[count];
  68. double[] numArray7 = new double[count];
  69. DateTimeFormatInfo invariantInfo = DateTimeFormatInfo.InvariantInfo;
  70. NumberFormatInfo info2 = NumberFormatInfo.InvariantInfo;
  71. for (int j = 0; j < count; j++)
  72. {
  73. string[] strArray2 = ((string) list[j]).Split(new char[] { ',' });
  74. if (strArray2.Length < 7)
  75. {
  76. string[] strArray3 = new string[7];
  77. for (int k = 0; k < strArray2.Length; k++)
  78. {
  79. strArray3[k] = strArray2[k];
  80. }
  81. if (strArray2.Length == 6)
  82. {
  83. strArray3[6] = strArray2[4];
  84. }
  85. if (strArray2.Length == 2)
  86. {
  87. for (int m = 2; m < strArray3.Length; m++)
  88. {
  89. strArray3[m] = strArray2[1];
  90. }
  91. }
  92. strArray2 = strArray3;
  93. }
  94. int index = (count - j) - 1;
  95. numArray6[index] = DateTime.ParseExact(strArray2[0], "%d-MMM-yy", invariantInfo).ToOADate();
  96. numArray2[index] = double.Parse(strArray2[1], info2);
  97. numArray3[index] = double.Parse(strArray2[2], info2);
  98. numArray4[index] = double.Parse(strArray2[3], info2);
  99. numArray[index] = double.Parse(strArray2[4], info2);
  100. numArray5[index] = double.Parse(strArray2[5], info2);
  101. numArray7[index] = double.Parse(strArray2[6], info2);
  102. }
  103. CommonDataProvider provider = new CommonDataProvider(this);
  104. provider.LoadBinary(new double[][] { numArray2, numArray3, numArray4, numArray, numArray5, numArray6, numArray7 });
  105. return provider;
  106. }
  107. public CommonDataProvider LoadYahooCSV(byte[] data)
  108. {
  109. return this.LoadYahooCSV(new MemoryStream(data));
  110. }
  111. public CommonDataProvider LoadYahooCSV(string FileName)
  112. {
  113. return this.LoadYahooCSV(File.OpenRead(FileName));
  114. }
  115. public override void SaveData(string Symbol, IDataProvider idp, Stream OutStream, DateTime Start, DateTime End, bool Intraday)
  116. {
  117. int count = idp.Count;
  118. double[] numArray = idp["DATE"];
  119. double[] numArray2 = idp["OPEN"];
  120. double[] numArray3 = idp["HIGH"];
  121. double[] numArray4 = idp["LOW"];
  122. double[] numArray5 = idp["CLOSE"];
  123. double[] numArray6 = idp["VOLUME"];
  124. double[] numArray7 = idp["ADJCLOSE"];
  125. using (StreamWriter writer = new StreamWriter(OutStream))
  126. {
  127. writer.WriteLine("Date,Open,High,Low,Close,Volume,Adj. Close*");
  128. double num2 = Start.ToOADate();
  129. double num3 = End.ToOADate();
  130. for (int i = count - 1; i >= 0; i--)
  131. {
  132. if ((numArray[i] >= num2) && (numArray[i] <= num3))
  133. {
  134. writer.WriteLine(DateTime.FromOADate(numArray[i]).ToString("dd-MMM-yy", DateTimeFormatInfo.InvariantInfo) + "," + numArray2[i].ToString("f2") + "," + numArray3[i].ToString("f2") + "," + numArray4[i].ToString("f2") + "," + numArray5[i].ToString("f2") + "," + numArray6[i].ToString("f0") + "," + numArray7[i].ToString("f2"));
  135. }
  136. }
  137. }
  138. }
  139. }
  140. }