YahooCSVDataManager.cs 6.0 KB

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