コピペ: OmegaChart KABU+から株価取得。ローカルファイル利用版。KabuPlus.cs その3

形式
Plain text
投稿日時
2018-10-22 22:05
公開期間
無期限
  1. using System;
  2. using System.Text;
  3. using System.IO;
  4. using System.Collections;
  5. using System.Diagnostics;
  6. using Zanetti.Data;
  7. namespace Zanetti.DataSource.Specialized
  8. {
  9. internal class KabuPlusDataSource : DailyDataSource
  10. {
  11. private bool _includesDomesticIndices;
  12. private const string HOZON_DIR = @"c:\temp";//要変更 KABU+からダウンロードしたCSVファイルを保存しておくフォルダ名
  13. public KabuPlusDataSource(int[] dates) : base(dates)
  14. {
  15. }
  16. public bool IncludesDomesticIndices
  17. {
  18. get
  19. {
  20. return _includesDomesticIndices;
  21. }
  22. set
  23. {
  24. _includesDomesticIndices = value;
  25. }
  26. }
  27. public override void Run()
  28. {
  29. Hashtable[] newdata = new Hashtable[_dates.Length];
  30. //データをまずダウンロード
  31. for (int i = 0; i < _dates.Length; i++)
  32. {
  33. newdata[i] = FillData(_dates[i]);
  34. SendMessage(AsyncConst.WM_ASYNCPROCESS, _dates[i] | DATE_MASK, AsyncConst.LPARAM_PROGRESS_SUCCESSFUL);
  35. }
  36. //各データの追加と保存
  37. IDictionaryEnumerator ie = Env.BrandCollection.GetEnumerator();
  38. while (ie.MoveNext())
  39. {
  40. AbstractBrand br = (AbstractBrand)ie.Value;
  41. if ((br.Market == MarketType.B && !IsMujinzouSupportedIndices(br.Code)) || br.Market == MarketType.Custom) continue;
  42. bool trace_flag = false;
  43. using (DailyDataFarm f = (DailyDataFarm)br.CreateDailyFarm(_dates.Length))
  44. {
  45. for (int i = 0; i < _dates.Length; i++)
  46. {
  47. NewDailyData td = (NewDailyData)newdata[i][br.Code];
  48. if (td == null)
  49. {
  50. if (!trace_flag)
  51. {
  52. trace_flag = true;
  53. Debug.WriteLine("Data not found(mujinzou) : code=" + br.Code + " market=" + br.Market.ToString());
  54. }
  55. }
  56. else
  57. f.UpdateDataFarm(_dates[i], td);
  58. }
  59. f.Save(Util.GetDailyDataFileName(br.Code));
  60. }
  61. SendMessage(AsyncConst.WM_ASYNCPROCESS, br.Code, AsyncConst.LPARAM_PROGRESS_SUCCESSFUL);
  62. }
  63. }
  64. //ある日付に対して、コードのint値からPanRollingTradeDataへのハッシュテーブルを構築して返す
  65. //japan-all-stock-prices-2 25列
  66. //SC,名称,市場,業種,日時,株価,前日比,前日比(%),前日終値,始値,高値,安値,VWAP,出来高,出来高率,売買代金(千円),時価総額(百万円),値幅下限,値幅上限,高値日付,年初来高値,年初来高値乖離率,安値日付,年初来安値,年初来安値乖離率
  67. //tosho-etf-stock-prices 17列
  68. //tosho-reit-stock-prices 17列
  69. //tosho-fund-and-others-stock-prices 17列
  70. //SC,名称,市場,業種,日時,株価,前日比,前日比(%),前日終値,始値,高値,安値,出来高,売買代金(千円),時価総額(百万円),値幅下限,値幅上限
  71. private Hashtable FillData(int date)
  72. {
  73. Hashtable result = new Hashtable();
  74. try
  75. {
  76. DateTime d2 = Util.IntToDate(date);
  77. var files = System.IO.Directory.GetFiles(HOZON_DIR, string.Format("*{0}*.csv", date), System.IO.SearchOption.TopDirectoryOnly);
  78. String2Hashtable(files, ref result);
  79. }
  80. catch (Exception ex)
  81. {
  82. Console.WriteLine(ex.Message);
  83. }
  84. return result;
  85. }
  86. private void String2Hashtable(string[] files, ref Hashtable result)
  87. {
  88. Array.Sort(files);
  89. foreach (var fn in files)
  90. {
  91. var is_japanallstockprices2 = false;
  92. if (!ChkFilename(fn, ref is_japanallstockprices2)) continue;
  93. using (var sr = new StreamReader(fn, System.Text.Encoding.GetEncoding("shift_jis")))
  94. {
  95. var line = sr.ReadLine();
  96. while (line != null)
  97. {
  98. line = line.Replace("\"", "");
  99. string[] t = line.Split(',');
  100. if (chk_t(t))
  101. {
  102. var code = ParseCode(t[0]);
  103. if (code == 0) continue;
  104. BasicBrand br = Env.BrandCollection.FindBrand(code) as BasicBrand;
  105. if (code == 101 || code == 102)
  106. {
  107. line = sr.ReadLine();
  108. continue;//日経平均とTOPIXはここで取らない場合は抜ける
  109. }
  110. if (br != null && CheckMarket(br, t[2], t[3]))
  111. {
  112. double vv = 1, pv = 10; // 株価は10倍で記録
  113. //倍率調整
  114. if (IsDomesticIndex(code))
  115. {
  116. vv = 0.001; //DreamVisorのものにあわせる格好で。1000株単位かな
  117. pv = 100;
  118. }
  119. if (code == (int)BuiltInIndex.TOPIX_F)
  120. { //TOPIX先物は整数単位で記録されている
  121. pv = 10;
  122. }
  123. NewDailyData td = new NewDailyData();
  124. try
  125. {
  126. td.volume = (int)(String2Double(t[is_japanallstockprices2 ? 13 : 12]) * vv);//個別株用のCSVファイルはその他と出来高の位置が違う
  127. NewDailyData existing = (NewDailyData)result[code];
  128. //Debug.WriteLine(line);
  129. td.open = (int)(String2Double(t[9]) * pv);
  130. td.high = (int)(String2Double(t[10]) * pv);
  131. td.low = (int)(String2Double(t[11]) * pv);
  132. td.close = (int)(String2Double(t[5]) * pv);
  133. }
  134. catch (Exception ex)
  135. {
  136. Console.WriteLine(ex.Message);
  137. }
  138. result[code] = td;
  139. }
  140. }
  141. line = sr.ReadLine();
  142. }
  143. }
  144. }
  145. }
  146. private bool ChkFilename(string filename, ref bool is_japanallstockprices2)
  147. {
  148. is_japanallstockprices2 = false;
  149. var ret = false;
  150. if (filename.IndexOf("japan-all-stock-prices-2") >= 0)
  151. {
  152. is_japanallstockprices2 = true;
  153. ret = true;
  154. }
  155. else
  156. {
  157. foreach (var e in new string[] { "tosho-etf-stock-prices", "tosho-reit-stock-prices", "tosho-fund-and-others-stock-prices" })
  158. {
  159. if (filename.IndexOf(e) >= 0)
  160. {
  161. ret = true;
  162. }
  163. }
  164. }
  165. return ret;
  166. }
  167. private double String2Double(string s)
  168. {
  169. var line = s.Replace("-", "0");
  170. if (s == string.Empty)
  171. line = "0";
  172. var f = .0;
  173. if (!double.TryParse(line, out f))
  174. f = 0;
  175. return f;
  176. }
  177. private bool chk_t(string[] t)
  178. {
  179. var ret = true;
  180. var d = 0;
  181. if (t[0].Length != 4) ret = false;
  182. if (!int.TryParse(t[0], out d)) ret = false;
  183. return ret;
  184. }
  185. private static bool CheckMarket(BasicBrand br, string mar, string gyo)
  186. {
  187. MarketType mt = br.Market;
  188. switch (mt)
  189. {
  190. //2006/12/26のデータより、東証はすべてコード11, 大証1・2部は21, ヘラクレスは23に変更になった。旧データを読んだときでも大丈夫なように古い判定も残しておく
  191. case MarketType.B:
  192. return mar == "東証"; //指数は東証一部として記録されている
  193. case MarketType.T1:
  194. return mar == "東証一部" || gyo == "ETF" || gyo == "ETN" || gyo == "REIT" || gyo == "インフラファンド" || gyo == "カントリーファンド" || gyo == "銀行";
  195. case MarketType.T2:
  196. return mar == "東証二部";
  197. case MarketType.M:
  198. return mar == "東証マザ";
  199. case MarketType.J:
  200. return mar == "JQG" || mar == "JQS" || gyo == "銀行";
  201. default:
  202. return false;
  203. }
  204. }
  205. private static bool IsMujinzouSupportedIndices(int code)
  206. {
  207. return code == (int)BuiltInIndex.Nikkei225 ||
  208. code == (int)BuiltInIndex.TOPIX ||
  209. code == (int)BuiltInIndex.JASDAQ ||
  210. code == (int)BuiltInIndex.Nikkei225_F ||
  211. code == (int)BuiltInIndex.TOPIX_F;
  212. }
  213. private static int ParseCode(string code)
  214. {
  215. int t = Util.ParseInt(code);
  216. //以下に該当するものでなければデータの取り込みは行われない
  217. if (t < 1300)
  218. {
  219. switch (t)
  220. {
  221. case 0001: return (int)BuiltInIndex.Nikkei225;
  222. case 0002: return (int)BuiltInIndex.TOPIX;
  223. case 1004: return (int)BuiltInIndex.Nikkei225_F;
  224. case 1005: return (int)BuiltInIndex.JASDAQ;
  225. case 1006: return (int)BuiltInIndex.TOPIX_F;
  226. default: return 0;
  227. }
  228. }
  229. else
  230. return t; //ふつうの銘柄
  231. }
  232. private static bool IsDomesticIndex(int code)
  233. {
  234. return code == (int)BuiltInIndex.Nikkei225 || code == (int)BuiltInIndex.TOPIX || code == (int)BuiltInIndex.JASDAQ;
  235. }
  236. }
  237. }
ダウンロード 印刷用表示

このコピペの URL

JavaScript での埋め込み

iframe での埋め込み

元のテキスト