Format
Plain text
Post date
2017-11-18 20:09
Zeitraum der Veröffentlichung
Unbegrenzt
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Text;
  6. using System.Collections;
  7. using NDde.Client;
  8. using Zanetti.Data;
  9. namespace Zanetti.DataSource.Specialized
  10. {
  11. internal class RakuteRssDataSource : DailyDataSource
  12. {
  13. private const int MAXRETRYCOUNT = 3;
  14. public RakuteRssDataSource(int[] dates)
  15. : base(dates)
  16. {
  17. }
  18. public override void Run()
  19. {
  20. if (!ChkRss(_dates[_dates.Length - 1]))
  21. {
  22. //RSSが動いてない旨、メッセージを出したいけどわからない
  23. //SendMessage(AsyncConst.WM_ASYNCPROCESS, 0, AsyncConst.LPARAM_ERROR);
  24. System.Windows.Forms.MessageBox.Show("楽天RSSが起動していません。","エラー", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
  25. return;
  26. }
  27. GetIndices(); // 先に指数のデータを読む。
  28. var newdata = new Dictionary<int, Dictionary<int, NewDailyData>>();
  29. foreach (var date in _dates)
  30. {
  31. newdata[date] = FillData(date);
  32. SendMessage(AsyncConst.WM_ASYNCPROCESS, date | DATE_MASK, AsyncConst.LPARAM_PROGRESS_SUCCESSFUL);
  33. }
  34. foreach (AbstractBrand br in Env.BrandCollection.Values)
  35. {
  36. if (br.Market == MarketType.B && !IsSupportedIndex(br.Code) || br.Market == MarketType.Custom)
  37. continue;
  38. using (var farm = (DailyDataFarm)br.CreateDailyFarm(_dates.Length))
  39. {
  40. var traceFlag = false;
  41. foreach (var date in _dates)
  42. {
  43. NewDailyData td;
  44. if (newdata[date].TryGetValue(br.Code, out td))
  45. {
  46. farm.UpdateDataFarm(date, td);
  47. }
  48. else
  49. {
  50. if (traceFlag)
  51. continue;
  52. traceFlag = true;
  53. Debug.WriteLine("Data not found(RakutenRSS) : code=" + br.Code + " market=" + br.Market);
  54. }
  55. }
  56. farm.Save(Util.GetDailyDataFileName(br.Code));
  57. SendMessage(AsyncConst.WM_ASYNCPROCESS, br.Code, AsyncConst.LPARAM_PROGRESS_SUCCESSFUL);
  58. }
  59. }
  60. }
  61. private readonly Dictionary<DateTime, int> _marketVolume = new Dictionary<DateTime, int>();
  62. private readonly Dictionary<DateTime, NewDailyData> _nikkei225 = new Dictionary<DateTime, NewDailyData>();
  63. private readonly Dictionary<DateTime, NewDailyData> _topix = new Dictionary<DateTime, NewDailyData>();
  64. //private readonly Dictionary<DateTime, NewDailyData> _mothers = new Dictionary<DateTime, NewDailyData>();
  65. private readonly Dictionary<DateTime, NewDailyData> _jasdaq = new Dictionary<DateTime, NewDailyData>();
  66. private void GetIndices()
  67. {
  68. GetMarketVolume();
  69. GetIndexValues();
  70. }
  71. private void GetMarketVolume()
  72. {
  73. var date = DateTime.Parse(GetRssReturnString("TV1", "現在日付"));
  74. var volume = double.Parse(GetRssReturnString("TV1", "出来高"));
  75. _marketVolume[date] = (int)(volume * 100);//楽天RSS、東証一部出来高は1万株の桁から
  76. }
  77. private void GetIndexValues()
  78. {
  79. //foreach (var code in new[] { "N225", "TOPX", "MTHR", "JSD" })
  80. foreach (var code in new[] { "N225", "TOPX", "JSD" })
  81. {
  82. //var prices = code == "N225" ? _nikkei225 : code == "TOPX" ? _topix : code == "MTHR" ? _mothers : _jasdaq;
  83. var prices = code == "N225" ? _nikkei225 : code == "TOPX" ? _topix : _jasdaq;
  84. var date = DateTime.Parse(GetRssReturnString(code, "現在日付"));
  85. prices[date] = new NewDailyData
  86. {
  87. open = (int)(double.Parse(GetRssReturnString(code, "始値")) * 100),
  88. high = (int)(double.Parse(GetRssReturnString(code, "高値")) * 100),
  89. low = (int)(double.Parse(GetRssReturnString(code, "安値")) * 100),
  90. close = (int)(double.Parse(GetRssReturnString(code, "現在値")) * 100),
  91. volume = _marketVolume[date]
  92. };
  93. }
  94. }
  95. private void InsertIndices(DateTime date, Dictionary<int, NewDailyData> result)
  96. {
  97. result[(int)BuiltInIndex.Nikkei225] = _nikkei225[date];
  98. result[(int)BuiltInIndex.TOPIX] = _topix[date];
  99. //result[(int)BuiltInIndex.MOTHERS] = _mothers[date];
  100. result[(int)BuiltInIndex.JASDAQ] = _jasdaq[date];
  101. }
  102. private class BrandComparer : IComparer
  103. {
  104. public int Compare(object x, object y)
  105. {
  106. return ((AbstractBrand)x).Code - ((AbstractBrand)y).Code;
  107. }
  108. }
  109. private Dictionary<int, NewDailyData> FillData(int date)
  110. {
  111. var result = new Dictionary<int, NewDailyData>();
  112. var d2 = Util.IntToDate(date);
  113. InsertIndices(d2, result);
  114. ArrayList ar = new ArrayList(Env.BrandCollection.Values);
  115. ar.Sort(new BrandComparer());
  116. foreach (AbstractBrand br in ar)
  117. {
  118. var basic = br as BasicBrand;
  119. if (basic == null || basic.Market == MarketType.B || basic.Market == MarketType.Custom || basic.Obsolete == true)
  120. continue;
  121. var cnt = 0;
  122. var op = string.Empty;
  123. var hi = string.Empty;
  124. var lo = string.Empty;
  125. var cl = string.Empty;
  126. var vo = string.Empty;
  127. while (!set_value(br.Code, ref op, ref hi, ref lo, ref cl, ref vo))
  128. {
  129. cnt++;
  130. if (cnt >= MAXRETRYCOUNT)
  131. break;
  132. }
  133. //存在しなかった銘柄
  134. if (op == null || hi == null || lo == null || cl == null || vo == null)
  135. continue;
  136. //出来高がなかった銘柄
  137. else if (op.Trim() == string.Empty || hi.Trim() == string.Empty || lo.Trim() == string.Empty || cl.Trim() == string.Empty || vo.Trim() == "0")
  138. continue;
  139. const double shift = 10; // 株価は10倍で記録
  140. try
  141. {
  142. result[br.Code] = new NewDailyData
  143. {
  144. open = int.Parse((double.Parse(op) * shift).ToString()),
  145. high = int.Parse((double.Parse(hi) * shift).ToString()),
  146. low = int.Parse((double.Parse(lo) * shift).ToString()),
  147. close = int.Parse((double.Parse(cl) * shift).ToString()),
  148. volume = int.Parse((double.Parse(vo)).ToString())
  149. };
  150. }
  151. catch (Exception ex)
  152. {
  153. Console.WriteLine(ex.Message);
  154. }
  155. SendMessage(AsyncConst.WM_ASYNCPROCESS, br.Code, AsyncConst.LPARAM_PROGRESS_SUCCESSFUL);
  156. }
  157. return result;
  158. }
  159. private bool set_value(object code, ref string op, ref string hi, ref string lo, ref string cl, ref string vo)
  160. {
  161. op = GetRssReturnString(code + ".T", "始値");
  162. hi = GetRssReturnString(code + ".T", "高値");
  163. lo = GetRssReturnString(code + ".T", "安値");
  164. cl = GetRssReturnString(code + ".T", "現在値");
  165. vo = GetRssReturnString(code + ".T", "出来高");
  166. //存在しない銘柄コードを入れるとnullが返る
  167. if (op == null || hi == null || lo == null || cl == null || vo == null)
  168. {
  169. System.Threading.Thread.Sleep(1000);
  170. return false;
  171. }
  172. else
  173. return true;
  174. }
  175. private string GetRssReturnString(object code, string syubetu)
  176. {
  177. byte[] data = null;
  178. string ret = null;
  179. NDde.Client.DdeClient client = null;
  180. try
  181. {
  182. client = new DdeClient("RSS", string.Format("{0}", code));
  183. client.Connect();
  184. data = client.Request(syubetu, 1, 1000);
  185. ret = Encoding.Default.GetString(data).Replace("\0", "");
  186. }
  187. catch (Exception ex)
  188. {
  189. Console.WriteLine(ex.Message);
  190. }
  191. finally
  192. {
  193. if (client != null) client.Dispose();
  194. }
  195. return ret;
  196. }
  197. private bool ChkRss(int date)
  198. {
  199. NDde.Client.DdeClient client = null;
  200. bool ret = true;
  201. try
  202. {
  203. client = new DdeClient("RSS", "N225");
  204. client.Connect();
  205. byte[] data = client.Request("現在日付", 1, 1000);
  206. }
  207. catch (Exception ex)
  208. {
  209. Console.WriteLine(ex.Message);
  210. ret = false;
  211. }
  212. finally
  213. {
  214. if (client != null) client.Dispose();
  215. }
  216. return ret;
  217. }
  218. private bool IsSupportedIndex(int code)
  219. {
  220. return code == (int)BuiltInIndex.Nikkei225 ||
  221. code == (int)BuiltInIndex.TOPIX ||
  222. //code == (int)BuiltInIndex.MOTHERS ||
  223. code == (int)BuiltInIndex.JASDAQ;
  224. }
  225. }
  226. }
Download Printable view

URL of this paste

Embed with JavaScript

Embed with iframe

Raw text