CommonDataProvider.cs 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  1. namespace Easychart.Finance.DataProvider
  2. {
  3. using Easychart.Finance;
  4. using System;
  5. using System.Collections;
  6. using System.IO;
  7. using System.Reflection;
  8. public class CommonDataProvider : IDataProvider
  9. {
  10. private bool adjusted = true;
  11. private IDataProvider baseDataProvider;
  12. private Easychart.Finance.DataCycle dataCycle = Easychart.Finance.DataCycle.Day;
  13. private MergeCycleType dateMergeType = MergeCycleType.OPEN;
  14. private IDataManager dm;
  15. private int futureBars;
  16. private Hashtable htAllCycle = new Hashtable();
  17. private Hashtable htConstData = new Hashtable();
  18. private Hashtable htData = new Hashtable();
  19. private Hashtable htRealtime = new Hashtable();
  20. private Hashtable htStringData = new Hashtable();
  21. private ExchangeIntraday intradayInfo;
  22. private bool isPointAndFigure;
  23. private static string[] Keys = new string[] { "DATE", "OPEN", "HIGH", "LOW", "CLOSE", "VOLUME", "ADJCLOSE", "TURNOVERS" };
  24. private int maxCount = -1;
  25. private int weekAdjust;
  26. public CommonDataProvider(IDataManager dm)
  27. {
  28. this.dm = dm;
  29. }
  30. private double[] AdjustByBase(double[] Date, double[] dd)
  31. {
  32. double[] numArray = this.BaseDataProvider["DATE"];
  33. double[] numArray2 = new double[numArray.Length];
  34. for (int i = 0; i < numArray.Length; i++)
  35. {
  36. numArray2[i] = double.NaN;
  37. }
  38. int index = dd.Length - 1;
  39. int num3 = numArray.Length - 1;
  40. while ((num3 >= 0) && (index >= 0))
  41. {
  42. if (numArray[num3] == Date[index])
  43. {
  44. numArray2[num3--] = dd[index--];
  45. }
  46. else
  47. {
  48. if (numArray[num3] > Date[index])
  49. {
  50. num3--;
  51. continue;
  52. }
  53. index--;
  54. }
  55. }
  56. return numArray2;
  57. }
  58. public static void AppendStreamingData(string Path, StreamingData sd)
  59. {
  60. using (FileStream stream = File.OpenWrite(string.Format(Path, sd.Symbol)))
  61. {
  62. using (BinaryWriter writer = new BinaryWriter(stream))
  63. {
  64. writer.Write(sd.QuoteTime.ToOADate());
  65. writer.Write(sd.Price);
  66. writer.Write(sd.Volume);
  67. }
  68. }
  69. }
  70. public void ClearData()
  71. {
  72. this.htData.Clear();
  73. for (int i = 0; i < Keys.Length; i++)
  74. {
  75. this.htData.Add(Keys[i], new double[0]);
  76. }
  77. this.htAllCycle.Clear();
  78. }
  79. public void DeleteData(DateTime FromTime, DateTime ToTime)
  80. {
  81. ArrayList[] listArray = new ArrayList[Keys.Length];
  82. for (int i = 0; i < listArray.Length; i++)
  83. {
  84. listArray[i] = new ArrayList();
  85. listArray[i].AddRange(this[Keys[i]]);
  86. }
  87. double num2 = FromTime.ToOADate();
  88. double num3 = ToTime.ToOADate();
  89. int index = 0;
  90. while (index < listArray[0].Count)
  91. {
  92. double num5 = (double) listArray[0][index];
  93. if ((num5 > num2) && (num5 < num3))
  94. {
  95. for (int k = 0; k < Keys.Length; k++)
  96. {
  97. listArray[k].RemoveAt(index);
  98. }
  99. }
  100. else
  101. {
  102. index++;
  103. }
  104. }
  105. this.htData.Clear();
  106. for (int j = 0; j < Keys.Length; j++)
  107. {
  108. this.htData.Add(Keys[j], (double[]) listArray[j].ToArray(typeof(double)));
  109. }
  110. this.htAllCycle.Clear();
  111. }
  112. private Hashtable DoExpandMinute(Hashtable ht)
  113. {
  114. double[] numArray = (double[]) ht["DATE"];
  115. if ((numArray == null) || (numArray.Length <= 0))
  116. {
  117. return ht;
  118. }
  119. double num = 0.00069444444444444436;
  120. double num2 = (int) numArray[0];
  121. double num3 = (int) (numArray[numArray.Length - 1] + 1.0);
  122. ArrayList list = new ArrayList();
  123. ArrayList list2 = new ArrayList();
  124. double d = (int) num2;
  125. for (int i = 1; i < numArray.Length; i++)
  126. {
  127. int end = (int) numArray[i];
  128. int start = (int) numArray[i - 1];
  129. if ((end - start) > 1)
  130. {
  131. this.intradayInfo.AddRemoveDays(start, end);
  132. }
  133. }
  134. int index = 0;
  135. while (d <= num3)
  136. {
  137. if (this.intradayInfo.InTimePeriod(d))
  138. {
  139. if (index >= numArray.Length)
  140. {
  141. list.Add(d);
  142. list2.Add(-1);
  143. }
  144. else
  145. {
  146. if (numArray[index] < (d - (num * 0.0001)))
  147. {
  148. index++;
  149. continue;
  150. }
  151. if (numArray[index] < (d + (num * 0.9999)))
  152. {
  153. list.Add(numArray[index]);
  154. list2.Add(index);
  155. index++;
  156. continue;
  157. }
  158. if ((list.Count == 0) || (((double) list[list.Count - 1]) < (d + (num / 100.0))))
  159. {
  160. list.Add(d + (num / 100.0));
  161. if (this.intradayInfo.InTimePeriod(numArray[index]))
  162. {
  163. if (list2.Count > 0)
  164. {
  165. list2.Add(list2[list2.Count - 1]);
  166. }
  167. else
  168. {
  169. list2.Add(0);
  170. }
  171. }
  172. else
  173. {
  174. list2.Add(index);
  175. }
  176. }
  177. }
  178. }
  179. for (d += num; this.intradayInfo.InRemoveDays(d); d++)
  180. {
  181. }
  182. }
  183. Hashtable hashtable = new Hashtable();
  184. double[] numArray2 = (double[]) ht["CLOSE"];
  185. foreach (string str in ht.Keys)
  186. {
  187. double[] numArray3 = (double[]) ht[str];
  188. double[] numArray4 = new double[list.Count];
  189. for (int j = 0; j < list.Count; j++)
  190. {
  191. if (str == "DATE")
  192. {
  193. numArray4[j] = (double) list[j];
  194. }
  195. else
  196. {
  197. int num10 = (int) list2[j];
  198. if (num10 < 0)
  199. {
  200. numArray4[j] = double.NaN;
  201. }
  202. else if (((j > 0) && (((int) list2[j]) == ((int) list2[j - 1]))) || ((j == 0) && (((int) list2[j]) == 0)))
  203. {
  204. if (str == "VOLUME")
  205. {
  206. numArray4[j] = 0.0;
  207. }
  208. else
  209. {
  210. numArray4[j] = numArray2[num10];
  211. }
  212. }
  213. else
  214. {
  215. numArray4[j] = numArray3[num10];
  216. }
  217. }
  218. }
  219. hashtable[str] = numArray4;
  220. }
  221. return hashtable;
  222. }
  223. private Hashtable ExpandFutureBars(Hashtable ht)
  224. {
  225. double[] numArray = (double[]) ht["DATE"];
  226. if ((numArray != null) && (numArray.Length > 0))
  227. {
  228. Hashtable hashtable = new Hashtable();
  229. double[] numArray2 = (double[]) ht["CLOSE"];
  230. foreach (string str in ht.Keys)
  231. {
  232. double[] numArray3 = (double[]) ht[str];
  233. double[] numArray4 = new double[numArray3.Length + this.futureBars];
  234. for (int i = 0; i < numArray3.Length; i++)
  235. {
  236. numArray4[i] = numArray3[i];
  237. }
  238. double num2 = DateTime.Today.ToOADate();
  239. if ((str == "DATE") && (numArray3.Length > 0))
  240. {
  241. num2 = numArray3[numArray3.Length - 1];
  242. }
  243. for (int j = numArray3.Length; j < numArray4.Length; j++)
  244. {
  245. if (str == "DATE")
  246. {
  247. numArray4[j] = (num2 + (j - numArray3.Length)) + 1.0;
  248. }
  249. else
  250. {
  251. numArray4[j] = double.NaN;
  252. }
  253. }
  254. hashtable[str] = numArray4;
  255. }
  256. return hashtable;
  257. }
  258. return ht;
  259. }
  260. private double First(double d1, double d2)
  261. {
  262. if (double.IsNaN(d1))
  263. {
  264. return d2;
  265. }
  266. return d1;
  267. }
  268. public double GetConstData(string DataType)
  269. {
  270. return (double) this.htConstData[DataType];
  271. }
  272. public Hashtable GetCycleData(Easychart.Finance.DataCycle dc)
  273. {
  274. if (((dc.CycleBase == DataCycleBase.DAY) && (dc.Repeat == 1)) && !this.Adjusted)
  275. {
  276. return this.htData;
  277. }
  278. dc.WeekAdjust = this.weekAdjust;
  279. Hashtable hashtable = (Hashtable) this.htAllCycle[dc.ToString()];
  280. if (hashtable == null)
  281. {
  282. if (this.htData == null)
  283. {
  284. return this.htData;
  285. }
  286. Hashtable htData = this.htData;
  287. if (this.intradayInfo != null)
  288. {
  289. htData = this.DoExpandMinute(htData);
  290. }
  291. if (this.futureBars != 0)
  292. {
  293. htData = this.ExpandFutureBars(htData);
  294. }
  295. if (htData["CLOSE"] != null)
  296. {
  297. if (htData["OPEN"] == null)
  298. {
  299. htData["OPEN"] = htData["CLOSE"];
  300. }
  301. if (htData["HIGH"] == null)
  302. {
  303. htData["HIGH"] = htData["CLOSE"];
  304. }
  305. if (htData["LOW"] == null)
  306. {
  307. htData["LOW"] = htData["CLOSE"];
  308. }
  309. }
  310. double[] oDATE = (double[]) htData["DATE"];
  311. if (oDATE == null)
  312. {
  313. return null;
  314. }
  315. int[] nEWDATE = new int[oDATE.Length];
  316. int num = -2147483648;
  317. int num2 = -1;
  318. for (int i = 0; i < oDATE.Length; i++)
  319. {
  320. int sequence;
  321. if (this.DataCycle.CycleBase == DataCycleBase.TICK)
  322. {
  323. sequence = i;
  324. }
  325. else
  326. {
  327. sequence = this.DataCycle.GetSequence(oDATE[i]);
  328. }
  329. if (sequence > num)
  330. {
  331. num2++;
  332. }
  333. nEWDATE[i] = num2;
  334. num = sequence;
  335. }
  336. hashtable = new Hashtable();
  337. foreach (string str in htData.Keys)
  338. {
  339. hashtable[str] = new double[num2 + 1];
  340. }
  341. bool flag = (this.Adjusted && (htData["ADJCLOSE"] != null)) && (htData["CLOSE"] != null);
  342. double[] cLOSE = (double[]) htData["CLOSE"];
  343. double[] aDJCLOSE = (double[]) htData["ADJCLOSE"];
  344. foreach (string str2 in htData.Keys)
  345. {
  346. MergeCycleType dateMergeType;
  347. bool doAdjust = flag;
  348. doAdjust = false;
  349. switch (str2)
  350. {
  351. case "DATE":
  352. dateMergeType = this.dateMergeType;
  353. break;
  354. case "VOLUME":
  355. case "AMOUNT":
  356. case "TURNOVERS":
  357. dateMergeType = MergeCycleType.SUM;
  358. break;
  359. default:
  360. try
  361. {
  362. dateMergeType = (MergeCycleType) Enum.Parse(typeof(MergeCycleType), str2);
  363. doAdjust = true;
  364. }
  365. catch
  366. {
  367. dateMergeType = MergeCycleType.CLOSE;
  368. }
  369. break;
  370. }
  371. this.MergeCycle(oDATE, nEWDATE, cLOSE, aDJCLOSE, (double[]) htData[str2], (double[]) hashtable[str2], dateMergeType, doAdjust);
  372. }
  373. this.htAllCycle[dc.ToString()] = hashtable;
  374. }
  375. return hashtable;
  376. }
  377. private double[] GetData(string DataType)
  378. {
  379. Hashtable cycleData = this.GetCycleData(this.DataCycle);
  380. if (cycleData == null)
  381. {
  382. throw new Exception(string.Concat(new object[] { "Quote data ", DataType, " ", this.DataCycle, " not found" }));
  383. }
  384. double[] dd = (double[]) cycleData[DataType.ToUpper()];
  385. if (dd == null)
  386. {
  387. throw new Exception("The name " + DataType + " does not exist.");
  388. }
  389. if ((this.BaseDataProvider != null) && (this.BaseDataProvider != this))
  390. {
  391. dd = this.AdjustByBase((double[]) cycleData["DATE"], dd);
  392. }
  393. if ((this.MaxCount == -1) || (dd.Length <= this.MaxCount))
  394. {
  395. return dd;
  396. }
  397. double[] destinationArray = new double[this.MaxCount];
  398. Array.Copy(dd, dd.Length - this.MaxCount, destinationArray, 0, this.MaxCount);
  399. return destinationArray;
  400. }
  401. public DataPacket GetDataPackage(int Index)
  402. {
  403. return new DataPacket(this.GetStringData("Code"), this["DATE"][Index], (double) ((float) this["OPEN"][Index]), (double) ((float) this["HIGH"][Index]), (double) ((float) this["LOW"][Index]), (double) ((float) this["CLOSE"][Index]), this["VOLUME"][Index], (double) ((float) this["ADJCLOSE"][Index]));
  404. }
  405. public DataPacket[] GetLastDataPackages(int Count)
  406. {
  407. return this.GetLastDataPackages(this.Count - Count, Count);
  408. }
  409. public DataPacket[] GetLastDataPackages(int Start, int Count)
  410. {
  411. DataPacket[] packetArray = new DataPacket[Count];
  412. double[] numArray = this["DATE"];
  413. double[] numArray2 = this["OPEN"];
  414. double[] numArray3 = this["HIGH"];
  415. double[] numArray4 = this["LOW"];
  416. double[] numArray5 = this["CLOSE"];
  417. double[] numArray6 = this["VOLUME"];
  418. double[] numArray7 = this["ADJCLOSE"];
  419. for (int i = Start; i < (Start + Count); i++)
  420. {
  421. if ((i >= 0) && (i < this.Count))
  422. {
  423. packetArray[i - Start] = new DataPacket(this.GetStringData("Code"), numArray[i], (double) ((float) numArray2[i]), (double) ((float) numArray3[i]), (double) ((float) numArray4[i]), (double) ((float) numArray5[i]), numArray6[i], (double) ((float) numArray7[i]));
  424. }
  425. }
  426. return packetArray;
  427. }
  428. public DataPacket GetLastPackage()
  429. {
  430. return this.GetDataPackage(this.Count - 1);
  431. }
  432. public string GetStringData(string DataType)
  433. {
  434. return (string) this.htStringData[DataType.ToUpper()];
  435. }
  436. public string GetUnique()
  437. {
  438. return this.DataCycle.ToString();
  439. }
  440. public void LoadBinary(Stream stream)
  441. {
  442. byte[] buffer = new byte[stream.Length];
  443. stream.Read(buffer, 0, (int) stream.Length);
  444. this.LoadByteBinary(buffer);
  445. }
  446. public void LoadBinary(string FileName)
  447. {
  448. using (FileStream stream = new FileStream(FileName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
  449. {
  450. this.LoadBinary(stream);
  451. }
  452. }
  453. public void LoadBinary(double[][] ds)
  454. {
  455. if (ds.Length > 4)
  456. {
  457. this.htData.Clear();
  458. this.htData.Add("OPEN", ds[0]);
  459. this.htData.Add("HIGH", ds[1]);
  460. this.htData.Add("LOW", ds[2]);
  461. this.htData.Add("CLOSE", ds[3]);
  462. this.htData.Add("VOLUME", ds[4]);
  463. this.htData.Add("DATE", ds[5]);
  464. if (ds.Length > 6)
  465. {
  466. this.htData.Add("ADJCLOSE", ds[6]);
  467. }
  468. else
  469. {
  470. double[] dst = new double[ds[0].Length];
  471. Buffer.BlockCopy(ds[3], 0, dst, 0, ds[0].Length * 8);
  472. this.htData.Add("ADJCLOSE", dst);
  473. }
  474. if (ds.Length > 7)
  475. {
  476. this.htData.Add("TURNOVERS", ds[7]);
  477. }
  478. else
  479. {
  480. double[] dst = new double[ds[0].Length];
  481. Buffer.BlockCopy(ds[3], 0, dst, 0, ds[0].Length * 8);
  482. this.htData.Add("TURNOVERS", dst);
  483. }
  484. }
  485. this.htAllCycle.Clear();
  486. }
  487. public void LoadBinary(byte[] bs, int N)
  488. {
  489. this.htData.Clear();
  490. double[] numArray = new double[N];
  491. double[] numArray2 = new double[N];
  492. double[] numArray3 = new double[N];
  493. double[] numArray4 = new double[N];
  494. double[] dst = new double[N];
  495. double[] numArray6 = new double[N];
  496. double[] numArray7 = new double[N];
  497. float[] numArray8 = new float[N * DataPacket.PacketSize];
  498. Buffer.BlockCopy(bs, 0, numArray8, 0, bs.Length);
  499. for (int i = 0; i < N; i++)
  500. {
  501. Buffer.BlockCopy(numArray8, i * DataPacket.PacketByteSize, numArray6, i * 8, 8);
  502. numArray[i] = numArray8[(i * DataPacket.PacketSize) + 5];
  503. numArray2[i] = numArray8[(i * DataPacket.PacketSize) + 2];
  504. if (numArray2[i] == 0.0)
  505. {
  506. numArray2[i] = numArray[i];
  507. }
  508. numArray3[i] = numArray8[(i * DataPacket.PacketSize) + 3];
  509. if (numArray3[i] == 0.0)
  510. {
  511. numArray3[i] = numArray[i];
  512. }
  513. numArray4[i] = numArray8[(i * DataPacket.PacketSize) + 4];
  514. if (numArray4[i] == 0.0)
  515. {
  516. numArray4[i] = numArray[i];
  517. }
  518. Buffer.BlockCopy(numArray8, ((i * DataPacket.PacketSize) + 6) * 4, dst, i * 8, 8);
  519. numArray7[i] = numArray8[(i * DataPacket.PacketSize) + 8];
  520. }
  521. this.htData.Add("CLOSE", numArray);
  522. this.htData.Add("OPEN", numArray2);
  523. this.htData.Add("HIGH", numArray3);
  524. this.htData.Add("LOW", numArray4);
  525. this.htData.Add("VOLUME", dst);
  526. this.htData.Add("DATE", numArray6);
  527. this.htData.Add("ADJCLOSE", numArray7);
  528. this.htAllCycle.Clear();
  529. }
  530. public void LoadBinary(string DataType, double[] ds)
  531. {
  532. this.htData[DataType.ToUpper()] = ds;
  533. this.htAllCycle.Clear();
  534. }
  535. public void LoadByteBinary(byte[] bs)
  536. {
  537. this.LoadBinary(bs, bs.Length / DataPacket.PacketByteSize);
  538. }
  539. public void LoadStreamingBinary(Stream stream)
  540. {
  541. byte[] buffer = new byte[stream.Length];
  542. stream.Read(buffer, 0, (int) stream.Length);
  543. this.LoadStreamingBinary(buffer);
  544. }
  545. public void LoadStreamingBinary(string FileName)
  546. {
  547. using (FileStream stream = File.OpenRead(FileName))
  548. {
  549. this.LoadStreamingBinary(stream);
  550. }
  551. }
  552. public void LoadStreamingBinary(byte[] bs)
  553. {
  554. int num = bs.Length / 0x18;
  555. this.htData.Clear();
  556. double[] numArray = new double[num];
  557. double[] numArray2 = new double[num];
  558. double[] numArray3 = new double[num];
  559. double[] dst = new double[num * 3];
  560. Buffer.BlockCopy(bs, 0, dst, 0, bs.Length);
  561. for (int i = 0; i < num; i++)
  562. {
  563. numArray3[i] = dst[i * 3];
  564. numArray[i] = dst[(i * 3) + 1];
  565. numArray2[i] = dst[(i * 3) + 2];
  566. }
  567. this.htData.Add("CLOSE", numArray);
  568. this.htData.Add("VOLUME", numArray2);
  569. this.htData.Add("DATE", numArray3);
  570. this.htAllCycle.Clear();
  571. }
  572. private double Max(double d1, double d2)
  573. {
  574. if (double.IsNaN(d1))
  575. {
  576. return d2;
  577. }
  578. return Math.Max(d1, d2);
  579. }
  580. public void Merge(CommonDataProvider cdp)
  581. {
  582. ArrayList[] listArray = new ArrayList[Keys.Length];
  583. ArrayList[] listArray2 = new ArrayList[Keys.Length];
  584. for (int i = 0; i < listArray.Length; i++)
  585. {
  586. listArray[i] = new ArrayList();
  587. listArray[i].AddRange((double[]) this.htData[Keys[i]]);
  588. listArray2[i] = new ArrayList();
  589. listArray2[i].AddRange((double[]) cdp.htData[Keys[i]]);
  590. }
  591. int index = 0;
  592. int num3 = 0;
  593. while (num3 < listArray2[0].Count)
  594. {
  595. if (index < listArray[0].Count)
  596. {
  597. if (((double) listArray[0][index]) < ((double) listArray2[0][num3]))
  598. {
  599. index++;
  600. }
  601. else if (((double) listArray[0][index]) >= ((double) listArray2[0][num3]))
  602. {
  603. if (((double) listArray[0][index]) > ((double) listArray2[0][num3]))
  604. {
  605. for (int k = 0; k < Keys.Length; k++)
  606. {
  607. listArray[k].Insert(index, listArray2[k][num3]);
  608. }
  609. }
  610. else
  611. {
  612. for (int m = 1; m < Keys.Length; m++)
  613. {
  614. listArray[m][index] = listArray2[m][num3];
  615. }
  616. }
  617. index++;
  618. num3++;
  619. }
  620. }
  621. else
  622. {
  623. for (int n = num3; n < listArray2[0].Count; n++)
  624. {
  625. for (int num7 = 0; num7 < Keys.Length; num7++)
  626. {
  627. listArray[num7].Add(listArray2[num7][n]);
  628. }
  629. }
  630. break;
  631. }
  632. }
  633. this.htData.Clear();
  634. for (int j = 0; j < Keys.Length; j++)
  635. {
  636. this.htData.Add(Keys[j], (double[]) listArray[j].ToArray(typeof(double)));
  637. }
  638. this.htAllCycle.Clear();
  639. }
  640. public void Merge(DataPacket dp)
  641. {
  642. if ((dp != null) && !dp.IsZeroValue)
  643. {
  644. ArrayList[] listArray = new ArrayList[Keys.Length];
  645. for (int i = 0; i < listArray.Length; i++)
  646. {
  647. listArray[i] = new ArrayList();
  648. listArray[i].AddRange((double[]) this.htData[Keys[i]]);
  649. }
  650. for (int j = 0; j <= listArray[0].Count; j++)
  651. {
  652. if (j < listArray[0].Count)
  653. {
  654. if (((double) listArray[0][j]) < dp.DoubleDate)
  655. {
  656. continue;
  657. }
  658. if (((double) listArray[0][j]) > dp.DoubleDate)
  659. {
  660. for (int m = 0; m < Keys.Length; m++)
  661. {
  662. listArray[m].Insert(j, dp[Keys[m]]);
  663. }
  664. }
  665. else
  666. {
  667. for (int n = 1; n < Keys.Length; n++)
  668. {
  669. listArray[n][j] = dp[Keys[n]];
  670. }
  671. }
  672. }
  673. else
  674. {
  675. for (int num5 = 0; num5 < Keys.Length; num5++)
  676. {
  677. listArray[num5].Add(dp[Keys[num5]]);
  678. }
  679. }
  680. break;
  681. }
  682. this.htData.Clear();
  683. for (int k = 0; k < Keys.Length; k++)
  684. {
  685. this.htData.Add(Keys[k], (double[]) listArray[k].ToArray(typeof(double)));
  686. }
  687. this.htAllCycle.Clear();
  688. }
  689. }
  690. private void MergeCycle(double[] ODATE, int[] NEWDATE, double[] CLOSE, double[] ADJCLOSE, double[] ht, double[] htCycle, MergeCycleType mct, bool DoAdjust)
  691. {
  692. int num = -1;
  693. int index = -1;
  694. for (int i = 0; i < ODATE.Length; i++)
  695. {
  696. double num4 = 1.0;
  697. if (DoAdjust && (ADJCLOSE != null))
  698. {
  699. num4 = ADJCLOSE[i] / CLOSE[i];
  700. }
  701. double num5 = ht[i] * num4;
  702. if (num4 != 1.0)
  703. {
  704. num5 = Math.Round(num5, 2);
  705. }
  706. if (num != NEWDATE[i])
  707. {
  708. index++;
  709. htCycle[index] = num5;
  710. }
  711. else if (!double.IsNaN(num5))
  712. {
  713. if (mct == MergeCycleType.HIGH)
  714. {
  715. htCycle[index] = this.Max(htCycle[index], num5);
  716. }
  717. else if (mct == MergeCycleType.LOW)
  718. {
  719. htCycle[index] = this.Min(htCycle[index], num5);
  720. }
  721. else if (mct == MergeCycleType.CLOSE)
  722. {
  723. htCycle[index] = num5;
  724. }
  725. else if (mct == MergeCycleType.ADJCLOSE)
  726. {
  727. htCycle[index] = ht[i];
  728. }
  729. else if (mct == MergeCycleType.OPEN)
  730. {
  731. htCycle[index] = this.First(htCycle[index], num5);
  732. }
  733. else
  734. {
  735. htCycle[index] = this.Sum(htCycle[index], num5);
  736. }
  737. }
  738. num = NEWDATE[i];
  739. }
  740. }
  741. public static DataPacket MergeFile(string Filename, DataPacket dp, Easychart.Finance.DataCycle dc)
  742. {
  743. using (FileStream stream = new FileStream(Filename, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
  744. {
  745. byte[] buffer = new byte[DataPacket.PacketByteSize];
  746. if (stream.Length >= buffer.Length)
  747. {
  748. stream.Seek((long) -buffer.Length, SeekOrigin.End);
  749. }
  750. int num = stream.Read(buffer, 0, buffer.Length);
  751. DataPacket packet = DataPacket.FromBytes(buffer);
  752. packet.Symbol = dp.Symbol;
  753. if (packet.Merge(dp, dc))
  754. {
  755. stream.Seek((long) -buffer.Length, SeekOrigin.End);
  756. }
  757. buffer = packet.ToByte();
  758. stream.Write(buffer, 0, buffer.Length);
  759. return packet;
  760. }
  761. }
  762. public static byte[] MergeOneQuote(byte[] bs, DataPacket dp)
  763. {
  764. float[] dst = new float[(bs.Length / 4) + DataPacket.PacketSize];
  765. Buffer.BlockCopy(bs, 0, dst, 0, bs.Length);
  766. DateTime date = dp.Date.Date;
  767. int num = (dst.Length / DataPacket.PacketSize) - 1;
  768. for (int i = num - 1; i >= -1; i--)
  769. {
  770. DateTime minValue = DateTime.MinValue;
  771. if (i > -1)
  772. {
  773. minValue = DataPacket.GetDateTime(dst, i).Date;
  774. }
  775. int num3 = 0;
  776. if (minValue <= date)
  777. {
  778. if (minValue < date)
  779. {
  780. if ((i < (num - 1)) && (num > 0))
  781. {
  782. Buffer.BlockCopy(dst, (i + 1) * DataPacket.PacketByteSize, dst, (i + 2) * DataPacket.PacketByteSize, ((num - i) - 1) * DataPacket.PacketByteSize);
  783. }
  784. num3 = 1;
  785. }
  786. Buffer.BlockCopy(dp.GetFloat(), 0, dst, (i + num3) * DataPacket.PacketByteSize, DataPacket.PacketByteSize);
  787. bs = new byte[(dst.Length * 4) - ((1 - num3) * DataPacket.PacketByteSize)];
  788. Buffer.BlockCopy(dst, 0, bs, 0, bs.Length);
  789. return bs;
  790. }
  791. }
  792. return bs;
  793. }
  794. private double Min(double d1, double d2)
  795. {
  796. if (double.IsNaN(d1))
  797. {
  798. return d2;
  799. }
  800. return Math.Min(d1, d2);
  801. }
  802. public byte[] SaveBinary()
  803. {
  804. MemoryStream stream = new MemoryStream();
  805. this.SaveBinary(stream);
  806. return stream.ToArray();
  807. }
  808. public void SaveBinary(Stream stream)
  809. {
  810. using (BinaryWriter writer = new BinaryWriter(stream))
  811. {
  812. double[] numArray = (double[]) this.htData["CLOSE"];
  813. double[] numArray2 = (double[]) this.htData["OPEN"];
  814. double[] numArray3 = (double[]) this.htData["HIGH"];
  815. double[] numArray4 = (double[]) this.htData["LOW"];
  816. double[] numArray5 = (double[]) this.htData["VOLUME"];
  817. double[] numArray6 = (double[]) this.htData["DATE"];
  818. double[] numArray7 = (double[]) this.htData["ADJCLOSE"];
  819. int length = numArray.Length;
  820. for (int i = 0; i < length; i++)
  821. {
  822. writer.Write(numArray6[i]);
  823. writer.Write((float) numArray2[i]);
  824. writer.Write((float) numArray3[i]);
  825. writer.Write((float) numArray4[i]);
  826. writer.Write((float) numArray[i]);
  827. writer.Write(numArray5[i]);
  828. writer.Write((float) numArray7[i]);
  829. }
  830. }
  831. }
  832. public void SaveBinary(string FileName)
  833. {
  834. using (FileStream stream = new FileStream(FileName, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
  835. {
  836. this.SaveBinary(stream);
  837. }
  838. }
  839. public byte[] SaveStreamingBinary()
  840. {
  841. MemoryStream stream = new MemoryStream();
  842. this.SaveStreamingBinary(stream);
  843. return stream.ToArray();
  844. }
  845. public void SaveStreamingBinary(Stream stream)
  846. {
  847. using (BinaryWriter writer = new BinaryWriter(stream))
  848. {
  849. double[] numArray = (double[]) this.htData["CLOSE"];
  850. double[] numArray2 = (double[]) this.htData["VOLUME"];
  851. double[] numArray3 = (double[]) this.htData["DATE"];
  852. int length = numArray.Length;
  853. for (int i = 0; i < length; i++)
  854. {
  855. writer.Write(numArray3[i]);
  856. writer.Write(numArray[i]);
  857. writer.Write(numArray2[i]);
  858. }
  859. }
  860. }
  861. public void SaveStreamingBinary(string FileName)
  862. {
  863. this.SaveStreamingBinary(File.OpenWrite(FileName));
  864. }
  865. public void SetStringData(string DataType, string Value)
  866. {
  867. this.htStringData[DataType.ToUpper()] = Value;
  868. }
  869. private double Sum(double d1, double d2)
  870. {
  871. if (double.IsNaN(d1))
  872. {
  873. return d2;
  874. }
  875. return (d1 + d2);
  876. }
  877. public void TrimTime()
  878. {
  879. double[] numArray = (double[]) this.htData["DATE"];
  880. for (int i = 0; i < numArray.Length; i++)
  881. {
  882. numArray[i] = (int) numArray[i];
  883. }
  884. }
  885. public bool Adjusted
  886. {
  887. get
  888. {
  889. return this.adjusted;
  890. }
  891. set
  892. {
  893. this.adjusted = value;
  894. }
  895. }
  896. public ICollection AllDataType
  897. {
  898. get
  899. {
  900. return this.htData.Keys;
  901. }
  902. }
  903. public IDataProvider BaseDataProvider
  904. {
  905. get
  906. {
  907. return this.baseDataProvider;
  908. }
  909. set
  910. {
  911. this.baseDataProvider = value;
  912. }
  913. }
  914. public int Count
  915. {
  916. get
  917. {
  918. return this["DATE"].Length;
  919. }
  920. }
  921. public Easychart.Finance.DataCycle DataCycle
  922. {
  923. get
  924. {
  925. return this.dataCycle;
  926. }
  927. set
  928. {
  929. this.dataCycle = value;
  930. }
  931. }
  932. public IDataManager DataManager
  933. {
  934. get
  935. {
  936. if (this.dm == null)
  937. {
  938. return this.dm;
  939. }
  940. return this.dm;
  941. }
  942. set
  943. {
  944. this.dm = value;
  945. }
  946. }
  947. public MergeCycleType DateMergeType
  948. {
  949. get
  950. {
  951. return this.dateMergeType;
  952. }
  953. set
  954. {
  955. this.dateMergeType = value;
  956. }
  957. }
  958. public static CommonDataProvider Empty
  959. {
  960. get
  961. {
  962. CommonDataProvider provider = new CommonDataProvider(null);
  963. provider.LoadByteBinary(new byte[0]);
  964. return provider;
  965. }
  966. }
  967. public int FutureBars
  968. {
  969. get
  970. {
  971. return this.futureBars;
  972. }
  973. set
  974. {
  975. this.futureBars = value;
  976. }
  977. }
  978. public bool HasData
  979. {
  980. get
  981. {
  982. return (((double[]) this.htData["DATE"]).Length > 0);
  983. }
  984. }
  985. public ExchangeIntraday IntradayInfo
  986. {
  987. get
  988. {
  989. return this.intradayInfo;
  990. }
  991. set
  992. {
  993. this.intradayInfo = value;
  994. this.htAllCycle.Clear();
  995. }
  996. }
  997. public bool IsPointAndFigure
  998. {
  999. get
  1000. {
  1001. return this.isPointAndFigure;
  1002. }
  1003. set
  1004. {
  1005. this.isPointAndFigure = value;
  1006. }
  1007. }
  1008. public double[] this[string Name]
  1009. {
  1010. get
  1011. {
  1012. try
  1013. {
  1014. return this.GetData(Name);
  1015. }
  1016. catch (Exception ex)
  1017. {
  1018. return new double[]{};
  1019. }
  1020. }
  1021. }
  1022. public int MaxCount
  1023. {
  1024. get
  1025. {
  1026. return this.maxCount;
  1027. }
  1028. set
  1029. {
  1030. this.maxCount = value;
  1031. }
  1032. }
  1033. protected int WeekAdjust
  1034. {
  1035. get
  1036. {
  1037. return this.weekAdjust;
  1038. }
  1039. set
  1040. {
  1041. this.weekAdjust = value;
  1042. }
  1043. }
  1044. public void Dispose()
  1045. {
  1046. htAllCycle.Clear();
  1047. htAllCycle.Clear();
  1048. htConstData.Clear();
  1049. htData.Clear();
  1050. htRealtime.Clear();
  1051. htStringData.Clear();
  1052. }
  1053. }
  1054. }