Pastebin: 最後のスクリーニング結果 SystemTrading.cs

Formato
Plain text
Post date
2015-04-04 22:29
Publication Period
Unlimited
  1. /*
  2. * Copyright (c) Daisuke OKAJIMA All rights reserved.
  3. *
  4. * $Id$
  5. */
  6. using System;
  7. using System.Threading;
  8. using System.Collections;
  9. using System.Diagnostics;
  10. using Travis.Util;
  11. using Zanetti.Data;
  12. using Zanetti.Config;
  13. //スクリーニングとセオリー検証の共通機能をまとめる
  14. namespace Zanetti.SystemTrading
  15. {
  16. //銘柄列挙関係
  17. [EnumDesc(typeof(FilterType))]
  18. internal enum FilterType {
  19. [EnumValue(Description="すべての銘柄")] None,
  20. [EnumValue(Description="日経平均採用銘柄")] Nikkei225,
  21. [EnumValue(Description="平均売買代金上位500銘柄")] Active500,
  22. [EnumValue(Description="東証・大証1部")] Div1,
  23. [EnumValue(Description="東証・大証1部以外")] NotDiv1,
  24. [EnumValue(Description="お気に入りのフォルダ")] Favorite,
  25. [EnumValue(Description = "最後のスクリーニング結果")] LastResult
  26. }
  27. [EnumDesc(typeof(TradingType))]
  28. internal enum TradingType {
  29. [EnumValue(Description="買い")] Long,
  30. [EnumValue(Description="売り")] Short
  31. }
  32. [EnumDesc(typeof(EntryType))]
  33. internal enum EntryType {
  34. [EnumValue(Description="当日終値")] TodayClose,
  35. [EnumValue(Description="翌日始値")] TomorrowOpen,
  36. [EnumValue(Description="逆指値")] Gyakusashine
  37. }
  38. internal abstract class BrandEnumerator {
  39. //プログレスバーのコントロールがあるので、Countで返した値だけNextを呼び、実際に処理対象にするのはCheckに通ったやつのみというルールでいく
  40. public abstract AbstractBrand Next();
  41. public abstract bool Check(AbstractBrand br);
  42. public abstract int Count {
  43. get;
  44. }
  45. public abstract string Description {
  46. get;
  47. }
  48. }
  49. internal class AllBrandEnumerator : BrandEnumerator {
  50. private IDictionaryEnumerator _e;
  51. public AllBrandEnumerator() {
  52. _e = Env.BrandCollection.GetEnumerator();
  53. }
  54. public override AbstractBrand Next() {
  55. return _e.MoveNext()? (AbstractBrand)_e.Value : null;
  56. }
  57. public override int Count {
  58. get {
  59. return Env.BrandCollection.TotalCount;
  60. }
  61. }
  62. public override bool Check(AbstractBrand br) {
  63. return br is BasicBrand && !br.IsBuiltIn;
  64. }
  65. public override string Description {
  66. get {
  67. return "全銘柄";
  68. }
  69. }
  70. }
  71. internal class BookmarkedBrandEnumerator2 : BrandEnumerator
  72. {
  73. private ArrayList _brands;
  74. private int _index;
  75. public BookmarkedBrandEnumerator2()
  76. {
  77. _brands = new ArrayList();
  78. _index = 0;
  79. }
  80. public override AbstractBrand Next()
  81. {
  82. AbstractBrand r = null;
  83. try
  84. {
  85. if (Env.LastScreeningResult.ResultCount <= _index) return null;
  86. object t = Env.LastScreeningResult.GetAt(_index++);
  87. Screening.ScreeningResultEntry ent = t as Screening.ScreeningResultEntry;
  88. r = ent.Brand;
  89. }
  90. catch (Exception ex)
  91. {
  92. Console.WriteLine(ex.Message);
  93. }
  94. return r;
  95. }
  96. public override int Count
  97. {
  98. get
  99. {
  100. return Env.LastScreeningResult.ResultCount;
  101. }
  102. }
  103. public override bool Check(AbstractBrand br)
  104. {
  105. return true;
  106. }
  107. public override string Description
  108. {
  109. get
  110. {
  111. return "ブックマーク ";
  112. }
  113. }
  114. }
  115. internal class FilteredBrandEnumerator : AllBrandEnumerator {
  116. private FilterType _filter;
  117. public FilteredBrandEnumerator(FilterType t) {
  118. _filter = t;
  119. }
  120. public FilterType FilterType {
  121. get {
  122. return _filter;
  123. }
  124. }
  125. public override bool Check(AbstractBrand br0) {
  126. if(!base.Check(br0)) return false;
  127. BasicBrand br = br0 as BasicBrand;
  128. switch(_filter) {
  129. case FilterType.Div1:
  130. return br.Market==MarketType.T1 || br.Market==MarketType.O1;
  131. case FilterType.NotDiv1:
  132. return br.Market!=MarketType.T1 && br.Market!=MarketType.O1;
  133. case FilterType.Nikkei225:
  134. return br.Nikkei225;
  135. case FilterType.Active500:
  136. return br.Active500;
  137. default:
  138. return br.Market!=MarketType.B; //指数は当然含めない
  139. }
  140. }
  141. public override string Description {
  142. get {
  143. return EnumDescAttribute.For(typeof(FilterType)).GetDescription(_filter);
  144. }
  145. }
  146. }
  147. internal class SingleBrandEnumerator : BrandEnumerator {
  148. private AbstractBrand _br;
  149. private bool _finished;
  150. public SingleBrandEnumerator(AbstractBrand br) {
  151. _br = br;
  152. }
  153. public override AbstractBrand Next() {
  154. if(_finished)
  155. return null;
  156. else {
  157. _finished = true;
  158. return _br;
  159. }
  160. }
  161. public override int Count {
  162. get {
  163. return 1;
  164. }
  165. }
  166. public override bool Check(AbstractBrand br) {
  167. return true;
  168. }
  169. public override string Description {
  170. get {
  171. return _br.Name;
  172. }
  173. }
  174. }
  175. internal class BookmarkedBrandEnumerator : BrandEnumerator {
  176. private BookmarkFolder _bookmarkFolder;
  177. private ArrayList _brands;
  178. private int _index;
  179. public BookmarkedBrandEnumerator(BookmarkFolder f) {
  180. _bookmarkFolder = f;
  181. _brands = new ArrayList();
  182. _bookmarkFolder.GatherTo(_brands, GatherOption.Brand);
  183. _index = 0;
  184. }
  185. public override AbstractBrand Next() {
  186. AbstractBrand r = null;
  187. while(r==null && _index<_brands.Count) {
  188. object t = _brands[_index++];
  189. r = t as AbstractBrand;
  190. }
  191. return r;
  192. }
  193. public override int Count {
  194. get {
  195. return _brands.Count;
  196. }
  197. }
  198. public override bool Check(AbstractBrand br) {
  199. return true;
  200. }
  201. public override string Description {
  202. get {
  203. return "ブックマーク " + _bookmarkFolder.Name;
  204. }
  205. }
  206. }
  207. //横断して何かをする基底クラス
  208. internal abstract class SystemTradingExecutor {
  209. protected string _errorMessage;
  210. protected string _name;
  211. protected BrandEnumerator _brandEnumerator;
  212. protected IntPtr _notifyTarget;
  213. protected Thread _thread;
  214. protected SystemTradingExecutor(string name) {
  215. _name = name;
  216. }
  217. public BrandEnumerator BrandEnumerator {
  218. get {
  219. return _brandEnumerator;
  220. }
  221. set {
  222. _brandEnumerator = value;
  223. }
  224. }
  225. public string Name {
  226. get {
  227. return _name;
  228. }
  229. }
  230. public string ErrorMessage {
  231. get {
  232. return _errorMessage;
  233. }
  234. }
  235. public abstract SystemTradingResult Result {
  236. get;
  237. }
  238. public void Execute() {
  239. //初期化
  240. BeforeExecute();
  241. this.Result.BrandDescription = _brandEnumerator.Description;
  242. AbstractBrand br = _brandEnumerator.Next();
  243. while(br!=null) {
  244. try {
  245. if(_brandEnumerator.Check(br)) {
  246. ExecuteBrandResult t = ExecuteBrand(br);
  247. this.Result.CheckedBrandCount++; //!!これはここでインクリメントするが、エラーの情報は派生クラス側というのがわかりづらい仕様だ
  248. if(t==ExecuteBrandResult.TooManyResult) return;
  249. }
  250. if(_notifyTarget!=IntPtr.Zero)
  251. Win32.SendMessage(_notifyTarget, AsyncConst.WM_ASYNCPROCESS, new IntPtr(br.Code), new IntPtr(AsyncConst.LPARAM_PROGRESS_SUCCESSFUL));
  252. br = _brandEnumerator.Next();
  253. }
  254. catch(TradeDataOverflowException ) {
  255. Debug.WriteLine("Overflow in screening " + br.Code);
  256. this.Result.AddDataErrorBrand(br);
  257. }
  258. catch(Exception ex) {
  259. Debug.WriteLine(ex.StackTrace);
  260. _errorMessage = ex.Message;
  261. if(_notifyTarget!=IntPtr.Zero)
  262. Win32.SendMessage(_notifyTarget, AsyncConst.WM_ASYNCPROCESS, IntPtr.Zero, new IntPtr(AsyncConst.LPARAM_ERROR));
  263. }
  264. }
  265. AfterExecute();
  266. }
  267. //実行のサポート
  268. protected virtual void BeforeExecute() { }
  269. protected virtual void AfterExecute() { }
  270. protected abstract ExecuteBrandResult ExecuteBrand(AbstractBrand br);
  271. //非同期実行
  272. public void AsyncExecute(IntPtr target) {
  273. _notifyTarget = target;
  274. _thread = new Thread(new ThreadStart(Run));
  275. _thread.Start();
  276. }
  277. private void Run() {
  278. try {
  279. Execute();
  280. if(_notifyTarget!=IntPtr.Zero) Win32.SendMessage(_notifyTarget, AsyncConst.WM_ASYNCPROCESS, IntPtr.Zero, new IntPtr(AsyncConst.LPARAM_FINISHED));
  281. }
  282. catch(Exception ex) {
  283. _errorMessage = ex.Message;
  284. Util.SilentReportCriticalError(ex);
  285. if(_notifyTarget!=IntPtr.Zero) Win32.SendMessage(_notifyTarget, AsyncConst.WM_ASYNCPROCESS, IntPtr.Zero, new IntPtr(AsyncConst.LPARAM_ERROR));
  286. }
  287. }
  288. public void Abort() {
  289. if(_thread!=null)
  290. _thread.Abort();
  291. }
  292. }
  293. internal enum ExecuteBrandResult {
  294. Succeeded,
  295. Ignored,
  296. DataError,
  297. Filtered,
  298. TooManyResult
  299. }
  300. internal abstract class SystemTradingResult {
  301. protected ArrayList _data;
  302. protected string _brandDescription;
  303. protected bool _sortRequired;
  304. protected int _checkedCount;
  305. protected ArrayList _dataErrorBrands;
  306. protected bool _tooManyResults;
  307. public SystemTradingResult(int capacity) {
  308. _data = new ArrayList(capacity);
  309. _dataErrorBrands = new ArrayList();
  310. }
  311. public void AddDataErrorBrand(AbstractBrand br) {
  312. _dataErrorBrands.Add(br);
  313. }
  314. public SystemTradingResultEntry GetAt(int index) {
  315. if(_sortRequired) {
  316. _data.Sort();
  317. _sortRequired = false;
  318. }
  319. return (SystemTradingResultEntry)_data[index];
  320. }
  321. public string BrandDescription {
  322. get {
  323. return _brandDescription;
  324. }
  325. set {
  326. _brandDescription = value;
  327. }
  328. }
  329. public int CheckedBrandCount {
  330. get {
  331. return _checkedCount;
  332. }
  333. set {
  334. _checkedCount = value;
  335. }
  336. }
  337. public bool HasTooManuResults {
  338. get {
  339. return _tooManyResults;
  340. }
  341. set {
  342. _tooManyResults = value;
  343. }
  344. }
  345. public IList DataErrorBrands {
  346. get {
  347. return _dataErrorBrands;
  348. }
  349. }
  350. public int ResultCount {
  351. get {
  352. return _data.Count;
  353. }
  354. }
  355. }
  356. internal abstract class SystemTradingResultEntry : IComparable {
  357. protected AbstractBrand _brand;
  358. public SystemTradingResultEntry(AbstractBrand br) {
  359. _brand = br;
  360. }
  361. public AbstractBrand Brand {
  362. get {
  363. return _brand;
  364. }
  365. }
  366. public abstract int CompareTo(object obj);
  367. }
  368. }
Download Printable view

URL of this paste

Embed with JavaScript

Embed with iframe

Raw text