コピペ: KABU+(こもりばん氏サイト)から株価取得 差し替え その1

形式
Plain text
投稿日時
2018-02-25 21:57
公開期間
無期限
  1. /*
  2. このプログラムはKabu+が提供するデータを利用しています。
  3. 日本株全銘柄 株価一覧表 https://kabu.plus/
  4. */
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Diagnostics;
  8. using System.IO;
  9. using System.Net;
  10. using Zanetti.Data;
  11. //株価一覧表(新フォーマット)
  12. //SC(株価コード)、名称、市場、業種、日時、株価、前日比、前日終値、始値、高値、安値、VWAP、出来高、出来高率、売買代金、時価総額、値幅下限、値幅上限、高値日付、年初来高値、年初来高値乖離率、安値日付、年初来安値、年初来安値乖離率
  13. //1時間当たりのアクセス回数制限がStandardコースで4回なので、複数日付株価取得は不可能と思われます(未確認)
  14. namespace Zanetti.DataSource.Specialized
  15. {
  16. internal class KabuPlusDataSource : DailyDataSource
  17. {
  18. public KabuPlusDataSource(int[] dates) : base(dates)
  19. {
  20. }
  21. //適宜、IDとパスワードを変更
  22. private const string SITE_ID = "trial";
  23. private const string SITE_PASSWORD = "PW@20170129";
  24. private const int mindate = 20170104;//データがこれ以前は存在しない。ただし、1年分の株価しか閲覧できない仕様がどうなるかを確かめてから仕様を固める
  25. public override void Run()
  26. {
  27. //var ld = Util.GuessLatestTradeDate();
  28. var date = _dates[0];//時間当たりのデータ取得制限のため指定した日付のみ取得
  29. var newdata = new Dictionary<int, NewDailyData>();
  30. for (int i = 0; i < 4; i++)
  31. {
  32. FillData(date, MakeUrl(date, i), ref newdata);
  33. }
  34. foreach (AbstractBrand br in Env.BrandCollection.Values)
  35. {
  36. if (br.Market == MarketType.B && !IsSupportedIndex(br.Code) || br.Market == MarketType.Custom || ((BasicBrand)br).Obsolete)
  37. continue;
  38. using (var farm = (DailyDataFarm)br.CreateDailyFarm(_dates.Length))
  39. {
  40. var traceFlag = false;
  41. if (date < mindate) continue;
  42. NewDailyData td;
  43. if (newdata.TryGetValue(br.Code, out td))
  44. {
  45. farm.UpdateDataFarm(date, td);
  46. }
  47. else
  48. {
  49. if (traceFlag)
  50. continue;
  51. traceFlag = true;
  52. Debug.WriteLine("Data not found(KabuPlus) : code=" + br.Code + " market=" + br.Market);
  53. }
  54. farm.Save(Util.GetDailyDataFileName(br.Code));
  55. SendMessage(AsyncConst.WM_ASYNCPROCESS, br.Code, AsyncConst.LPARAM_PROGRESS_SUCCESSFUL);
  56. }
  57. }
  58. }
  59. private readonly Dictionary<DateTime, int> _marketVolume = new Dictionary<DateTime, int>();
  60. private readonly Dictionary<DateTime, NewDailyData> _nikkei225 = new Dictionary<DateTime, NewDailyData>();
  61. private readonly Dictionary<DateTime, NewDailyData> _topix = new Dictionary<DateTime, NewDailyData>();
  62. private void InsertIndices(DateTime date, Dictionary<int, NewDailyData> result)
  63. {
  64. result[(int)BuiltInIndex.Nikkei225] = _nikkei225[date];
  65. result[(int)BuiltInIndex.TOPIX] = _topix[date];
  66. }
  67. private void FillData(int date, string url, ref Dictionary<int, NewDailyData> result)
  68. {
  69. var uri = new Uri(url);
  70. using (var webClient = new System.Net.WebClient())
  71. {
  72. webClient.Credentials = new NetworkCredential(SITE_ID, SITE_PASSWORD);
  73. //var enc = new System.Text.UTF8Encoding();
  74. using (var reader = new StreamReader(webClient.OpenRead(url), System.Text.Encoding.GetEncoding("shift_jis")))
  75. {
  76. string line;
  77. while ((line = reader.ReadLine()) != null)
  78. {
  79. var tokens = line.Replace("\"", "").Split(',');
  80. if (tokens.Length != 17 || tokens[0].Length != 4)
  81. continue;
  82. var code = int.Parse(tokens[0]);
  83. code = code == 1 ? 101 : code == 2 ? 102 : code;
  84. var br = Env.BrandCollection.FindBrand(code) as BasicBrand;
  85. if (br == null || !CheckMarket(br.Market, tokens[2]))//個別のみ通す
  86. continue;
  87. if (String2Int(tokens[4]) != date)
  88. continue;
  89. var volume = (int)ParseField(tokens[13]);
  90. var shift = IsIndex(code) ? 100 : 10; // 指数は100倍、株式は10倍で記録する
  91. var td = new NewDailyData
  92. {
  93. volume = volume,
  94. open = (int)(ParseField(tokens[9]) * shift),
  95. high = (int)(ParseField(tokens[10]) * shift),
  96. low = (int)(ParseField(tokens[11]) * shift),
  97. close = (int)(ParseField(tokens[5]) * shift)
  98. };
  99. result.Add(code, td);
  100. }
  101. }
  102. }
  103. }
  104. private string MakeUrl(int date, int kind)
  105. {
  106. var url_base = "https://csvex.com/kabu.plus/csv";
  107. switch (kind)
  108. {
  109. case 0://個別株
  110. return string.Format("{0}/japan-all-stock-prices/daily/japan-all-stock-prices_{1:D8}.csv", url_base, date);
  111. case 1://ETF・ETN
  112. return string.Format("{0}/tosho-etf-stock-prices/daily/tosho-etf-stock-prices_{1:D8}.csv", url_base, date);
  113. case 2://REIT
  114. return string.Format("{0}/tosho-reit-stock-prices/daily/tosho-reit-stock-prices_{1:D8}.csv", url_base, date);
  115. case 3://上場ファンド
  116. return string.Format("{0}/tosho-fund-and-others-stock-prices/daily/tosho-fund-and-others-stock-prices_{1:D8}.csv", url_base, date);
  117. }
  118. return null;
  119. }
  120. private bool IsIndex(int code)
  121. {
  122. return code == (int)BuiltInIndex.Nikkei225 ||
  123. code == (int)BuiltInIndex.TOPIX;
  124. }
  125. private int String2Int(string s)
  126. {
  127. DateTime dt = DateTime.Now;
  128. return DateTime.TryParse(s, out dt) ? int.Parse(dt.ToString("yyyyMMdd")) : -1;
  129. }
  130. private bool CheckMarket(MarketType market, string name)
  131. {
  132. switch (name)
  133. {
  134. case "東証":
  135. case "東証1部":
  136. case "東証一部":
  137. return market == MarketType.T1;
  138. case "東証2部":
  139. case "東証二部":
  140. return market == MarketType.T2;
  141. case "東証マザ":
  142. case "東証マザーズ":
  143. case "東証マザーズ外国":
  144. case "東証マザーズ審査":
  145. return market == MarketType.M;
  146. case "東証TPM":
  147. return market == MarketType.Custom;
  148. case "東証1部外国":
  149. return market == MarketType.T1;
  150. case "東証2部外国":
  151. return market == MarketType.T2;
  152. case "大証":
  153. case "大証1部":
  154. return market == MarketType.O1;
  155. case "大証2部":
  156. return market == MarketType.O2;
  157. case "東証JQグロース":
  158. case "東証JQスタンダード":
  159. case "東証JQスタンダード外国":
  160. case "JQ":
  161. case "JQS":
  162. case "JQスタンダード":
  163. case "JQスタンダード外国":
  164. case "JQグロース":
  165. case "JQG":
  166. case "JQNEO":
  167. return market == MarketType.J;
  168. case "HCスタンダード":
  169. case "HCスタンダード外国":
  170. case "HCグロース":
  171. return market == MarketType.H;
  172. case "福証":
  173. case "福証QB":
  174. case "福証Q-Board":
  175. case "札証":
  176. case "札証アンビシャス":
  177. case "札証アンビ":
  178. case "名証":
  179. case "名証一部":
  180. case "名証二部":
  181. case "名証セント":
  182. return false;
  183. }
  184. return false;
  185. }
  186. private bool IsSupportedIndex(int code)
  187. {
  188. return code == (int)BuiltInIndex.Nikkei225 ||
  189. code == (int)BuiltInIndex.TOPIX;
  190. }
  191. private double ParseField(string s)
  192. {
  193. // 空文字列を0として扱う。
  194. return s.Replace("-","").Replace("\t","").Trim() == "" ? 0 : double.Parse(s);
  195. }
  196. }
  197. }
ダウンロード 印刷用表示

このコピペの URL

JavaScript での埋め込み

iframe での埋め込み

元のテキスト