MSDataManager.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. namespace Easychart.Finance.DataProvider
  2. {
  3. using System;
  4. using System.Collections;
  5. using System.Data;
  6. using System.IO;
  7. using System.Runtime.InteropServices;
  8. using System.Text;
  9. using System.Threading;
  10. public class MSDataManager : DataManagerBase
  11. {
  12. private int Fields = 7;
  13. private string FilePath;
  14. private Master[] Masters;
  15. private MetaStockTimeFrame TimeFrame = MetaStockTimeFrame.Daily;
  16. private XMaster[] XMasters;
  17. public MSDataManager(string FilePath)
  18. {
  19. FilePath = Path.GetDirectoryName(FilePath);
  20. if (!FilePath.EndsWith(@"\"))
  21. {
  22. FilePath = FilePath + @"\";
  23. }
  24. this.FilePath = FilePath;
  25. if (File.Exists(FilePath + "XMASTER"))
  26. {
  27. this.LoadXMaster();
  28. }
  29. if (!File.Exists(FilePath + "MASTER"))
  30. {
  31. throw new Exception("MetaStock Path Not Found!" + FilePath + "MASTER");
  32. }
  33. this.LoadMaster();
  34. }
  35. public override int DeleteSymbols(string Exchange, string[] Symbols, bool Remain, bool DeleteRealtime, bool DeleteHistorical)
  36. {
  37. ArrayList list = new ArrayList();
  38. int num = 0;
  39. if (this.Masters != null)
  40. {
  41. list.AddRange(this.Masters);
  42. int index = 0;
  43. while (index < list.Count)
  44. {
  45. if (Array.IndexOf(Symbols, (list[index] as Master).symbol) >= 0)
  46. {
  47. list.RemoveAt(index);
  48. num++;
  49. }
  50. else
  51. {
  52. index++;
  53. }
  54. }
  55. if (num > 0)
  56. {
  57. this.Masters = (Master[]) list.ToArray(typeof(Master));
  58. this.SaveMaster();
  59. }
  60. }
  61. if (this.XMasters != null)
  62. {
  63. list.Clear();
  64. num = 0;
  65. list.AddRange(this.XMasters);
  66. int num3 = 0;
  67. while (num3 < list.Count)
  68. {
  69. if (Array.IndexOf(Symbols, (list[num3] as XMaster).StockSymbol) >= 0)
  70. {
  71. list.RemoveAt(num3);
  72. num++;
  73. }
  74. else
  75. {
  76. num3++;
  77. }
  78. }
  79. if (num > 0)
  80. {
  81. this.XMasters = (XMaster[]) list.ToArray(typeof(XMaster));
  82. this.SaveXMaster();
  83. }
  84. }
  85. return num;
  86. }
  87. private void fieee2msbin(float[] ff)
  88. {
  89. uint[] dst = new uint[ff.Length];
  90. Buffer.BlockCopy(ff, 0, dst, 0, ff.Length * 4);
  91. for (int i = 0; i < ff.Length; i++)
  92. {
  93. if (dst[i] != 0)
  94. {
  95. uint num = dst[i] >> 0x10;
  96. uint num2 = ((num << 1) & 0xff00) + 0x200;
  97. if ((num2 & 0x8000) == ((num << 1) & 0x8000))
  98. {
  99. num = (num & 0x7f) | ((num >> 8) & 0x80);
  100. num |= num2;
  101. dst[i] = (dst[i] & 0xffff) | (num << 0x10);
  102. }
  103. }
  104. }
  105. Buffer.BlockCopy(dst, 0, ff, 0, ff.Length * 4);
  106. }
  107. public Master FindByNumber(string Number)
  108. {
  109. foreach (Master master in this.Masters)
  110. {
  111. if (("F" + master.file_num) == Number)
  112. {
  113. return master;
  114. }
  115. }
  116. return null;
  117. }
  118. public Master FindBySymbol(string Symbol, out XMaster xm)
  119. {
  120. xm = null;
  121. if (this.Masters != null)
  122. {
  123. foreach (Master master in this.Masters)
  124. {
  125. if (master.symbol == Symbol)
  126. {
  127. return master;
  128. }
  129. }
  130. }
  131. if (this.XMasters != null)
  132. {
  133. foreach (XMaster master2 in this.XMasters)
  134. {
  135. if (master2.StockSymbol == Symbol)
  136. {
  137. xm = master2;
  138. return null;
  139. }
  140. }
  141. }
  142. return null;
  143. }
  144. private void fmsbin2ieee(float[] ff)
  145. {
  146. uint[] dst = new uint[ff.Length];
  147. Buffer.BlockCopy(ff, 0, dst, 0, ff.Length * 4);
  148. for (int i = 0; i < ff.Length; i++)
  149. {
  150. if (dst[i] != 0)
  151. {
  152. uint num = dst[i] >> 0x10;
  153. uint num2 = (num & 0xff00) - 0x200;
  154. num = (num & 0x7f) | ((num << 8) & 0x8000);
  155. num |= num2 >> 1;
  156. dst[i] = (dst[i] & 0xffff) | (num << 0x10);
  157. }
  158. }
  159. Buffer.BlockCopy(dst, 0, ff, 0, ff.Length * 4);
  160. }
  161. public override IDataProvider GetData(string Code, int Count)
  162. {
  163. CommonDataProvider cdp = new CommonDataProvider(this);
  164. string path = this.LookupDataFile(Code, cdp, ref this.Fields, ref this.TimeFrame);
  165. if ((path != "") && File.Exists(path))
  166. {
  167. using (FileStream stream = this.ReadData(path))
  168. {
  169. byte[] buffer = new byte[this.Fields * 4];
  170. byte[] buffer2 = new byte[stream.Length - buffer.Length];
  171. stream.Read(buffer, 0, buffer.Length);
  172. stream.Read(buffer2, 0, buffer2.Length);
  173. float[] dst = new float[buffer2.Length / 4];
  174. Buffer.BlockCopy(buffer2, 0, dst, 0, buffer2.Length);
  175. this.fmsbin2ieee(dst);
  176. int num = dst.Length / this.Fields;
  177. double[] numArray2 = new double[num];
  178. double[] numArray3 = new double[num];
  179. double[] numArray4 = new double[num];
  180. double[] numArray5 = new double[num];
  181. double[] numArray6 = new double[num];
  182. double[] numArray7 = new double[num];
  183. double[] numArray8 = new double[num];
  184. if (this.Fields == 5)
  185. {
  186. numArray3 = numArray6;
  187. numArray8 = numArray6;
  188. }
  189. for (int i = 0; i < num; i++)
  190. {
  191. int num3 = (int) dst[i * this.Fields];
  192. DateTime time = new DateTime((num3 / 0x2710) + 0x76c, (num3 / 100) % 100, num3 % 100);
  193. int num4 = 0;
  194. if ((this.Fields == 8) || (this.TimeFrame == MetaStockTimeFrame.Intraday))
  195. {
  196. int num5 = (int) dst[(i * this.Fields) + 1];
  197. time += new TimeSpan(num5 / 0x2710, (num5 / 100) % 100, num5 % 100);
  198. num4 = 1;
  199. }
  200. numArray2[i] = time.ToOADate();
  201. if (this.Fields >= 6)
  202. {
  203. numArray3[i] = dst[((i * this.Fields) + 1) + num4];
  204. numArray4[i] = dst[((i * this.Fields) + 2) + num4];
  205. numArray5[i] = dst[((i * this.Fields) + 3) + num4];
  206. numArray6[i] = dst[((i * this.Fields) + 4) + num4];
  207. numArray7[i] = dst[((i * this.Fields) + 5) + num4];
  208. numArray8[i] = numArray6[i];
  209. }
  210. else
  211. {
  212. numArray4[i] = dst[(i * this.Fields) + 1];
  213. numArray5[i] = dst[(i * this.Fields) + 2];
  214. numArray6[i] = dst[(i * this.Fields) + 3];
  215. numArray7[i] = dst[(i * this.Fields) + 4];
  216. }
  217. }
  218. cdp.LoadBinary(new double[][] { numArray3, numArray4, numArray5, numArray6, numArray7, numArray2, numArray8 });
  219. return cdp;
  220. }
  221. }
  222. cdp.LoadByteBinary(new byte[0]);
  223. return cdp;
  224. }
  225. private byte[] GetEmptyByteArray(int Count, byte Fill)
  226. {
  227. byte[] buffer = new byte[Count];
  228. for (int i = 0; i < buffer.Length; i++)
  229. {
  230. buffer[i] = Fill;
  231. }
  232. return buffer;
  233. }
  234. public override DataTable GetStockList(string Exchange, string ConditionId, string Key, string Sort, int StartRecords, int MaxRecords)
  235. {
  236. return base.RecordRange(this.GetTable(), StartRecords, MaxRecords);
  237. }
  238. public string GetSymbol(string Number)
  239. {
  240. Master master = this.FindByNumber(Number);
  241. if (master == null)
  242. {
  243. return "";
  244. }
  245. return master.symbol.Trim();
  246. }
  247. public override DataTable GetSymbolList(string Exchange, string ConditionId, string Key, string Sort, int StartRecords, int MaxRecords)
  248. {
  249. return base.RecordRange(this.GetTable(), StartRecords, MaxRecords);
  250. }
  251. public DataTable GetTable()
  252. {
  253. DataTable table = new DataTable();
  254. table.Columns.Add("Symbol");
  255. table.Columns.Add("Name");
  256. if (this.Masters != null)
  257. {
  258. foreach (Master master in this.Masters)
  259. {
  260. table.Rows.Add(new object[] { master.symbol, master.issue_name });
  261. }
  262. }
  263. if (this.XMasters != null)
  264. {
  265. foreach (XMaster master2 in this.XMasters)
  266. {
  267. table.Rows.Add(new object[] { master2.StockSymbol, master2.StockName });
  268. }
  269. }
  270. return table;
  271. }
  272. private void LoadMaster()
  273. {
  274. using (FileStream stream = this.ReadData(this.FilePath + "MASTER"))
  275. {
  276. this.Masters = new Master[(stream.Length / 0x35L) - 1L];
  277. using (BinaryReader reader = new BinaryReader(stream))
  278. {
  279. reader.ReadBytes(0x35);
  280. int index = 0;
  281. do
  282. {
  283. this.Masters[index] = new Master();
  284. this.Masters[index].file_num = reader.ReadByte();
  285. this.Masters[index].file_type = reader.ReadBytes(2);
  286. this.Masters[index].rec_len = reader.ReadByte();
  287. this.Masters[index].num_fields = reader.ReadByte();
  288. this.Masters[index].reserved1 = reader.ReadBytes(2);
  289. this.Masters[index].issue_name = Encoding.ASCII.GetString(reader.ReadBytes(0x10)).Trim();
  290. this.Masters[index].reserved2 = reader.ReadByte();
  291. this.Masters[index].CT_v2_8_flag = reader.ReadByte();
  292. this.Masters[index].first_date = reader.ReadSingle();
  293. this.Masters[index].last_date = reader.ReadSingle();
  294. this.Masters[index].time_frame = (MetaStockTimeFrame) reader.ReadByte();
  295. this.Masters[index].ida_time = reader.ReadInt16();
  296. this.Masters[index].symbol = Encoding.ASCII.GetString(reader.ReadBytes(14)).Trim();
  297. this.Masters[index].reserved3 = reader.ReadByte();
  298. this.Masters[index].flag = reader.ReadByte();
  299. this.Masters[index].reserved4 = reader.ReadByte();
  300. index++;
  301. }
  302. while (index < this.Masters.Length);
  303. }
  304. }
  305. }
  306. private void LoadXMaster()
  307. {
  308. using (FileStream stream = this.ReadData(this.FilePath + "XMASTER"))
  309. {
  310. this.XMasters = new XMaster[(stream.Length / 150L) - 1L];
  311. using (BinaryReader reader = new BinaryReader(stream))
  312. {
  313. reader.ReadBytes(150);
  314. int index = 0;
  315. do
  316. {
  317. this.XMasters[index] = new XMaster();
  318. this.XMasters[index].Unknown1 = reader.ReadByte();
  319. this.XMasters[index].StockSymbol = this.TrimToZero(Encoding.ASCII.GetString(reader.ReadBytes(15)));
  320. this.XMasters[index].StockName = this.TrimToZero(Encoding.ASCII.GetString(reader.ReadBytes(0x2e)));
  321. this.XMasters[index].TimeFrame = (MetaStockTimeFrame) reader.ReadByte();
  322. this.XMasters[index].Unknown2 = reader.ReadBytes(2);
  323. this.XMasters[index].Fn = reader.ReadInt16();
  324. this.XMasters[index].Unknown3 = reader.ReadBytes(13);
  325. this.XMasters[index].EndDate = reader.ReadInt32();
  326. this.XMasters[index].Unknown4 = reader.ReadBytes(20);
  327. this.XMasters[index].StartDate = reader.ReadInt32();
  328. this.XMasters[index].StartDate2 = reader.ReadInt32();
  329. this.XMasters[index].Unknown5 = reader.ReadBytes(4);
  330. this.XMasters[index].EndDate2 = reader.ReadInt32();
  331. this.XMasters[index].Unknown6 = reader.ReadBytes(30);
  332. index++;
  333. }
  334. while (index < this.XMasters.Length);
  335. }
  336. }
  337. }
  338. public string LookupDataFile(string Code, CommonDataProvider cdp)
  339. {
  340. return this.LookupDataFile(Code, cdp, ref this.Fields, ref this.TimeFrame);
  341. }
  342. public string LookupDataFile(string Code, CommonDataProvider cdp, ref int Fields, ref MetaStockTimeFrame TimeFrame)
  343. {
  344. if (cdp != null)
  345. {
  346. cdp.SetStringData("Code", Code);
  347. }
  348. foreach (Master master in this.Masters)
  349. {
  350. if (string.Compare(master.symbol, Code, true) == 0)
  351. {
  352. if (cdp != null)
  353. {
  354. cdp.SetStringData("Name", master.issue_name);
  355. }
  356. Fields = master.num_fields;
  357. TimeFrame = master.time_frame;
  358. return string.Concat(new object[] { this.FilePath, "F", master.file_num, ".DAT" });
  359. }
  360. }
  361. if (this.XMasters != null)
  362. {
  363. foreach (XMaster master2 in this.XMasters)
  364. {
  365. if (string.Compare(master2.StockSymbol, Code, true) == 0)
  366. {
  367. if (cdp != null)
  368. {
  369. cdp.SetStringData("Name", master2.StockName);
  370. }
  371. TimeFrame = master2.TimeFrame;
  372. return string.Concat(new object[] { this.FilePath, "F", master2.Fn, ".MWD" });
  373. }
  374. }
  375. }
  376. return "";
  377. }
  378. private FileStream ReadData(string Filename)
  379. {
  380. for (int i = 5; i >= 0; i--)
  381. {
  382. try
  383. {
  384. return new FileStream(Filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
  385. }
  386. catch
  387. {
  388. if (i == 0)
  389. {
  390. throw;
  391. }
  392. Thread.Sleep(100);
  393. }
  394. }
  395. return null;
  396. }
  397. public override void SaveData(string Symbol, IDataProvider idp, Stream OutStream, DateTime Start, DateTime End, bool Intraday)
  398. {
  399. CommonDataProvider cdp = (CommonDataProvider) idp;
  400. if ((Symbol != null) && (Symbol != ""))
  401. {
  402. XMaster master;
  403. int count = cdp.Count;
  404. Master master2 = this.FindBySymbol(Symbol, out master);
  405. bool flag = false;
  406. bool flag2 = false;
  407. this.Fields = (byte) (7 + (Intraday ? 1 : 0));
  408. if ((master2 == null) && (master == null))
  409. {
  410. int num2 = this.MaxNum + 1;
  411. if (num2 > 0xff)
  412. {
  413. master = new XMaster();
  414. master.Fn = (short) num2;
  415. ArrayList list = new ArrayList(this.XMasters);
  416. master.StockSymbol = Symbol;
  417. master.StockName = cdp.GetStringData("Name");
  418. if (master.StockName == null)
  419. {
  420. master.StockName = Symbol;
  421. }
  422. list.Add(master);
  423. this.XMasters = (XMaster[]) list.ToArray(typeof(XMaster));
  424. flag2 = true;
  425. }
  426. else
  427. {
  428. master2 = new Master();
  429. master2.file_num = (byte) num2;
  430. ArrayList list2 = new ArrayList(this.Masters);
  431. master2.symbol = Symbol;
  432. master2.num_fields = (byte) this.Fields;
  433. master2.issue_name = cdp.GetStringData("Name");
  434. if (master2.issue_name == null)
  435. {
  436. master2.issue_name = Symbol;
  437. }
  438. list2.Add(master2);
  439. this.Masters = (Master[]) list2.ToArray(typeof(Master));
  440. flag = true;
  441. }
  442. }
  443. double[] numArray = cdp["DATE"];
  444. double[] numArray2 = cdp["OPEN"];
  445. double[] numArray3 = cdp["HIGH"];
  446. double[] numArray4 = cdp["LOW"];
  447. double[] numArray5 = cdp["CLOSE"];
  448. double[] numArray6 = cdp["VOLUME"];
  449. double[] numArray7 = cdp["ADJCLOSE"];
  450. float[] ff = new float[count * this.Fields];
  451. for (int i = 0; i < count; i++)
  452. {
  453. int num4 = 0;
  454. DateTime time = DateTime.FromOADate(numArray[i]);
  455. ff[(i * this.Fields) + num4] = (((time.Year - 0x76c) * 0x2710) + (time.Month * 100)) + time.Day;
  456. if (this.Fields == 8)
  457. {
  458. num4 = 1;
  459. ff[(i * this.Fields) + num4] = ((time.Hour * 0x2710) + (time.Minute * 100)) + time.Second;
  460. }
  461. ff[((i * this.Fields) + 1) + num4] = (float) numArray2[i];
  462. ff[((i * this.Fields) + 2) + num4] = (float) numArray3[i];
  463. ff[((i * this.Fields) + 3) + num4] = (float) numArray4[i];
  464. ff[((i * this.Fields) + 4) + num4] = (float) numArray5[i];
  465. ff[((i * this.Fields) + 5) + num4] = (float) numArray6[i];
  466. ff[((i * this.Fields) + 6) + num4] = (float) numArray5[i];
  467. }
  468. this.fieee2msbin(ff);
  469. byte[] dst = new byte[ff.Length * 4];
  470. Buffer.BlockCopy(ff, 0, dst, 0, dst.Length);
  471. using (FileStream stream = File.Create(this.LookupDataFile(Symbol, cdp)))
  472. {
  473. stream.Write(dst, 0, dst.Length);
  474. }
  475. if (flag)
  476. {
  477. this.SaveMaster();
  478. }
  479. if (flag2)
  480. {
  481. this.SaveXMaster();
  482. }
  483. }
  484. }
  485. private void SaveMaster()
  486. {
  487. using (FileStream stream = File.Create(this.FilePath + "MASTER"))
  488. {
  489. using (BinaryWriter writer = new BinaryWriter(stream))
  490. {
  491. foreach (Master master in this.Masters)
  492. {
  493. writer.Write(master.file_num);
  494. writer.Write(master.file_type);
  495. writer.Write(master.rec_len);
  496. writer.Write((byte) this.Fields);
  497. writer.Write(master.reserved1);
  498. if ((master.issue_name == null) || (master.issue_name == ""))
  499. {
  500. master.issue_name = master.symbol;
  501. }
  502. writer.Write(this.StringToBytes(master.issue_name, 0x10, 0x20));
  503. writer.Write(master.reserved2);
  504. writer.Write(master.CT_v2_8_flag);
  505. writer.Write(master.first_date);
  506. writer.Write(master.last_date);
  507. writer.Write((byte) master.time_frame);
  508. writer.Write(master.ida_time);
  509. writer.Write(this.StringToBytes(master.symbol, 14, 0x20));
  510. writer.Write(master.reserved3);
  511. writer.Write(master.flag);
  512. writer.Write(master.reserved4);
  513. }
  514. }
  515. }
  516. }
  517. private void SaveXMaster()
  518. {
  519. using (FileStream stream = File.Create(this.FilePath + "XMaster"))
  520. {
  521. using (BinaryWriter writer = new BinaryWriter(stream))
  522. {
  523. if (this.XMasters != null)
  524. {
  525. foreach (XMaster master in this.XMasters)
  526. {
  527. writer.Write(master.Unknown1);
  528. writer.Write(this.StringToBytes(master.StockSymbol, 15, 0));
  529. writer.Write(this.StringToBytes(master.StockName, 0x2e, 0));
  530. writer.Write((byte) master.TimeFrame);
  531. writer.Write(master.Unknown2);
  532. writer.Write(master.Fn);
  533. writer.Write(master.Unknown3);
  534. writer.Write(master.EndDate);
  535. writer.Write(master.Unknown4);
  536. writer.Write(master.StartDate);
  537. writer.Write(master.StartDate2);
  538. writer.Write(master.Unknown5);
  539. writer.Write(master.EndDate2);
  540. writer.Write(master.Unknown6);
  541. }
  542. }
  543. }
  544. }
  545. }
  546. private byte[] StringToBytes(string s, int Count, byte Fill)
  547. {
  548. byte[] emptyByteArray = this.GetEmptyByteArray(Count, Fill);
  549. Encoding.ASCII.GetBytes(s, 0, s.Length, emptyByteArray, 0);
  550. return emptyByteArray;
  551. }
  552. public override int SymbolCount(string Exchange, string ConditionId, string Key)
  553. {
  554. return this.GetTable().Rows.Count;
  555. }
  556. private string TrimToZero(string s)
  557. {
  558. for (int i = 0; i < s.Length; i++)
  559. {
  560. if (s[i] == '\0')
  561. {
  562. return s.Substring(0, i);
  563. }
  564. }
  565. return s;
  566. }
  567. public override DataTable Exchanges
  568. {
  569. get
  570. {
  571. DataTable table = new DataTable();
  572. table.Columns.Add("Value");
  573. table.Columns.Add("Text");
  574. table.Rows.Add(new object[] { "", "ALL" });
  575. return table;
  576. }
  577. }
  578. private int MaxNum
  579. {
  580. get
  581. {
  582. int fn = 1;
  583. if (this.Masters != null)
  584. {
  585. foreach (Master master in this.Masters)
  586. {
  587. if (fn < master.file_num)
  588. {
  589. fn = master.file_num;
  590. }
  591. }
  592. }
  593. if (this.XMasters != null)
  594. {
  595. foreach (XMaster master2 in this.XMasters)
  596. {
  597. if (fn < master2.Fn)
  598. {
  599. fn = master2.Fn;
  600. }
  601. }
  602. }
  603. return fn;
  604. }
  605. }
  606. }
  607. }