CommonDataProvider.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  1. namespace IndexFormula.Finance.DataProvider
  2. {
  3. using IndexFormula.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 IndexFormula.Finance.DataCycle dataCycle = IndexFormula.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" };
  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(IndexFormula.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. dateMergeType = MergeCycleType.SUM;
  357. break;
  358. default:
  359. try
  360. {
  361. dateMergeType = (MergeCycleType) Enum.Parse(typeof(MergeCycleType), str2);
  362. doAdjust = true;
  363. }
  364. catch
  365. {
  366. dateMergeType = MergeCycleType.CLOSE;
  367. }
  368. break;
  369. }
  370. this.MergeCycle(oDATE, nEWDATE, cLOSE, aDJCLOSE, (double[]) htData[str2], (double[]) hashtable[str2], dateMergeType, doAdjust);
  371. }
  372. this.htAllCycle[dc.ToString()] = hashtable;
  373. }
  374. return hashtable;
  375. }
  376. private double[] GetData(string DataType)
  377. {
  378. Hashtable cycleData = this.GetCycleData(this.DataCycle);
  379. if (cycleData == null)
  380. {
  381. throw new Exception(string.Concat(new object[] { "Quote data ", DataType, " ", this.DataCycle, " not found" }));
  382. }
  383. double[] dd = (double[]) cycleData[DataType.ToUpper()];
  384. if (dd == null)
  385. {
  386. throw new Exception("The name " + DataType + " does not exist.");
  387. }
  388. if ((this.BaseDataProvider != null) && (this.BaseDataProvider != this))
  389. {
  390. dd = this.AdjustByBase((double[]) cycleData["DATE"], dd);
  391. }
  392. if ((this.MaxCount == -1) || (dd.Length <= this.MaxCount))
  393. {
  394. return dd;
  395. }
  396. double[] destinationArray = new double[this.MaxCount];
  397. Array.Copy(dd, dd.Length - this.MaxCount, destinationArray, 0, this.MaxCount);
  398. return destinationArray;
  399. }
  400. public DataPacket GetDataPackage(int Index)
  401. {
  402. 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]));
  403. }
  404. public DataPacket[] GetLastDataPackages(int Count)
  405. {
  406. return this.GetLastDataPackages(this.Count - Count, Count);
  407. }
  408. public DataPacket[] GetLastDataPackages(int Start, int Count)
  409. {
  410. DataPacket[] packetArray = new DataPacket[Count];
  411. double[] numArray = this["DATE"];
  412. double[] numArray2 = this["OPEN"];
  413. double[] numArray3 = this["HIGH"];
  414. double[] numArray4 = this["LOW"];
  415. double[] numArray5 = this["CLOSE"];
  416. double[] numArray6 = this["VOLUME"];
  417. double[] numArray7 = this["ADJCLOSE"];
  418. for (int i = Start; i < (Start + Count); i++)
  419. {
  420. if ((i >= 0) && (i < this.Count))
  421. {
  422. 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]));
  423. }
  424. }
  425. return packetArray;
  426. }
  427. public DataPacket GetLastPackage()
  428. {
  429. return this.GetDataPackage(this.Count - 1);
  430. }
  431. public string GetStringData(string DataType)
  432. {
  433. return (string) this.htStringData[DataType.ToUpper()];
  434. }
  435. public string GetUnique()
  436. {
  437. return this.DataCycle.ToString();
  438. }
  439. public void LoadBinary(Stream stream)
  440. {
  441. byte[] buffer = new byte[stream.Length];
  442. stream.Read(buffer, 0, (int) stream.Length);
  443. this.LoadByteBinary(buffer);
  444. }
  445. public void LoadBinary(string FileName)
  446. {
  447. using (FileStream stream = new FileStream(FileName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
  448. {
  449. this.LoadBinary(stream);
  450. }
  451. }
  452. /// <summary>
  453. /// 载入数据源
  454. /// </summary>
  455. /// <param name="ds">二维double数据源,ds[0]=OPEN,ds[1]=HIGH,ds[2]=LOW,ds[3]=CLOSE,ds[4]=VOLUME,ds[5]=DATE</param>
  456. public void LoadBinary(double[][] ds)
  457. {
  458. if (ds.Length > 4)
  459. {
  460. this.htData.Clear();
  461. this.htData.Add("OPEN", ds[0]);
  462. this.htData.Add("HIGH", ds[1]);
  463. this.htData.Add("LOW", ds[2]);
  464. this.htData.Add("CLOSE", ds[3]);
  465. this.htData.Add("VOLUME", ds[4]);
  466. this.htData.Add("DATE", ds[5]);
  467. if (ds.Length > 6)
  468. {
  469. this.htData.Add("ADJCLOSE", ds[6]);
  470. }
  471. else
  472. {
  473. double[] dst = new double[ds[0].Length];
  474. Buffer.BlockCopy(ds[3], 0, dst, 0, ds[0].Length * 8);
  475. this.htData.Add("ADJCLOSE", dst);
  476. }
  477. }
  478. this.htAllCycle.Clear();
  479. }
  480. public void LoadBinary(byte[] bs, int N)
  481. {
  482. this.htData.Clear();
  483. double[] numArray = new double[N];
  484. double[] numArray2 = new double[N];
  485. double[] numArray3 = new double[N];
  486. double[] numArray4 = new double[N];
  487. double[] dst = new double[N];
  488. double[] numArray6 = new double[N];
  489. double[] numArray7 = new double[N];
  490. float[] numArray8 = new float[N * DataPacket.PacketSize];
  491. Buffer.BlockCopy(bs, 0, numArray8, 0, bs.Length);
  492. for (int i = 0; i < N; i++)
  493. {
  494. Buffer.BlockCopy(numArray8, i * DataPacket.PacketByteSize, numArray6, i * 8, 8);
  495. numArray[i] = numArray8[(i * DataPacket.PacketSize) + 5];
  496. numArray2[i] = numArray8[(i * DataPacket.PacketSize) + 2];
  497. if (numArray2[i] == 0.0)
  498. {
  499. numArray2[i] = numArray[i];
  500. }
  501. numArray3[i] = numArray8[(i * DataPacket.PacketSize) + 3];
  502. if (numArray3[i] == 0.0)
  503. {
  504. numArray3[i] = numArray[i];
  505. }
  506. numArray4[i] = numArray8[(i * DataPacket.PacketSize) + 4];
  507. if (numArray4[i] == 0.0)
  508. {
  509. numArray4[i] = numArray[i];
  510. }
  511. Buffer.BlockCopy(numArray8, ((i * DataPacket.PacketSize) + 6) * 4, dst, i * 8, 8);
  512. numArray7[i] = numArray8[(i * DataPacket.PacketSize) + 8];
  513. }
  514. this.htData.Add("CLOSE", numArray);
  515. this.htData.Add("OPEN", numArray2);
  516. this.htData.Add("HIGH", numArray3);
  517. this.htData.Add("LOW", numArray4);
  518. this.htData.Add("VOLUME", dst);
  519. this.htData.Add("DATE", numArray6);
  520. this.htData.Add("ADJCLOSE", numArray7);
  521. this.htAllCycle.Clear();
  522. }
  523. public void LoadBinary(string DataType, double[] ds)
  524. {
  525. this.htData[DataType.ToUpper()] = ds;
  526. this.htAllCycle.Clear();
  527. }
  528. public void LoadByteBinary(byte[] bs)
  529. {
  530. this.LoadBinary(bs, bs.Length / DataPacket.PacketByteSize);
  531. }
  532. public void LoadStreamingBinary(Stream stream)
  533. {
  534. byte[] buffer = new byte[stream.Length];
  535. stream.Read(buffer, 0, (int) stream.Length);
  536. this.LoadStreamingBinary(buffer);
  537. }
  538. public void LoadStreamingBinary(string FileName)
  539. {
  540. using (FileStream stream = File.OpenRead(FileName))
  541. {
  542. this.LoadStreamingBinary(stream);
  543. }
  544. }
  545. public void LoadStreamingBinary(byte[] bs)
  546. {
  547. int num = bs.Length / 0x18;
  548. this.htData.Clear();
  549. double[] numArray = new double[num];
  550. double[] numArray2 = new double[num];
  551. double[] numArray3 = new double[num];
  552. double[] dst = new double[num * 3];
  553. Buffer.BlockCopy(bs, 0, dst, 0, bs.Length);
  554. for (int i = 0; i < num; i++)
  555. {
  556. numArray3[i] = dst[i * 3];
  557. numArray[i] = dst[(i * 3) + 1];
  558. numArray2[i] = dst[(i * 3) + 2];
  559. }
  560. this.htData.Add("CLOSE", numArray);
  561. this.htData.Add("VOLUME", numArray2);
  562. this.htData.Add("DATE", numArray3);
  563. this.htAllCycle.Clear();
  564. }
  565. private double Max(double d1, double d2)
  566. {
  567. if (double.IsNaN(d1))
  568. {
  569. return d2;
  570. }
  571. return Math.Max(d1, d2);
  572. }
  573. public void Merge(CommonDataProvider cdp)
  574. {
  575. ArrayList[] listArray = new ArrayList[Keys.Length];
  576. ArrayList[] listArray2 = new ArrayList[Keys.Length];
  577. for (int i = 0; i < listArray.Length; i++)
  578. {
  579. listArray[i] = new ArrayList();
  580. listArray[i].AddRange((double[]) this.htData[Keys[i]]);
  581. listArray2[i] = new ArrayList();
  582. listArray2[i].AddRange((double[]) cdp.htData[Keys[i]]);
  583. }
  584. int index = 0;
  585. int num3 = 0;
  586. while (num3 < listArray2[0].Count)
  587. {
  588. if (index < listArray[0].Count)
  589. {
  590. if (((double) listArray[0][index]) < ((double) listArray2[0][num3]))
  591. {
  592. index++;
  593. }
  594. else if (((double) listArray[0][index]) >= ((double) listArray2[0][num3]))
  595. {
  596. if (((double) listArray[0][index]) > ((double) listArray2[0][num3]))
  597. {
  598. for (int k = 0; k < Keys.Length; k++)
  599. {
  600. listArray[k].Insert(index, listArray2[k][num3]);
  601. }
  602. }
  603. else
  604. {
  605. for (int m = 1; m < Keys.Length; m++)
  606. {
  607. listArray[m][index] = listArray2[m][num3];
  608. }
  609. }
  610. index++;
  611. num3++;
  612. }
  613. }
  614. else
  615. {
  616. for (int n = num3; n < listArray2[0].Count; n++)
  617. {
  618. for (int num7 = 0; num7 < Keys.Length; num7++)
  619. {
  620. listArray[num7].Add(listArray2[num7][n]);
  621. }
  622. }
  623. break;
  624. }
  625. }
  626. this.htData.Clear();
  627. for (int j = 0; j < Keys.Length; j++)
  628. {
  629. this.htData.Add(Keys[j], (double[]) listArray[j].ToArray(typeof(double)));
  630. }
  631. this.htAllCycle.Clear();
  632. }
  633. public void Merge(DataPacket dp)
  634. {
  635. if ((dp != null) && !dp.IsZeroValue)
  636. {
  637. ArrayList[] listArray = new ArrayList[Keys.Length];
  638. for (int i = 0; i < listArray.Length; i++)
  639. {
  640. listArray[i] = new ArrayList();
  641. listArray[i].AddRange((double[]) this.htData[Keys[i]]);
  642. }
  643. for (int j = 0; j <= listArray[0].Count; j++)
  644. {
  645. if (j < listArray[0].Count)
  646. {
  647. if (((double) listArray[0][j]) < dp.DoubleDate)
  648. {
  649. continue;
  650. }
  651. if (((double) listArray[0][j]) > dp.DoubleDate)
  652. {
  653. for (int m = 0; m < Keys.Length; m++)
  654. {
  655. listArray[m].Insert(j, dp[Keys[m]]);
  656. }
  657. }
  658. else
  659. {
  660. for (int n = 1; n < Keys.Length; n++)
  661. {
  662. listArray[n][j] = dp[Keys[n]];
  663. }
  664. }
  665. }
  666. else
  667. {
  668. for (int num5 = 0; num5 < Keys.Length; num5++)
  669. {
  670. listArray[num5].Add(dp[Keys[num5]]);
  671. }
  672. }
  673. break;
  674. }
  675. this.htData.Clear();
  676. for (int k = 0; k < Keys.Length; k++)
  677. {
  678. this.htData.Add(Keys[k], (double[]) listArray[k].ToArray(typeof(double)));
  679. }
  680. this.htAllCycle.Clear();
  681. }
  682. }
  683. private void MergeCycle(double[] ODATE, int[] NEWDATE, double[] CLOSE, double[] ADJCLOSE, double[] ht, double[] htCycle, MergeCycleType mct, bool DoAdjust)
  684. {
  685. int num = -1;
  686. int index = -1;
  687. for (int i = 0; i < ODATE.Length; i++)
  688. {
  689. double num4 = 1.0;
  690. if (DoAdjust && (ADJCLOSE != null))
  691. {
  692. num4 = ADJCLOSE[i] / CLOSE[i];
  693. }
  694. double num5 = ht[i] * num4;
  695. if (num4 != 1.0)
  696. {
  697. num5 = Math.Round(num5, 2);
  698. }
  699. if (num != NEWDATE[i])
  700. {
  701. index++;
  702. htCycle[index] = num5;
  703. }
  704. else if (!double.IsNaN(num5))
  705. {
  706. if (mct == MergeCycleType.HIGH)
  707. {
  708. htCycle[index] = this.Max(htCycle[index], num5);
  709. }
  710. else if (mct == MergeCycleType.LOW)
  711. {
  712. htCycle[index] = this.Min(htCycle[index], num5);
  713. }
  714. else if (mct == MergeCycleType.CLOSE)
  715. {
  716. htCycle[index] = num5;
  717. }
  718. else if (mct == MergeCycleType.ADJCLOSE)
  719. {
  720. htCycle[index] = ht[i];
  721. }
  722. else if (mct == MergeCycleType.OPEN)
  723. {
  724. htCycle[index] = this.First(htCycle[index], num5);
  725. }
  726. else
  727. {
  728. htCycle[index] = this.Sum(htCycle[index], num5);
  729. }
  730. }
  731. num = NEWDATE[i];
  732. }
  733. }
  734. public static DataPacket MergeFile(string Filename, DataPacket dp, IndexFormula.Finance.DataCycle dc)
  735. {
  736. using (FileStream stream = new FileStream(Filename, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
  737. {
  738. byte[] buffer = new byte[DataPacket.PacketByteSize];
  739. if (stream.Length >= buffer.Length)
  740. {
  741. stream.Seek((long) -buffer.Length, SeekOrigin.End);
  742. }
  743. int num = stream.Read(buffer, 0, buffer.Length);
  744. DataPacket packet = DataPacket.FromBytes(buffer);
  745. packet.Symbol = dp.Symbol;
  746. if (packet.Merge(dp, dc))
  747. {
  748. stream.Seek((long) -buffer.Length, SeekOrigin.End);
  749. }
  750. buffer = packet.ToByte();
  751. stream.Write(buffer, 0, buffer.Length);
  752. return packet;
  753. }
  754. }
  755. public static byte[] MergeOneQuote(byte[] bs, DataPacket dp)
  756. {
  757. float[] dst = new float[(bs.Length / 4) + DataPacket.PacketSize];
  758. Buffer.BlockCopy(bs, 0, dst, 0, bs.Length);
  759. DateTime date = dp.Date.Date;
  760. int num = (dst.Length / DataPacket.PacketSize) - 1;
  761. for (int i = num - 1; i >= -1; i--)
  762. {
  763. DateTime minValue = DateTime.MinValue;
  764. if (i > -1)
  765. {
  766. minValue = DataPacket.GetDateTime(dst, i).Date;
  767. }
  768. int num3 = 0;
  769. if (minValue <= date)
  770. {
  771. if (minValue < date)
  772. {
  773. if ((i < (num - 1)) && (num > 0))
  774. {
  775. Buffer.BlockCopy(dst, (i + 1) * DataPacket.PacketByteSize, dst, (i + 2) * DataPacket.PacketByteSize, ((num - i) - 1) * DataPacket.PacketByteSize);
  776. }
  777. num3 = 1;
  778. }
  779. Buffer.BlockCopy(dp.GetFloat(), 0, dst, (i + num3) * DataPacket.PacketByteSize, DataPacket.PacketByteSize);
  780. bs = new byte[(dst.Length * 4) - ((1 - num3) * DataPacket.PacketByteSize)];
  781. Buffer.BlockCopy(dst, 0, bs, 0, bs.Length);
  782. return bs;
  783. }
  784. }
  785. return bs;
  786. }
  787. private double Min(double d1, double d2)
  788. {
  789. if (double.IsNaN(d1))
  790. {
  791. return d2;
  792. }
  793. return Math.Min(d1, d2);
  794. }
  795. public byte[] SaveBinary()
  796. {
  797. MemoryStream stream = new MemoryStream();
  798. this.SaveBinary(stream);
  799. return stream.ToArray();
  800. }
  801. public void SaveBinary(Stream stream)
  802. {
  803. using (BinaryWriter writer = new BinaryWriter(stream))
  804. {
  805. double[] numArray = (double[]) this.htData["CLOSE"];
  806. double[] numArray2 = (double[]) this.htData["OPEN"];
  807. double[] numArray3 = (double[]) this.htData["HIGH"];
  808. double[] numArray4 = (double[]) this.htData["LOW"];
  809. double[] numArray5 = (double[]) this.htData["VOLUME"];
  810. double[] numArray6 = (double[]) this.htData["DATE"];
  811. double[] numArray7 = (double[]) this.htData["ADJCLOSE"];
  812. int length = numArray.Length;
  813. for (int i = 0; i < length; i++)
  814. {
  815. writer.Write(numArray6[i]);
  816. writer.Write((float) numArray2[i]);
  817. writer.Write((float) numArray3[i]);
  818. writer.Write((float) numArray4[i]);
  819. writer.Write((float) numArray[i]);
  820. writer.Write(numArray5[i]);
  821. writer.Write((float) numArray7[i]);
  822. }
  823. }
  824. }
  825. public void SaveBinary(string FileName)
  826. {
  827. using (FileStream stream = new FileStream(FileName, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
  828. {
  829. this.SaveBinary(stream);
  830. }
  831. }
  832. public byte[] SaveStreamingBinary()
  833. {
  834. MemoryStream stream = new MemoryStream();
  835. this.SaveStreamingBinary(stream);
  836. return stream.ToArray();
  837. }
  838. public void SaveStreamingBinary(Stream stream)
  839. {
  840. using (BinaryWriter writer = new BinaryWriter(stream))
  841. {
  842. double[] numArray = (double[]) this.htData["CLOSE"];
  843. double[] numArray2 = (double[]) this.htData["VOLUME"];
  844. double[] numArray3 = (double[]) this.htData["DATE"];
  845. int length = numArray.Length;
  846. for (int i = 0; i < length; i++)
  847. {
  848. writer.Write(numArray3[i]);
  849. writer.Write(numArray[i]);
  850. writer.Write(numArray2[i]);
  851. }
  852. }
  853. }
  854. public void SaveStreamingBinary(string FileName)
  855. {
  856. this.SaveStreamingBinary(File.OpenWrite(FileName));
  857. }
  858. public void SetStringData(string DataType, string Value)
  859. {
  860. this.htStringData[DataType.ToUpper()] = Value;
  861. }
  862. private double Sum(double d1, double d2)
  863. {
  864. if (double.IsNaN(d1))
  865. {
  866. return d2;
  867. }
  868. return (d1 + d2);
  869. }
  870. public void TrimTime()
  871. {
  872. double[] numArray = (double[]) this.htData["DATE"];
  873. for (int i = 0; i < numArray.Length; i++)
  874. {
  875. numArray[i] = (int) numArray[i];
  876. }
  877. }
  878. public bool Adjusted
  879. {
  880. get
  881. {
  882. return this.adjusted;
  883. }
  884. set
  885. {
  886. this.adjusted = value;
  887. }
  888. }
  889. public ICollection AllDataType
  890. {
  891. get
  892. {
  893. return this.htData.Keys;
  894. }
  895. }
  896. public IDataProvider BaseDataProvider
  897. {
  898. get
  899. {
  900. return this.baseDataProvider;
  901. }
  902. set
  903. {
  904. this.baseDataProvider = value;
  905. }
  906. }
  907. public int Count
  908. {
  909. get
  910. {
  911. return this["DATE"].Length;
  912. }
  913. }
  914. public IndexFormula.Finance.DataCycle DataCycle
  915. {
  916. get
  917. {
  918. return this.dataCycle;
  919. }
  920. set
  921. {
  922. this.dataCycle = value;
  923. }
  924. }
  925. public IDataManager DataManager
  926. {
  927. get
  928. {
  929. if (this.dm == null)
  930. {
  931. return this.dm;
  932. }
  933. return this.dm;
  934. }
  935. set
  936. {
  937. this.dm = value;
  938. }
  939. }
  940. public MergeCycleType DateMergeType
  941. {
  942. get
  943. {
  944. return this.dateMergeType;
  945. }
  946. set
  947. {
  948. this.dateMergeType = value;
  949. }
  950. }
  951. public static CommonDataProvider Empty
  952. {
  953. get
  954. {
  955. CommonDataProvider provider = new CommonDataProvider(null);
  956. provider.LoadByteBinary(new byte[0]);
  957. return provider;
  958. }
  959. }
  960. public int FutureBars
  961. {
  962. get
  963. {
  964. return this.futureBars;
  965. }
  966. set
  967. {
  968. this.futureBars = value;
  969. }
  970. }
  971. public bool HasData
  972. {
  973. get
  974. {
  975. return (((double[]) this.htData["DATE"]).Length > 0);
  976. }
  977. }
  978. public ExchangeIntraday IntradayInfo
  979. {
  980. get
  981. {
  982. return this.intradayInfo;
  983. }
  984. set
  985. {
  986. this.intradayInfo = value;
  987. this.htAllCycle.Clear();
  988. }
  989. }
  990. public bool IsPointAndFigure
  991. {
  992. get
  993. {
  994. return this.isPointAndFigure;
  995. }
  996. set
  997. {
  998. this.isPointAndFigure = value;
  999. }
  1000. }
  1001. public double[] this[string Name]
  1002. {
  1003. get
  1004. {
  1005. return this.GetData(Name);
  1006. }
  1007. }
  1008. public int MaxCount
  1009. {
  1010. get
  1011. {
  1012. return this.maxCount;
  1013. }
  1014. set
  1015. {
  1016. this.maxCount = value;
  1017. }
  1018. }
  1019. protected int WeekAdjust
  1020. {
  1021. get
  1022. {
  1023. return this.weekAdjust;
  1024. }
  1025. set
  1026. {
  1027. this.weekAdjust = value;
  1028. }
  1029. }
  1030. }
  1031. }