DataPacket.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. namespace Easychart.Finance.DataProvider
  2. {
  3. using Easychart.Finance;
  4. using System;
  5. using System.Globalization;
  6. using System.Reflection;
  7. public class DataPacket
  8. {
  9. public double AdjClose;
  10. public double Close;
  11. public DateTime Date;
  12. public string Exchange;
  13. public double High;
  14. public double Last;
  15. public double Low;
  16. public static int MaxValue = 0x591c8;
  17. public double Open;
  18. public static int PacketByteSize = (PacketSize * 4);
  19. public static int PacketSize = 9;
  20. public string StockName;
  21. public string Symbol;
  22. public double TimeZone;
  23. public double Volume;
  24. public DataPacket(string Symbol, DateTime QuoteTime, double Price, double Volume) : this(Symbol, QuoteTime, 0.0, 0.0, 0.0, Price, Volume, Price, 0.0)
  25. {
  26. }
  27. public DataPacket(string Symbol, double DoubleDate, double Price, double Volume) : this(Symbol, DateTime.FromOADate(DoubleDate), Price, Volume)
  28. {
  29. }
  30. public DataPacket(string Symbol, DateTime Date, double Open, double High, double Low, double Close, double Volume, double AdjClose) : this(Symbol, Date, Open, High, Low, Close, Volume, AdjClose, 0.0)
  31. {
  32. }
  33. public DataPacket(string Symbol, double DoubleDate, double Open, double High, double Low, double Close, double Volume, double AdjClose) : this(Symbol, DoubleDate, Open, High, Low, Close, Volume, AdjClose, 0.0)
  34. {
  35. }
  36. public DataPacket(string Symbol, DateTime Date, double Open, double High, double Low, double Close, double Volume, double AdjClose, double Last)
  37. {
  38. this.Symbol = Symbol;
  39. this.Date = Date;
  40. this.Close = Close;
  41. if (AdjClose == 0.0)
  42. {
  43. this.AdjClose = Close;
  44. }
  45. else
  46. {
  47. this.AdjClose = AdjClose;
  48. }
  49. if (Open == 0.0)
  50. {
  51. this.Open = Close;
  52. }
  53. else
  54. {
  55. this.Open = Open;
  56. }
  57. if (High == 0.0)
  58. {
  59. this.High = Close;
  60. }
  61. else
  62. {
  63. this.High = High;
  64. }
  65. if (Low == 0.0)
  66. {
  67. this.Low = Close;
  68. }
  69. else
  70. {
  71. this.Low = Low;
  72. }
  73. this.Volume = Volume;
  74. this.Last = Last;
  75. }
  76. public DataPacket(string Symbol, double DoubleDate, double Open, double High, double Low, double Close, double Volume, double AdjClose, double Last) : this(Symbol, DateTime.FromOADate(DoubleDate), Open, High, Low, Close, Volume, AdjClose, Last)
  77. {
  78. }
  79. public static DataPacket DownloadFromYahoo(string Code)
  80. {
  81. return ParseYahoo(GetFromYahoo(Code));
  82. }
  83. public static DataPacket[] DownloadMultiFromYahoo(string Codes)
  84. {
  85. string[] strArray = GetFromYahoo(Codes).Trim().Split(new char[] { '\r' });
  86. DataPacket[] packetArray = new DataPacket[strArray.Length];
  87. for (int i = 0; i < strArray.Length; i++)
  88. {
  89. packetArray[i] = ParseYahoo(strArray[i]);
  90. }
  91. return packetArray;
  92. }
  93. public static DataPacket FromBytes(byte[] bs)
  94. {
  95. float[] dst = new float[PacketSize];
  96. Buffer.BlockCopy(bs, 0, dst, 0, PacketByteSize);
  97. double[] numArray2 = new double[1];
  98. Buffer.BlockCopy(dst, 0, numArray2, 0, 8);
  99. double doubleDate = numArray2[0];
  100. float num2 = dst[2];
  101. float num3 = dst[3];
  102. float num4 = dst[4];
  103. float num5 = dst[5];
  104. Buffer.BlockCopy(dst, 0x18, numArray2, 0, 8);
  105. double volume = numArray2[0];
  106. float num7 = dst[8];
  107. return new DataPacket("", doubleDate, (double) num2, (double) num3, (double) num4, (double) num5, volume, (double) num7);
  108. }
  109. public static DateTime GetDateTime(float[] fs)
  110. {
  111. return GetDateTime(fs, 0);
  112. }
  113. public static DateTime GetDateTime(float[] fs, int i)
  114. {
  115. DateTime time;
  116. double[] dst = new double[1];
  117. try
  118. {
  119. Buffer.BlockCopy(fs, i * PacketByteSize, dst, 0, 8);
  120. time = DateTime.FromOADate(dst[0]);
  121. }
  122. catch (Exception exception)
  123. {
  124. throw new Exception(string.Concat(new object[] { exception.Message, ";", dst[0], ";i=", i, ";Length=", fs.Length / PacketByteSize }));
  125. }
  126. return time;
  127. }
  128. public DateTime GetExchangeTime(ExchangeIntraday ei)
  129. {
  130. if (this.TimeZone == double.MaxValue)
  131. {
  132. return this.Date;
  133. }
  134. return this.Date.AddHours(ei.TimeZone);
  135. }
  136. public float[] GetFloat()
  137. {
  138. float[] dst = new float[PacketSize];
  139. double[] src = new double[] { this.DoubleDate };
  140. Buffer.BlockCopy(src, 0, dst, 0, 8);
  141. dst[2] = (float) this.Open;
  142. dst[3] = (float) this.High;
  143. dst[4] = (float) this.Low;
  144. dst[5] = (float) this.Close;
  145. src[0] = this.Volume;
  146. Buffer.BlockCopy(src, 0, dst, 0x18, 8);
  147. dst[8] = (float) this.AdjClose;
  148. return dst;
  149. }
  150. private static string GetFromYahoo(string Code)
  151. {
  152. return YahooDataManager.DownloadRealtimeFromYahoo(Code, "snd1t1oml1vpx");
  153. }
  154. public static double GetVolume(float[] fs)
  155. {
  156. double[] dst = new double[1];
  157. Buffer.BlockCopy(fs, 0x18, dst, 0, 8);
  158. return dst[0];
  159. }
  160. public bool Merge(DataPacket dp, DataCycle dc)
  161. {
  162. if (!dc.SameSequence(dp.Date, this.Date))
  163. {
  164. this.Open = dp.Close;
  165. this.High = dp.Close;
  166. this.Low = dp.Close;
  167. this.Close = dp.Close;
  168. this.AdjClose = dp.Close;
  169. this.Volume = dp.Volume;
  170. this.Date = dp.Date;
  171. return false;
  172. }
  173. this.Close = dp.Close;
  174. this.AdjClose = this.Close;
  175. this.High = Math.Max(this.High, this.Close);
  176. this.Low = Math.Min(this.Low, this.Close);
  177. this.Volume = dp.Volume;
  178. this.Date = dp.Date;
  179. return true;
  180. }
  181. public static DataPacket ParseEODData(string s)
  182. {
  183. string[] strArray = s.Split(new char[] { ',' });
  184. IFormatProvider uSFormat = FormulaHelper.USFormat;
  185. try
  186. {
  187. for (int i = 0; i < strArray.Length; i++)
  188. {
  189. strArray[i] = strArray[i].Trim();
  190. }
  191. return new DataPacket(strArray[0], DateTime.Parse(strArray[1], uSFormat), double.Parse(strArray[2], uSFormat), double.Parse(strArray[3], uSFormat), double.Parse(strArray[4], uSFormat), double.Parse(strArray[5], uSFormat), double.Parse(strArray[6], uSFormat), double.Parse(strArray[5], uSFormat));
  192. }
  193. catch
  194. {
  195. }
  196. return null;
  197. }
  198. public static DataPacket ParseYahoo(string s)
  199. {
  200. try
  201. {
  202. string[] strArray = FormulaHelper.Split(s, ',');
  203. string[] strArray2 = RemoveQuotation(strArray[5]).Split(new char[] { '-' });
  204. double def = 0.0;
  205. if (strArray.Length > 8)
  206. {
  207. def = ToDouble(strArray[8], def);
  208. }
  209. IFormatProvider invariantInfo = DateTimeFormatInfo.InvariantInfo;
  210. DataPacket packet = new DataPacket(RemoveQuotation(strArray[0]), DateTime.ParseExact(RemoveQuotation(strArray[2]) + " " + RemoveQuotation(strArray[3]), "M/%d/yyyy h:mmtt", invariantInfo, DateTimeStyles.AllowWhiteSpaces), ToDouble(strArray[4], 0.0), ToDouble(strArray2[1], 0.0), ToDouble(strArray2[0], 0.0), ToDouble(strArray[6], 0.0), ToDouble(strArray[7], 0.0), ToDouble(strArray[6], 0.0), def);
  211. packet.StockName = RemoveQuotation(strArray[1]);
  212. packet.TimeZone = ExchangeIntraday.YahooTimeZone;
  213. if (strArray.Length > 9)
  214. {
  215. packet.Exchange = RemoveQuotation(strArray[9]);
  216. }
  217. return packet;
  218. }
  219. catch
  220. {
  221. return null;
  222. }
  223. }
  224. public static string RemoveQuotation(string s)
  225. {
  226. return s.Trim(new char[] { ' ', '"', '\r', '\n' });
  227. }
  228. public byte[] ToByte()
  229. {
  230. byte[] dst = new byte[PacketByteSize];
  231. Buffer.BlockCopy(this.GetFloat(), 0, dst, 0, dst.Length);
  232. return dst;
  233. }
  234. public static double ToDouble(string s, double Def)
  235. {
  236. try
  237. {
  238. IFormatProvider uSFormat = FormulaHelper.USFormat;
  239. return double.Parse(s, uSFormat);
  240. }
  241. catch
  242. {
  243. return Def;
  244. }
  245. }
  246. public static float ToFloat(string s, float Def)
  247. {
  248. try
  249. {
  250. return float.Parse(s, NumberFormatInfo.InvariantInfo);
  251. }
  252. catch
  253. {
  254. return Def;
  255. }
  256. }
  257. public double DoubleDate
  258. {
  259. get
  260. {
  261. return this.Date.ToOADate();
  262. }
  263. }
  264. public bool IsZeroValue
  265. {
  266. get
  267. {
  268. return ((((this.Open == 0.0) || (this.High == 0.0)) || (this.Low == 0.0)) || (this.Close == 0.0));
  269. }
  270. }
  271. public double this[string Type]
  272. {
  273. get
  274. {
  275. switch (Type.ToUpper())
  276. {
  277. case "OPEN":
  278. return this.Open;
  279. case "DATE":
  280. return this.DoubleDate;
  281. case "HIGH":
  282. return this.High;
  283. case "LOW":
  284. return this.Low;
  285. case "CLOSE":
  286. return this.Close;
  287. case "VOLUME":
  288. return this.Volume;
  289. case "ADJCLOSE":
  290. return this.AdjClose;
  291. }
  292. return double.NaN;
  293. }
  294. }
  295. }
  296. }