Pastebin: OmegaChart 株価がなぜか置いてあるサイトから株価取得

Format
Plain text
Post date
2017-12-14 19:20
Publication Period
Unlimited
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Text;
  6. using Zanetti.Data;
  7. namespace Zanetti.DataSource.Specialized
  8. {
  9. internal class DatadownloadDataSource : DailyDataSource
  10. {
  11. public DatadownloadDataSource(int[] dates) : base(dates)
  12. {
  13. }
  14. public override void Run()
  15. {
  16. var newdata = new Dictionary<int, Dictionary<int, NewDailyData>>();
  17. var mindate = 20171207;//データがこれ以前は存在しない。
  18. foreach (var date in _dates)
  19. {
  20. if (date < mindate) continue;
  21. newdata[date] = FillData(date);
  22. SendMessage(AsyncConst.WM_ASYNCPROCESS, date | DATE_MASK, AsyncConst.LPARAM_PROGRESS_SUCCESSFUL);
  23. }
  24. foreach (AbstractBrand br in Env.BrandCollection.Values)
  25. {
  26. if (br.Market == MarketType.B && !IsSupportedIndex(br.Code) || br.Market == MarketType.Custom)
  27. continue;
  28. using (var farm = (DailyDataFarm)br.CreateDailyFarm(_dates.Length))
  29. {
  30. var traceFlag = false;
  31. foreach (var date in _dates)
  32. {
  33. if (date < mindate) continue;
  34. NewDailyData td;
  35. if (newdata[date].TryGetValue(br.Code, out td))
  36. {
  37. farm.UpdateDataFarm(date, td);
  38. }
  39. else
  40. {
  41. if (traceFlag)
  42. continue;
  43. traceFlag = true;
  44. Debug.WriteLine("Data not found(datadownload.xdomain.jp) : code=" + br.Code + " market=" + br.Market);
  45. }
  46. }
  47. farm.Save(Util.GetDailyDataFileName(br.Code));
  48. SendMessage(AsyncConst.WM_ASYNCPROCESS, br.Code, AsyncConst.LPARAM_PROGRESS_SUCCESSFUL);
  49. }
  50. }
  51. }
  52. private readonly Dictionary<DateTime, int> _marketVolume = new Dictionary<DateTime, int>();
  53. private readonly Dictionary<DateTime, NewDailyData> _nikkei225 = new Dictionary<DateTime, NewDailyData>();
  54. private readonly Dictionary<DateTime, NewDailyData> _topix = new Dictionary<DateTime, NewDailyData>();
  55. private void InsertIndices(DateTime date, Dictionary<int, NewDailyData> result)
  56. {
  57. result[(int)BuiltInIndex.Nikkei225] = _nikkei225[date];
  58. result[(int)BuiltInIndex.TOPIX] = _topix[date];
  59. }
  60. private Dictionary<int, NewDailyData> FillData(int date)
  61. {
  62. var result = new Dictionary<int, NewDailyData>();
  63. var d2 = Util.IntToDate(date);
  64. var url = string.Format("http://datadownload.php.xdomain.jp/data/daily/{0:D8}.txt", date);
  65. using (var reader = new StreamReader(Util.HttpDownload(url), Encoding.GetEncoding("shift_jis")))
  66. {
  67. //InsertIndices(d2, result);
  68. string line;
  69. while ((line = reader.ReadLine()) != null)
  70. {
  71. var tokens = line.Split(',');
  72. if (tokens.Length != 6 || tokens[0].Length != 4)
  73. continue;
  74. var code = int.Parse(tokens[0]);
  75. var br = Env.BrandCollection.FindBrand(code) as BasicBrand;
  76. if (br == null)
  77. continue;
  78. var volume = (int)ParseField(tokens[5]);
  79. var shift = 10; // 株価は10倍で記録
  80. if (code < 1000) shift = 1;
  81. var td = new NewDailyData
  82. {
  83. volume = volume,
  84. open = (int)(ParseField(tokens[1]) * shift),
  85. high = (int)(ParseField(tokens[2]) * shift),
  86. low = (int)(ParseField(tokens[3]) * shift),
  87. close = (int)(ParseField(tokens[4]) * shift)
  88. };
  89. result[code] = td;
  90. }
  91. }
  92. return result;
  93. }
  94. private bool CheckMarket(MarketType market, string name)
  95. {
  96. switch (name)
  97. {
  98. case "東証":
  99. case "東証1部":
  100. return market == MarketType.T1;
  101. case "東証2部":
  102. return market == MarketType.T2;
  103. case "東証マザーズ":
  104. case "東証マザーズ外国":
  105. case "東証マザーズ審査": //20150119 k-db仕様変更にて追加
  106. return market == MarketType.M;
  107. case "東証TPM":
  108. return market == MarketType.Custom;
  109. case "東証1部外国":
  110. return market == MarketType.T1;
  111. case "東証2部外国":
  112. return market == MarketType.T2;
  113. case "大証":
  114. case "大証1部":
  115. return market == MarketType.O1;
  116. case "大証2部":
  117. return market == MarketType.O2;
  118. case "東証JQグロース":
  119. case "東証JQスタンダード":
  120. case "東証JQスタンダード外国":
  121. case "JQ":
  122. case "JQスタンダード":
  123. case "JQスタンダード外国":
  124. case "JQグロース":
  125. case "JQNEO":
  126. return market == MarketType.J;
  127. case "HCスタンダード":
  128. case "HCスタンダード外国":
  129. case "HCグロース":
  130. return market == MarketType.H;
  131. case "福証":
  132. case "福証Q-Board":
  133. case "札証":
  134. case "札証アンビシャス":
  135. case "名証":
  136. return false;
  137. }
  138. return false;
  139. }
  140. private bool IsSupportedIndex(int code)
  141. {
  142. return code == (int)BuiltInIndex.Nikkei225 ||
  143. code == (int)BuiltInIndex.TOPIX;
  144. }
  145. private double ParseField(string s)
  146. {
  147. // 空文字列を0として扱う。
  148. return s == "" ? 0 : double.Parse(s);
  149. }
  150. }
  151. }
Download Printable view

URL of this paste

Embed with JavaScript

Embed with iframe

Raw text