Pastebin: OmegaChart 楽天RSSから株価取得 最新日付のみ更新させるVer

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

URL of this paste

Embed with JavaScript

Embed with iframe

Raw text