Pastebin: 「日本株全銘柄 株価一覧表・投資指標データ」(こもりばん氏サイト)から株価取得

Format
Plain text
Post date
2017-11-21 18:15
Publication Period
Unlimited
  1. /*
  2. このプログラムは「日本株全銘柄 株価一覧表・投資指標データ」が提供するデータを利用しています。
  3. 日本株全銘柄 株価一覧表・投資指標データ
  4. https://hesonogoma.com/stocks/
  5. */
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Diagnostics;
  9. using System.IO;
  10. using System.Collections;
  11. using System.Net;
  12. using Zanetti.Data;
  13. namespace Zanetti.DataSource.Specialized
  14. {
  15. internal class KomoribanDataSource : DailyDataSource
  16. {
  17. private const string SITE_ID = "trial";
  18. private const string SITE_PASSWORD = "PW@20170129";
  19. public KomoribanDataSource(int[] dates) : base(dates)
  20. {
  21. }
  22. public override void Run()
  23. {
  24. DateTime ld = Util.GuessLatestTradeDate();
  25. var date = int.Parse(ld.ToString("yyyyMMdd"));
  26. var newdata = new Dictionary<int, NewDailyData>();
  27. for(int i=0;i<4;i++)
  28. {
  29. FillData(date, MakeUrl(date, i), ref newdata);
  30. }
  31. SendMessage(AsyncConst.WM_ASYNCPROCESS, date | DATE_MASK, AsyncConst.LPARAM_PROGRESS_SUCCESSFUL);
  32. foreach (AbstractBrand br in Env.BrandCollection.Values)
  33. {
  34. if (br.Market == MarketType.B && !IsSupportedIndex(br.Code) || br.Market == MarketType.Custom)
  35. continue;
  36. using (var farm = (DailyDataFarm)br.CreateDailyFarm(_dates.Length))
  37. {
  38. var traceFlag = false;
  39. NewDailyData td;
  40. if (newdata.TryGetValue(br.Code, out td))
  41. {
  42. farm.UpdateDataFarm(date, td);
  43. }
  44. else
  45. {
  46. if (traceFlag)
  47. continue;
  48. traceFlag = true;
  49. Debug.WriteLine("Data not found(Komoriban) : code=" + br.Code + " market=" + br.Market);
  50. }
  51. farm.Save(Util.GetDailyDataFileName(br.Code));
  52. SendMessage(AsyncConst.WM_ASYNCPROCESS, br.Code, AsyncConst.LPARAM_PROGRESS_SUCCESSFUL);
  53. }
  54. }
  55. }
  56. private void FillData(int date, string url, ref Dictionary<int, NewDailyData> result)
  57. {
  58. var uri = new Uri(url);
  59. using (var webClient = new System.Net.WebClient())
  60. {
  61. webClient.Credentials = new NetworkCredential(SITE_ID, SITE_PASSWORD);
  62. using (var reader = new StreamReader(webClient.OpenRead(url), System.Text.Encoding.GetEncoding("shift_jis")))
  63. {
  64. string line;
  65. while ((line = reader.ReadLine()) != null)
  66. {
  67. var tokens = line.Replace("\"","").Split(',');
  68. if (tokens.Length != 17 || tokens[0].Length != 4)
  69. continue;
  70. var code = int.Parse(tokens[0]);
  71. var br = Env.BrandCollection.FindBrand(code) as BasicBrand;
  72. if (br == null || !CheckMarket(br.Market, tokens[2]))
  73. continue;
  74. if (String2Int(tokens[4]) != date)
  75. continue;
  76. var volume = (int)ParseField(tokens[12]);
  77. const int shift = 10; // 株価は10倍で記録
  78. var td = new NewDailyData
  79. {
  80. volume = volume,
  81. open = (int)(ParseField(tokens[9]) * shift),
  82. high = (int)(ParseField(tokens[10]) * shift),
  83. low = (int)(ParseField(tokens[11]) * shift),
  84. close = (int)(ParseField(tokens[5]) * shift)
  85. };
  86. result.Add(code, td);
  87. }
  88. }
  89. }
  90. }
  91. private string MakeUrl(int date, int kind)
  92. {
  93. var url_base = "https://hesonogoma.com/stocks/download/csv";
  94. switch (kind)
  95. {
  96. case 0://個別株
  97. return string.Format("{0}/japan-all-stock-prices/daily/japan-all-stock-prices_{1:D8}.csv", url_base, date);
  98. case 1://ETF・ETN
  99. return string.Format("{0}/tosho-etf-stock-prices/daily/tosho-etf-stock-prices_{1:D8}.csv", url_base, date);
  100. case 2://REIT
  101. return string.Format("{0}/tosho-reit-stock-prices/daily/tosho-reit-stock-prices_{1:D8}.csv", url_base, date);
  102. case 3://上場ファンド
  103. return string.Format("{0}/tosho-fund-and-others-stock-prices/daily/tosho-fund-and-others-stock-prices_{1:D8}.csv", url_base, date);
  104. }
  105. return null;
  106. }
  107. private class BrandComparer : IComparer
  108. {
  109. public int Compare(object x, object y)
  110. {
  111. return ((AbstractBrand)x).Code - ((AbstractBrand)y).Code;
  112. }
  113. }
  114. private bool CheckMarket(MarketType market, string name)
  115. {
  116. switch (name)
  117. {
  118. case "東証":
  119. case "東証1部":
  120. case "東証一部":
  121. return market == MarketType.T1;
  122. case "東証2部":
  123. case "東証二部":
  124. return market == MarketType.T2;
  125. case "東証マザーズ":
  126. case "東証マザ":
  127. case "マザーズ":
  128. case "マザ":
  129. case "東証マザーズ外国":
  130. case "東証マザーズ審査": //20150119 k-db仕様変更にて追加
  131. return market == MarketType.M;
  132. case "東証TPM":
  133. return market == MarketType.Custom;
  134. case "東証1部外国":
  135. case "東証一部外国":
  136. return market == MarketType.T1;
  137. case "東証2部外国":
  138. case "東証二部外国":
  139. return market == MarketType.T2;
  140. case "大証":
  141. case "大証1部":
  142. return market == MarketType.O1;
  143. case "大証2部":
  144. return market == MarketType.O2;
  145. case "東証JQグロース":
  146. case "東証JQスタンダード":
  147. case "東証JQスタンダード外国":
  148. case "JQ":
  149. case "JQS":
  150. case "JQG":
  151. case "JQスタンダード":
  152. case "JQスタンダード外国":
  153. case "JQグロース":
  154. case "JQNEO":
  155. return market == MarketType.J;
  156. case "HCスタンダード":
  157. case "HCスタンダード外国":
  158. case "HCグロース":
  159. return market == MarketType.H;
  160. case "福証":
  161. case "福証QB":
  162. case "福証Q-Board":
  163. case "札証":
  164. case "札証アンビシャス":
  165. case "札証アンビ":
  166. case "名証":
  167. case "名証一部":
  168. case "名証二部":
  169. case "名証セント":
  170. return false;
  171. }
  172. return false;
  173. }
  174. private double ParseField(string s)
  175. {
  176. // 空文字列を0として扱う。
  177. return s == "" ? 0 : double.Parse(s);
  178. }
  179. private int String2Int(string s)
  180. {
  181. DateTime dt = DateTime.Now;
  182. return DateTime.TryParse(s, out dt) ? int.Parse(dt.ToString("yyyyMMdd")) : -1;
  183. }
  184. private bool IsSupportedIndex(int code)
  185. {
  186. return code == (int)BuiltInIndex.Nikkei225 ||
  187. code == (int)BuiltInIndex.TOPIX ||
  188. //code == (int)BuiltInIndex.MOTHERS ||
  189. code == (int)BuiltInIndex.JASDAQ;
  190. }
  191. }
  192. }
Download Printable view

URL of this paste

Embed with JavaScript

Embed with iframe

Raw text