コピペ: OmegaChart ファイルをドロップしてデータ更新 Import.cs 差し替えその1 新規追加ファイル

形式
Plain text
投稿日時
2019-02-11 11:31
公開期間
無期限
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Windows.Forms;
  5. using Zanetti.Commands;
  6. namespace Zanetti.Data
  7. {
  8. internal abstract class DataImporter
  9. {
  10. private const int SHIFT = 10;//株価10倍格納する場合
  11. private const int RECORD_LENGTH = 32;
  12. public static void ImportData(string[] fileName)
  13. {
  14. var code = -1;
  15. DataFarm farm = null;
  16. var dicCsv = new SortedDictionary<int, NewDailyData2>();//読み込みCSVファイル情報
  17. var dicFarm = new SortedDictionary<int, NewDailyData2>();//OmegaChartのdataフォルダ配下の情報
  18. var dicResult = new SortedDictionary<int, NewDailyData2>();//比較結果格納
  19. if (fileName.Length == 0) return;
  20. var path = fileName[0];
  21. if (IsTextFile(path))//複数ファイルNG
  22. {
  23. ReadCsv(path, ref dicCsv);
  24. DecisionCodeAndFarm(path, ref code, ref farm);
  25. if (farm.RawDataImage!=null)
  26. ReadFarm(farm, ref dicFarm);
  27. CompareCsvFarm(farm, dicCsv, dicFarm, ref dicResult);
  28. UpdateFarm(dicResult, ref dicFarm);
  29. WriteFarm(code, dicResult, dicFarm);
  30. }
  31. }
  32. private static void DecisionCodeAndFarm(string path, ref int code, ref DataFarm farm)
  33. {
  34. farm = Env.Frame.ChartCanvas.GetBrand().ReserveFarm();
  35. code = Path2IntCode(path);
  36. if (code < 0)//パス名が数字じゃなかったら、表示中の銘柄にインポートするものとする
  37. {
  38. code = farm.Brand.Code;
  39. }
  40. else//パス名が数字4文字含みだったら銘柄コードと決めつける
  41. {
  42. AbstractBrand br = Env.BrandCollection.FindBrand(code);
  43. if (br == null)//銘柄コードじゃなかったら戻す farmは最初から表示中のfarmを入れてある
  44. code = farm.Brand.Code;
  45. else
  46. farm = Env.BrandCollection.ReserveFarm(br);
  47. }
  48. }
  49. private static int Path2IntCode(string path)
  50. {
  51. var code = -1;
  52. var s = Path.GetFileName(path.Trim());
  53. if (s == string.Empty) return code;
  54. var sp = s.Split('.');
  55. if (sp[0].Length == 4)
  56. int.TryParse(sp[0], out code);
  57. return code;
  58. }
  59. private static void MakeBackupFile(int code)
  60. {
  61. var source = Util.GetDailyDataFileName(code);
  62. var dest = string.Format("{0}\\{1}.bak", Path.GetDirectoryName(source), code);
  63. File.Copy(source, dest, true);
  64. }
  65. private static void UpdateFarm(SortedDictionary<int, NewDailyData2> dicResult, ref SortedDictionary<int, NewDailyData2> dicFarm)
  66. {
  67. List<int> keyListResult = new List<int>(dicResult.Keys);
  68. foreach (var e in keyListResult)
  69. {
  70. if (dicFarm.ContainsKey(e))//キー(日付)が重複、かつ変更された日付の情報
  71. {
  72. //dicFarm[e] = dicResult[e];
  73. dicFarm[e].open = dicResult[e].open;
  74. dicFarm[e].high = dicResult[e].high;
  75. dicFarm[e].low = dicResult[e].low;
  76. dicFarm[e].close = dicResult[e].close;
  77. dicFarm[e].volume = dicResult[e].volume;
  78. }
  79. else//新規データ
  80. {
  81. dicFarm.Add(e, dicResult[e]);
  82. }
  83. }
  84. }
  85. private static void WriteFarm(int code, SortedDictionary<int, NewDailyData2> dicResult, SortedDictionary<int, NewDailyData2> dicFarm)
  86. {
  87. if (dicResult.Count > 0)
  88. {
  89. var message = string.Empty;
  90. message = string.Format("OmegaChartのデータを変更しますか?\n\n銘柄コード:{0:D4} 変更箇所数:{1}\n", code, dicResult.Count);
  91. List<int> keyListResult = new List<int>(dicResult.Keys);
  92. var cnt = 0;
  93. foreach (var key in keyListResult)
  94. {
  95. message += string.Format("{0:D8},{1},{2},{3},{4},{5}\n"
  96. , key
  97. , dicResult[key].open / SHIFT
  98. , dicResult[key].high / SHIFT
  99. , dicResult[key].low / SHIFT
  100. , dicResult[key].close / SHIFT
  101. , dicResult[key].volume);
  102. cnt++;
  103. if (cnt > 3)
  104. {
  105. message += "・・・以下略・・・";
  106. break;
  107. }
  108. }
  109. DialogResult result = MessageBox.Show(message, "質問", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2);
  110. if (result == DialogResult.OK)
  111. {
  112. MakeBackupFile(code);
  113. UpdateDataFarm(code, dicFarm);
  114. AbstractBrand br = Env.BrandCollection.FindBrand(code);
  115. CommandExec.ShowBrand(br);
  116. //CommandExec.RefreshChart();
  117. }
  118. }
  119. }
  120. private static void ReadFarm(DataFarm farm, ref SortedDictionary<int, NewDailyData2> dic)
  121. {
  122. for (var i = 0; i < farm.RawDataImage.Length / 32; i++)
  123. {
  124. var date = BitConverter.ToInt32(farm.RawDataImage, i * 32);
  125. NewDailyData2 ndd = new NewDailyData2();
  126. ndd.open = BitConverter.ToInt32(farm.RawDataImage, i * 32 + 4);
  127. ndd.high = BitConverter.ToInt32(farm.RawDataImage, i * 32 + 8);
  128. ndd.low = BitConverter.ToInt32(farm.RawDataImage, i * 32 + 12);
  129. ndd.close = BitConverter.ToInt32(farm.RawDataImage, i * 32 + 16);
  130. ndd.volume = BitConverter.ToInt32(farm.RawDataImage, i * 32 + 20);
  131. ndd.creditlong = BitConverter.ToInt32(farm.RawDataImage, i * 32 + 24);
  132. ndd.creditshort = BitConverter.ToInt32(farm.RawDataImage, i * 32 + 28);
  133. dic.Add(date, ndd);
  134. }
  135. }
  136. private static void CompareCsvFarm(DataFarm farm, SortedDictionary<int, NewDailyData2> dicCsv, SortedDictionary<int, NewDailyData2> dicFarm, ref SortedDictionary<int, NewDailyData2> dicResult)
  137. {
  138. List<int> keyList = new List<int>(dicCsv.Keys);
  139. foreach (var key in keyList)
  140. {
  141. if (dicCsv[key].volume == 0)
  142. {//個別銘柄で出来高ゼロの場合は特別作業が必要。OmegaChartは出来高がない日は前の日の値で埋められているため
  143. if (farm.Brand != null && farm.Brand.Market != MarketType.B && farm.Brand.Market != MarketType.Custom)
  144. {
  145. NewDailyData2 ndd = new NewDailyData2();
  146. ndd.open = 0;
  147. ndd.high = 0;
  148. ndd.low = 0;
  149. ndd.close = 0;
  150. ndd.volume = 0;
  151. dicCsv[key] = ndd;
  152. }
  153. }
  154. if (!dicFarm.ContainsKey(key))
  155. dicResult.Add(key, dicCsv[key]);
  156. else
  157. {
  158. if (dicFarm[key].open != dicCsv[key].open
  159. || dicFarm[key].high != dicCsv[key].high
  160. || dicFarm[key].low != dicCsv[key].low
  161. || dicFarm[key].close != dicCsv[key].close
  162. || dicFarm[key].volume != dicCsv[key].volume)
  163. dicResult.Add(key, dicCsv[key]);
  164. }
  165. }
  166. }
  167. private static void ReadCsv(string filename, ref SortedDictionary<int, NewDailyData2> dic)
  168. {
  169. System.Text.Encoding enc = System.Text.Encoding.GetEncoding("shift_jis");
  170. var str = System.IO.File.ReadAllText(filename, enc).Replace("\r", "");
  171. var lines = str.Split('\n');
  172. for (var i = 0; i < lines.Length; i++)
  173. {
  174. var sp = lines[i].Split(',');
  175. if (sp.Length != 6) continue;
  176. var dt = DateTime.MinValue;
  177. NewDailyData2 ndd = new NewDailyData2();
  178. double open = .0;
  179. double high = .0;
  180. double low = .0;
  181. double close = .0;
  182. if (DateTime.TryParse(sp[0], out dt)
  183. && double.TryParse(sp[1], out open)
  184. && double.TryParse(sp[2], out high)
  185. && double.TryParse(sp[3], out low)
  186. && double.TryParse(sp[4], out close)
  187. && int.TryParse(sp[5], out ndd.volume))
  188. {
  189. ndd.open = (int)(open * SHIFT);
  190. ndd.high = (int)(high * SHIFT);
  191. ndd.low = (int)(low * SHIFT);
  192. ndd.close = (int)(close * SHIFT);
  193. if (dt != DateTime.MinValue)
  194. dic.Add(int.Parse(dt.ToString("yyyyMMdd")), ndd);
  195. }
  196. }
  197. }
  198. private static void UpdateDataFarm(int code, SortedDictionary<int, NewDailyData2> prices)
  199. {
  200. var farm = (DailyDataFarm)Env.BrandCollection.FindBrand(code).CreateDailyFarm(prices.Count);
  201. var empty = farm.IsEmpty;
  202. var skip = true;
  203. foreach (var pair in prices)
  204. {
  205. if (empty && skip && pair.Value.volume == 0)
  206. continue;
  207. skip = false;
  208. farm.UpdateDataFarm(pair.Key, pair.Value);
  209. }
  210. farm.Save(Util.GetDailyDataFileName(code));
  211. }
  212. private static bool IsTextFile(string filePath)
  213. {
  214. var file = new System.IO.FileStream(filePath, FileMode.Open, FileAccess.Read);
  215. var byteData = new byte[1];
  216. while (file.Read(byteData, 0, byteData.Length) > 0)
  217. {
  218. if (byteData[0] == 0)
  219. return false;
  220. }
  221. return true;
  222. }
  223. }
  224. }
ダウンロード 印刷用表示

このコピペの URL

JavaScript での埋め込み

iframe での埋め込み

元のテキスト