CChartViewModel.cs 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215
  1. using FINANCEPRICELib;
  2. using GalaSoft.MvvmLight;
  3. using GalaSoft.MvvmLight.Command;
  4. using GalaSoft.MvvmLight.Ioc;
  5. using Muchinfo.MTPClient.Data;
  6. using Muchinfo.MTPClient.Data.Chart;
  7. using Muchinfo.MTPClient.Data.DTO;
  8. using Muchinfo.MTPClient.Data.Enums;
  9. using Muchinfo.MTPClient.Data.Model;
  10. using Muchinfo.MTPClient.Data.Quote;
  11. using Muchinfo.MTPClient.Infrastructure.Cache;
  12. using Muchinfo.MTPClient.Infrastructure.Helpers;
  13. using Muchinfo.MTPClient.Infrastructure.MessageBox;
  14. using Muchinfo.MTPClient.Infrastructure.Utilities;
  15. using Muchinfo.MTPClient.IService;
  16. using Muchinfo.PC.Common.Helpers;
  17. using System;
  18. using System.Collections.Generic;
  19. using System.IO;
  20. using System.Linq;
  21. using System.Runtime.InteropServices;
  22. using System.Text;
  23. //----------------------------------------------------------------
  24. //Module Name: $safeprojectname$
  25. //Purpose:
  26. //CopyRight: Muchinfo
  27. //History:
  28. //----------------------------------------------------------------
  29. //DateTime 2017/2/4 10:25:33
  30. //Author
  31. //Description Create
  32. //----------------------------------------------------------------
  33. using System.Windows.Input;
  34. using Muchinfo.MTPClient.Data.Model.Account;
  35. namespace Muchinfo.MTPClient.Analysis.ViewModels
  36. {
  37. public class CChartViewModel : ChartViewModelBase, IDisposable
  38. {
  39. private ICommonQueryService _commonQueryService;
  40. static CChartViewModel()
  41. {
  42. Init();
  43. }
  44. public static void Init()
  45. {
  46. //关于图表管理静态类初始化问题 以后考虑放到外面同意初始化
  47. //Exception ex=null;
  48. string SysteFile = System.IO.Path.Combine(UserManager.SysConfigFolderPath, "SystemFile.dat");
  49. string UserFile = System.IO.Path.Combine(UserManager.SysConfigFolderPath, "UserFile.dat");
  50. try
  51. {
  52. FinanceGlobal = new GlobalAttribClass();
  53. FinanceGlobal.SetDllType((int)DLLEDITIONTYPE.DllEditionType_MUCHINFO_MTP);
  54. FinanceGlobal.SetUseVirtualVolume(1);
  55. FinanceGlobal.SetUserFileName(UserFile);
  56. FinanceGlobal.SetSystemFileName(SysteFile);
  57. //FinanceChart.FilePath(UserIniFile);
  58. // FinanceChart.FilePath(UserIniFile);
  59. //STDMETHOD(SetSystemFileName)(/*[in]*/ BSTR strFileName);
  60. //STDMETHOD(SetUserFileName)(/*[in]*/ BSTR strFileName);
  61. #if !Futury
  62. FinanceGlobal.SetHistoryEdition((int)HistoryEdition.HistoryEdition_Muchinfo_MTP_WITH_SETTLE);
  63. #else
  64. FinanceGlobal.SetHistoryEdition((int)HistoryEdition.HistoryEdition_Muchinfo_MTP_WITH_SETTLE); //3
  65. #endif
  66. FinanceGlobal.SetSystemDateTime(ApplicationParameter.ServerTimeNow.ToString("yyyyMMddHHmmss"));
  67. FinanceGlobal.SetUseSingleViewConfig(1);
  68. }
  69. catch (COMException ex)
  70. {
  71. MessageBoxHelper.ShowQuestion(string.Format(Resources.Client_Resource.ChartRegisterError,
  72. Path.Combine(ChartPathDir, c_chartRegitFile)), Resources.Client_Resource.APP_Tips);
  73. LogHelper.WriteError(typeof(CChartViewModel), ex.ToString());
  74. }
  75. }
  76. private static GlobalAttribClass FinanceGlobal; ////图表配置
  77. private PriceChartClass _financeChart; ////图表对象
  78. private int _controlHandle;
  79. private CycleType _cycleType;
  80. private const string c_chartRegitFile = "regchart.bat"; ///注册图表控件的文件路径
  81. private ChartCycleType _chartCycleType = ChartCycleType.MinuteTik;
  82. ///图表路径目录
  83. public static string ChartPathDir
  84. {
  85. get
  86. {
  87. return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "COM");
  88. }
  89. }
  90. public CChartViewModel(QuoteGoods goods, int handle):base(goods)
  91. {
  92. try
  93. {
  94. if (_commonQueryService == null)
  95. {
  96. _commonQueryService = SimpleIoc.Default.GetInstance<ICommonQueryService>();
  97. /*废弃,不需要这个逻辑了
  98. if (CacheManager.QuoteSourceGroupMarketDatas == null ||
  99. CacheManager.QuoteSourceGroupMarketDatas.Count == 0)
  100. {
  101. _commonQueryService.QuoteSourceGroupMarketDate((list =>
  102. {
  103. }), entity =>
  104. {
  105. });
  106. }*/
  107. }
  108. RegiterMsg();
  109. _controlHandle = handle;
  110. string UserIniFile = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "FinancePrice.ini");
  111. _financeChart = new PriceChartClass();
  112. _financeChart.OnMenuMessage += new _IPriceChartEvents_OnMenuMessageEventHandler(Chart_OnMenuMessage);
  113. _financeChart.RequestHistoryPrice +=
  114. new _IPriceChartEvents_RequestHistoryPriceEventHandler(RequestHistoryPrice);
  115. PriceViewModel = new QuotePriceViewModel();
  116. _financeChart.FilePath = UserIniFile;
  117. _quoteDataService = SimpleIoc.Default.GetInstance<IQuoteDataService>();
  118. _currentGoods = goods;
  119. CommandTypes = SimpleIoc.Default.GetInstance<ISystemService>().GetIndicator();
  120. QueryGoodsReckonPlan(goods);
  121. InitSetChartColorStyle();
  122. ChartCycle =(int) ChartCycleType.MinuteTik;
  123. SetIndecatorEnable(0);
  124. }
  125. catch (Exception ex)
  126. {
  127. LogHelper.WriteError(typeof(CChartViewModel), ex.ToString());
  128. }
  129. }
  130. /// <summary>
  131. /// 图表请求数据
  132. /// </summary>
  133. /// <param name="lExchangeCode">交易所代码</param>
  134. /// <param name="strWareCode">商品代码</param>
  135. /// <param name="strStartDate">开始时间</param>
  136. /// <param name="strEndDate">结束时间</param>
  137. /// <param name="lPriceNum">请求数量</param>
  138. /// <param name="lDataCycle">周期</param>
  139. private void RequestHistoryPrice(int lExchangeCode, string strWareCode, string strStartDate, string strEndDate,
  140. int lPriceNum, int lDataCycle)
  141. {
  142. // Console.WriteLine("请求:" + lDataCycle);
  143. //var dataCycle = lDataCycle;
  144. //格式化开始时间
  145. string strStart = FormatSeverTime(strStartDate);
  146. //格式化结束时间
  147. string strEnd = FormatSeverTime(strEndDate);
  148. DateTime starTime; //取当前时间之前的
  149. if (string.IsNullOrWhiteSpace(strStart) || !DateTime.TryParse(strStart, out starTime))
  150. {
  151. starTime = ApplicationParameter.ServerTimeNow;
  152. }
  153. DateTime endTime;
  154. if (string.IsNullOrWhiteSpace(strEnd) || !DateTime.TryParse(strEnd, out endTime))
  155. {
  156. endTime = ApplicationParameter.BasicDateTime;
  157. }
  158. short count = (short)lPriceNum;
  159. _quoteDataService.GetHistoryCycleData(_currentGoods, (CycleType)lDataCycle, starTime, endTime, count,
  160. QueryHistorySuccess,
  161. ErrorSuccess);
  162. SetIndecatorEnable(0);
  163. }
  164. /// <summary>
  165. /// 历史数据回应
  166. /// </summary>
  167. /// <param name="data"></param>
  168. private void QueryHistorySuccess(byte[] data)
  169. {
  170. if (data == null || data.Length == 0)
  171. {
  172. LogHelper.WriteInfo("历史回应数据为空");
  173. }
  174. if (_financeChart != null && data != null)
  175. {
  176. _financeChart.ResponseHistoryPrice(data, data.Length);
  177. }
  178. }
  179. private List<SysMenuItem> _commandTypes;
  180. /// <summary>
  181. /// 底部菜单列表
  182. /// </summary>
  183. public List<SysMenuItem> CommandTypes
  184. {
  185. get { return _commandTypes; }
  186. set { Set(() => CommandTypes, ref _commandTypes, value); }
  187. }
  188. private SysMenuItem _currentIndexMenu;
  189. /// <summary>
  190. /// 当前选择的指标
  191. /// </summary>
  192. public SysMenuItem CurrentIndexMenu
  193. {
  194. get { return _currentIndexMenu; }
  195. set
  196. {
  197. Set(() => CurrentIndexMenu, ref _currentIndexMenu, value);
  198. SetChartIndecator(value);
  199. }
  200. }
  201. /// <summary>
  202. /// 设置图表指标
  203. /// </summary>
  204. /// <param name="menuItem"></param>
  205. private void SetChartIndecator(SysMenuItem menuItem)
  206. {
  207. if (_financeChart != null)
  208. {
  209. string index1 = string.Empty, index2 = string.Empty;
  210. _financeChart.GetFrameMessage2(_controlHandle, (int)UserDefineWindowsMessage.SetAddIndex, ref index1, ref index2);
  211. var hasIndex = string.IsNullOrWhiteSpace(index1);
  212. INDEX_TYPE indextype = INDEX_TYPE.INDEX_TYPE_MA;
  213. switch (menuItem.SysCommandType)
  214. {
  215. case MenuCommandType.MA:
  216. indextype = INDEX_TYPE.INDEX_TYPE_MA;
  217. if (!hasIndex && index1.Contains("MA"))
  218. {
  219. return; ///已存在MA
  220. }
  221. break;
  222. case MenuCommandType.BOLL:
  223. indextype = INDEX_TYPE.INDEX_TYPE_BOLL;
  224. if (!hasIndex && index1.Contains("BOLL"))
  225. {
  226. return; ///已存在BOLL
  227. }
  228. break;
  229. case MenuCommandType.MACD:
  230. indextype = INDEX_TYPE.INDEX_TYPE_MACD;
  231. if (!hasIndex && index1.Contains("MACD"))
  232. {
  233. return; ///已存在
  234. }
  235. break;
  236. case MenuCommandType.KDJ:
  237. indextype = INDEX_TYPE.INDEX_TYPE_KDJ;
  238. if (!hasIndex && index1.Contains("KDJ"))
  239. {
  240. return; ///已存在
  241. }
  242. break;
  243. case MenuCommandType.RSI:
  244. indextype = INDEX_TYPE.INDEX_TYPE_RSI;
  245. if (!hasIndex && index1.Contains("RSI"))
  246. {
  247. return; ///已存在
  248. }
  249. break;
  250. case MenuCommandType.PSY:
  251. indextype = INDEX_TYPE.INDEX_TYPE_PSY;
  252. if (!hasIndex && index1.Contains("PSY"))
  253. {
  254. return; ///已存在
  255. }
  256. break;
  257. case MenuCommandType.DMI:
  258. indextype = INDEX_TYPE.INDEX_TYPE_DMI;
  259. if (!hasIndex && index1.Contains("DMI"))
  260. {
  261. return; ///已存在
  262. }
  263. break;
  264. case MenuCommandType.VOL:
  265. indextype = INDEX_TYPE.INDEX_TYPE_VOLUME;
  266. if (!hasIndex && index1.Contains("VOL"))
  267. {
  268. return; ///已存在
  269. }
  270. break;
  271. default:
  272. break;
  273. }
  274. if (!hasIndex)
  275. {
  276. index1 = index1.Trim(';');
  277. var indexArray= index1.Split(';');
  278. if (indexArray.Any())
  279. {
  280. var indicator = indexArray[indexArray.Length - 1];
  281. var formulaArr = indicator.Split(',');
  282. if (formulaArr.Any())
  283. {
  284. bool isExist = false;
  285. var index= ChartIndecator(formulaArr[0],ref isExist);
  286. if (isExist)
  287. {
  288. _financeChart.PostFrameMessage(_controlHandle,
  289. (int) UserDefineWindowsMessage.SetDeleteIndex,
  290. (int) index, 0);
  291. }
  292. }
  293. }
  294. }
  295. _financeChart.PostFrameMessage(_controlHandle, (int) UserDefineWindowsMessage.SetAddIndex,
  296. (int) indextype, 0);
  297. }
  298. }
  299. private INDEX_TYPE ChartIndecator(string formula,ref bool isExist)
  300. {
  301. isExist = true;
  302. var indextype = INDEX_TYPE.INDEX_TYPE_VOLUME;
  303. switch (formula)
  304. {
  305. case "MA":
  306. indextype = INDEX_TYPE.INDEX_TYPE_MA;
  307. break;
  308. case "BOLL":
  309. indextype = INDEX_TYPE.INDEX_TYPE_BOLL;
  310. break;
  311. case "MACD":
  312. indextype = INDEX_TYPE.INDEX_TYPE_MACD;
  313. break;
  314. case "KDJ":
  315. indextype = INDEX_TYPE.INDEX_TYPE_KDJ;
  316. break;
  317. case "RSI":
  318. indextype = INDEX_TYPE.INDEX_TYPE_RSI;
  319. break;
  320. case "PSY":
  321. indextype = INDEX_TYPE.INDEX_TYPE_PSY;
  322. break;
  323. case "DMI":
  324. indextype = INDEX_TYPE.INDEX_TYPE_DMI;
  325. break;
  326. case "VOL":
  327. indextype = INDEX_TYPE.INDEX_TYPE_VOLUME;
  328. break;
  329. default:
  330. isExist = false;
  331. break;
  332. }
  333. return indextype;
  334. }
  335. private void ErrorSuccess(ErrorEntity errorEntity)
  336. {
  337. LogHelper.WriteError(typeof(CChartViewModel), errorEntity.ReturnCode.ToString());
  338. }
  339. /// <summary>
  340. /// 设置图表的样式
  341. /// </summary>
  342. private void InitSetChartColorStyle()
  343. {
  344. var colorStyle = UserManager.GetCurrentColorStyle();
  345. switch (colorStyle)
  346. {
  347. case ColorStyle.BlueWhite:
  348. FinanceGlobal.SetDefaultColorType((int)KLine_ColorType.KLine_COLOR_LCB_Red_RGB);
  349. break;
  350. case ColorStyle.DarkBlue:
  351. case ColorStyle.CoolBlack:
  352. FinanceGlobal.SetDefaultColorType((int)KLine_ColorType.KLine_COLOR_TDX_Red_Black);
  353. break;
  354. default:
  355. break;
  356. }
  357. }
  358. /// <summary>
  359. /// 图表周期
  360. /// </summary>
  361. public int ChartCycle { get; set; }
  362. /// <summary>
  363. /// 格式化服务器时间
  364. /// </summary>
  365. /// <param name="clienttime">客户端时间</param>
  366. /// <returns></returns>
  367. public string FormatSeverTime(string clienttime)
  368. {
  369. if (!string.IsNullOrEmpty(clienttime))
  370. {
  371. StringBuilder sb = new StringBuilder();
  372. sb.Append(clienttime.Substring(0, 4));
  373. if (clienttime.Length > 5)
  374. {
  375. sb.Append("-");
  376. sb.Append(clienttime.Substring(4, 2));
  377. }
  378. if (clienttime.Length > 7)
  379. {
  380. sb.Append("-");
  381. sb.Append(clienttime.Substring(6, 2));
  382. }
  383. if (clienttime.Length > 9)
  384. {
  385. sb.Append(" ");
  386. sb.Append(clienttime.Substring(8, 2));
  387. }
  388. if (clienttime.Length > 11)
  389. {
  390. sb.Append(":");
  391. sb.Append(clienttime.Substring(10, 2));
  392. }
  393. if (clienttime.Length > 13)
  394. {
  395. sb.Append(":");
  396. sb.Append(clienttime.Substring(12, 2));
  397. }
  398. return sb.ToString();
  399. }
  400. return string.Empty;
  401. }
  402. #region "转换商品类型"
  403. /// <summary>
  404. /// 商品类型转换
  405. /// </summary>
  406. /// <param name="goods"></param>
  407. /// <returns></returns>
  408. private WareAttributeClass ConverWare(QuoteGoods goods)
  409. {
  410. WareAttributeClass war = new WareAttributeClass();
  411. war.Name = goods.Name;
  412. war.Ware = goods.GoodsCode;
  413. war.Type = "0";
  414. war.HqKind = "1";
  415. war.Rate = (double)1;
  416. war.SellPoint = (double)0;
  417. war.PriceDigit = 7;
  418. war.PriceType = "1";
  419. war.HqWare = goods.GoodsCode;
  420. war.Logo = goods.MarketCode;
  421. war.HqDecimal = Math.Pow(10, -goods.DecimalPlaces); //GetWareTailLength(goods.DecimalPlaces.ToString());
  422. war.ShowDecimal = Math.Pow(10, -goods.DecimalPlaces); //GetWareTailLength(goods.DecimalPlaces.ToString());
  423. int exchange = 0;
  424. int.TryParse(goods.ExchHqCode, out exchange);
  425. war.HqExchangeCode = exchange;
  426. war.HqExchange = goods.ExchHqCode;
  427. //if (!string.IsNullOrEmpty(Mark))
  428. // war.TradeHour = Mark;
  429. //if (goods.Config != null)
  430. //{
  431. // //若历史开收盘时段不为空,则图表使用历史开收盘 by yxw 2014-12-01
  432. // if (!goods.Config.IsHistory || string.IsNullOrEmpty(goods.Config.HistoryMark))
  433. // war.TradeHour = goods.Config.Mark;
  434. // else
  435. // {
  436. // war.TradeHour = goods.Config.HistoryMark;
  437. // goods.Config.HistoryMark = string.Empty; //更新完后需释放,不影响下一次判定
  438. // }
  439. //}
  440. #if DEBUG
  441. if (CacheManager.TradeDayPlan.ContainsKey(goods.GoodsParameters.GoodsId))
  442. {
  443. var tradedates = CacheManager.TradeDayPlan[goods.GoodsParameters.GoodsId];
  444. war.TradeDate = tradedates.TradeDay.ToString("yyyyMMdd");
  445. war.TradeHour = ConvertTradeHour(tradedates.OpenCloseDates);
  446. }
  447. #else
  448. if (CacheManager.TradeDayPlan.ContainsKey(goods.GoodsParameters.GoodsId))
  449. {
  450. var tradedates = CacheManager.TradeDayPlan[goods.GoodsParameters.GoodsId];
  451. war.TradeDate = tradedates.TradeDay.ToString("yyyyMMdd");
  452. war.TradeHour = ConvertTradeHour(tradedates.OpenCloseDates);
  453. }
  454. #endif
  455. war.IsHasVolume = goods.TradeMode == eTradeMode.TRADEMODE_MARKETMAKE ? 0 : 1; ////是否是有量商品
  456. war.HighLimit = (double)goods.RaisesPrice;
  457. war.LowLimit = (double)goods.FallsPrice;
  458. //if (IsKLineMinuteTik)
  459. //{
  460. // if (goods.ExchangeCode == Exchange.Sge)
  461. // {
  462. // war.TradeDayCloseOffset = -1;
  463. // }
  464. // war.TradeDate = KLineMinuteDate;
  465. //}
  466. //else
  467. //{
  468. // if (goods.Config != null)
  469. // {
  470. // if (goods.ExchangeCode == Exchange.Sge && goods.Config.TradeDate >= ApplicationParamater.ServerTimeNow.Date)
  471. // {
  472. // war.TradeDayCloseOffset = -1;
  473. // }
  474. // war.TradeDate = goods.Config.TradeDate.ToString(FormatHelper.DateFormat2);
  475. // }
  476. //}
  477. //if (goods.ExchangeCode == Exchange.Sse || goods.ExchangeCode == Exchange.Szse)
  478. //{
  479. // war.HighLimit = (double)goods.Preclose * 1.1;
  480. // war.LowLimit = (double)goods.Preclose * 0.9;
  481. //}
  482. try
  483. {
  484. if (CacheManager.TradeDayPlan.ContainsKey(goods.GoodsParameters.GoodsId))
  485. {
  486. QuoteTradePlan tradedates = CacheManager.TradeDayPlan[goods.GoodsParameters.GoodsId];
  487. if (tradedates.OpenCloseDates.Count > 0)// 太白说一定会有index 0的值
  488. {
  489. OpenCloseDate index0 = tradedates.OpenCloseDates[0];
  490. war.TradeDayCloseOffset = index0.OpenDate.Day - tradedates.TradeDay.Day;// TradeDayCloseOffset -1表示向前偏移,1表示向后偏移。0不偏移
  491. }
  492. }
  493. }
  494. catch (Exception ex)
  495. {
  496. LogHelper.WriteError(typeof(UserManager), "处理TradeDayCloseOffset偏移量问题失败(CChartViewModel.cs 563行左右):" + ex.ToString());
  497. }
  498. return war;
  499. }
  500. /// <summary>
  501. /// 转换开收市时间
  502. /// </summary>
  503. /// <param name="openCloseDates">开收市时间</param>
  504. /// <returns></returns>
  505. private string ConvertTradeHour(List<OpenCloseDate> openCloseDates)
  506. {
  507. if (openCloseDates == null || !openCloseDates.Any())
  508. {
  509. return string.Empty;
  510. }
  511. var dateList = new List<string>();
  512. foreach (var openCloseDate in openCloseDates)
  513. {
  514. dateList.Add(openCloseDate.OpenDate.ToString("HHmm"));
  515. dateList.Add(openCloseDate.CloseDate.ToString("HHmm"));
  516. }
  517. return string.Join(",", dateList);
  518. }
  519. #endregion
  520. /// <summary>
  521. /// 图表添加商品
  522. /// </summary>
  523. /// <param name="goods"></param>
  524. private void AddWare(QuoteGoods goods)
  525. {
  526. var war = ConverWare(goods);
  527. _financeChart.AddWare(war);
  528. _financeChart.SetWareName(war.Ware, war.Name);
  529. // SetWareFlag(war.HqExchange.ToString(), war.Ware.ToString(), false);
  530. CreateChartView(goods);
  531. }
  532. /// <summary>
  533. /// 更新商品
  534. /// </summary>
  535. /// <param name="goods"></param>
  536. public void UpdateWare(QuoteGoods goods)
  537. {
  538. _currentGoods = goods;
  539. // var ware = ConverWare(goods);
  540. // _financeChart.AddWare(ware);
  541. // _financeChart.SetWareName(ware.Ware, ware.Name);
  542. QueryGoodsReckonPlan(goods);
  543. PriceViewModel.UpdateGoods(goods);
  544. // CreateChartView(goods, true);
  545. }
  546. /// <summary>
  547. /// 切换商品
  548. /// </summary>
  549. /// <param name="isNext"> true:切换下一个商品,false:切换上一个商品</param>
  550. public void ChangeGoodsNextFront(bool isNext)
  551. {
  552. var goodsList = new List<QuoteGoods>();
  553. var currentLayout = UserManager.GetCurrentLayoutStyle();
  554. if (currentLayout == LayoutStyle.PersonalCenter)
  555. {
  556. goodsList =
  557. CacheManager.CacheGoodsBaseInfos.Where((item) => item.TradeMode == eTradeMode.TRADEMODE_BIDDING)
  558. .ToList();
  559. }
  560. else
  561. {
  562. goodsList =
  563. CacheManager.CacheGoodsBaseInfos.Where((item) => item.TradeMode != eTradeMode.TRADEMODE_SALE)
  564. .ToList();
  565. }
  566. if (goodsList.Any())
  567. {
  568. QuoteGoods nextGoods;
  569. var index = goodsList.FindIndex((goods) => goods.Symbol == _currentGoods.Symbol);
  570. if (isNext)
  571. {
  572. nextGoods = (index == -1 || index == goodsList.Count - 1) ? goodsList[0] : goodsList[index + 1];
  573. }
  574. else
  575. {
  576. nextGoods = (index == -1 || index == 0) ? goodsList[goodsList.Count - 1] : goodsList[index - 1];
  577. }
  578. if (nextGoods != null)
  579. {
  580. UpdateWare(nextGoods);
  581. var dto = new QuoteGoodsDTO(nextGoods);
  582. MessengerHelper.DefaultSend<QuoteGoodsDTO>(dto, MessengerTokens.SelectGoodsChange);
  583. }
  584. }
  585. }
  586. /// <summary>
  587. /// 选中某个单据触发的图表显示问题
  588. /// </summary>
  589. /// <param name="quoteGoods"></param>
  590. public void SelectGoodsOrder(QuoteGoodsForSelectOrder quoteGoods)
  591. {
  592. if (quoteGoods != null && _currentGoods.GoodsId != quoteGoods.QuoteGoods.GoodsId)// 如果是相同的商品,就不要再做任何事情了
  593. {
  594. UpdateWare(quoteGoods.QuoteGoods);
  595. var dto = new QuoteGoodsDTO(quoteGoods.QuoteGoods);
  596. MessengerHelper.DefaultSend<QuoteGoodsDTO>(dto, MessengerTokens.SelectGoodsChange);
  597. }
  598. }
  599. private void Chart_OnMenuMessage(int lMessageType, int lSubMessageType, int lExchangeCode, string strWare,
  600. string strParam)
  601. {
  602. switch ((UserDefineWindowsMessage) lMessageType)
  603. {
  604. #region 暂时不使用
  605. case UserDefineWindowsMessage.None:
  606. break;
  607. case UserDefineWindowsMessage.Start:
  608. break;
  609. case UserDefineWindowsMessage.ChangeStyleCycle:
  610. break;
  611. case UserDefineWindowsMessage.DrawLine:
  612. break;
  613. case UserDefineWindowsMessage.ChangeViewCycle:
  614. break;
  615. case UserDefineWindowsMessage.ChangeViewFQ:
  616. break;
  617. case UserDefineWindowsMessage.Set_ShowRect:
  618. break;
  619. case UserDefineWindowsMessage.SetEnableDraw:
  620. break;
  621. case UserDefineWindowsMessage.SetDeleteIndex:
  622. break;
  623. case UserDefineWindowsMessage.SetAddIndex:
  624. break;
  625. case UserDefineWindowsMessage.SetRemoveAllIndex:
  626. break;
  627. case UserDefineWindowsMessage.GetCurIndexCount:
  628. break;
  629. case UserDefineWindowsMessage.SetMouseEvent:
  630. break;
  631. case UserDefineWindowsMessage.SetTradeDate:
  632. break;
  633. case UserDefineWindowsMessage.SetPreclose:
  634. break;
  635. case UserDefineWindowsMessage.SetMouseButtonRightUp:
  636. break;
  637. case UserDefineWindowsMessage.SetAddWare:
  638. break;
  639. case UserDefineWindowsMessage.SetSacle:
  640. break;
  641. case UserDefineWindowsMessage.DelAppendWare:
  642. break;
  643. case UserDefineWindowsMessage.SetVolumeUnit:
  644. break;
  645. case UserDefineWindowsMessage.SetOpenPrice:
  646. break;
  647. case UserDefineWindowsMessage.SetAppendWareClosePrice:
  648. break;
  649. case UserDefineWindowsMessage.FormulaInfo:
  650. break;
  651. case UserDefineWindowsMessage.ShowFormula:
  652. break;
  653. case UserDefineWindowsMessage.TRADEHOURDELAY:
  654. break;
  655. case UserDefineWindowsMessage.OPENPRICETIME:
  656. break;
  657. case UserDefineWindowsMessage.SHOWMATIK:
  658. break;
  659. case UserDefineWindowsMessage.TIKVOLUMEUNIT:
  660. break;
  661. case UserDefineWindowsMessage.MeanType:
  662. break;
  663. case UserDefineWindowsMessage.ViewPY:
  664. break;
  665. case UserDefineWindowsMessage.DrawIsdisplay:
  666. break;
  667. case UserDefineWindowsMessage.SetAutoScroll:
  668. break;
  669. case UserDefineWindowsMessage.ClearInvestLog:
  670. break;
  671. case UserDefineWindowsMessage.SetAxisShow:
  672. break;
  673. case UserDefineWindowsMessage.ClearInvestLogByViewHandler:
  674. break;
  675. case UserDefineWindowsMessage.ChartDispose:
  676. break;
  677. case UserDefineWindowsMessage.CloseStatFrom:
  678. break;
  679. case UserDefineWindowsMessage.CreateTime:
  680. break;
  681. case UserDefineWindowsMessage.ClearCache:
  682. break;
  683. case UserDefineWindowsMessage.OpenOrder:
  684. break;
  685. case UserDefineWindowsMessage.OpenMine:
  686. break;
  687. case UserDefineWindowsMessage.OpenDrawTool:
  688. break;
  689. case UserDefineWindowsMessage.AddToCustomBoard:
  690. break;
  691. case UserDefineWindowsMessage.OpenChartHide:
  692. break;
  693. case UserDefineWindowsMessage.OpenMouseOut:
  694. break;
  695. case UserDefineWindowsMessage.OpenMouseIn:
  696. break;
  697. case UserDefineWindowsMessage.OpenCycle:
  698. ChartCycle = lSubMessageType;
  699. SetIndecatorEnable(0);
  700. break;
  701. case UserDefineWindowsMessage.OpenReceiveHistory:
  702. break;
  703. case UserDefineWindowsMessage.OpenRequestMine:
  704. break;
  705. case UserDefineWindowsMessage.OpenKLineNode:
  706. break;
  707. case UserDefineWindowsMessage.DeleteFromCustomBoard:
  708. break;
  709. case UserDefineWindowsMessage.OpenAlarmCondition:
  710. break;
  711. case UserDefineWindowsMessage.OpenMouseEvent:
  712. break;
  713. case UserDefineWindowsMessage.OpenClickExportData:
  714. break;
  715. case UserDefineWindowsMessage.UNKNOWN_WARE_INFO:
  716. break;
  717. #endregion
  718. case UserDefineWindowsMessage.View_ZOOM_IN:
  719. IsPriceBoard = false;
  720. break;
  721. case UserDefineWindowsMessage.View_ZOOM_OUT:
  722. var currentLayout = UserManager.GetCurrentLayoutStyle();
  723. if (currentLayout != LayoutStyle.PersonalCenter)
  724. {
  725. IsPriceBoard = true;
  726. }
  727. break;
  728. default:
  729. break;
  730. }
  731. }
  732. private bool _isPriceBoard = true;
  733. /// <summary>
  734. /// 是否显示报价牌
  735. /// </summary>
  736. public bool IsPriceBoard
  737. {
  738. get { return _isPriceBoard; }
  739. set { Set(() => IsPriceBoard, ref _isPriceBoard, value); }
  740. }
  741. private bool isUpdate = false;
  742. #region "创建图表"
  743. /// <summary>
  744. /// 创建图表
  745. /// </summary>
  746. /// <param name="quoteChart">控件对象</param>
  747. internal void CreateChartView(QuoteGoods goods )
  748. {
  749. int exchange = 0;
  750. int.TryParse(goods.ExchHqCode, out exchange);
  751. if (!isUpdate)
  752. {
  753. ////图表 参数2是分时图
  754. _financeChart.CreateChartView2(_controlHandle, goods.GoodsCode, (int)_chartCycleType, exchange);
  755. isUpdate = true;
  756. RegisterMessage();
  757. _financeChart.PostFrameMessage(_controlHandle, (int)UserDefineWindowsMessage.SetSacle,
  758. 60, 0); ////设置分时图刻度
  759. }
  760. else
  761. {
  762. _financeChart.UpdateWareOfView2(_controlHandle, goods.GoodsCode, exchange);
  763. }
  764. if (CacheManager.TradeDayPlan.ContainsKey(goods.GoodsParameters.GoodsId))
  765. {
  766. var tradedates = CacheManager.TradeDayPlan[goods.GoodsParameters.GoodsId];
  767. _financeChart.PostFrameMessage2(_controlHandle, (int) UserDefineWindowsMessage.SetTradeDate,
  768. tradedates.TradeDay.ToString("yyyyMMdd"), string.Empty); ////设置交易日
  769. }
  770. _financeChart.PostFrameMessage2(_controlHandle, (int)UserDefineWindowsMessage.SetPreclose,
  771. goods.LastCloseDisplay, string.Empty); ////设置交易日
  772. var lastCalc = goods.TradeMode == eTradeMode.TRADEMODE_BIDDING ? MA_TIK_CALC_MODE.MA_TIK_CALC_MODE_TURNOVER_VOLUME : MA_TIK_CALC_MODE.MA_TIK_CALC_MODE_PRECLOSE; ////是否是有量商品
  773. // var lastCalc = 2; ////因图表按卖一价来累K线,第一按无量商品计算均线,
  774. _financeChart.PostFrameMessage(_controlHandle, (int)UserDefineWindowsMessage.MeanType,
  775. (int)lastCalc, 0); ///设置均线计算方式
  776. _financeChart.PostFrameMessage2(_controlHandle, (int)UserDefineWindowsMessage.TIKVOLUMEUNIT,
  777. goods.GoodsParameters.AgreeUnit+string.Empty, string.Empty);
  778. // FinanceChart.FilePath
  779. //FinanceChart.CreateChartView3(quoteChart.ViewHanlde, quoteChart.Goods.GoodsCode.ToString(), (int)quoteChart.CycleType, exchange, 0);
  780. // FinanceChart.
  781. //鼠标中轴滚动
  782. //FinanceChart.PostFrameMessage(quoteChart.ViewHanlde, (int)FUN_SET_MOUSE_EVENT, WM_MOUSEWHEEL, 0);
  783. ////右键事件放开
  784. //FinanceChart.PostFrameMessage(quoteChart.ViewHanlde, (int)FUN_SET_MOUSE_EVENT, WM_RBUTTONUP, 0);
  785. //InitDataBaseInfrom(quoteChart);
  786. }
  787. #endregion
  788. private void RegisterMessage()
  789. {
  790. MessengerHelper.QuoteUnregister<List<QuoteGoods>>(this, MessengerTokens.ReceiveRealTikQuote);
  791. MessengerHelper.QuoteRegister<List<QuoteGoods>>(this, MessengerTokens.ReceiveRealTikQuote, (quoteList) =>
  792. {
  793. if (quoteList == null || !quoteList.Any()) return;
  794. foreach (var item in quoteList)
  795. {
  796. if (item == null) continue;
  797. QuoteTradePlan qp = CacheManager.TradeDayPlan[_currentGoods.GoodsId];
  798. if (qp != null)
  799. {
  800. if (item.TradeDate != null)
  801. {
  802. if (!qp.TradeDay.ToString("yyyyMMdd").Equals(item.TradeDate)) // 如果交易日不一样,就重新设置一下交易日
  803. {
  804. AddWare(_currentGoods);
  805. _financeChart.PostFrameMessage2(_controlHandle, (int)UserDefineWindowsMessage.SetTradeDate,
  806. item.TradeDate, string.Empty); ////设置交易日
  807. }
  808. }
  809. }
  810. if (_currentGoods.Symbol.ToLower() == item.Symbol.ToLower() && item.CurrentPrice != 0)
  811. {
  812. //交易所代码
  813. string exchangeCode = _currentGoods.ExchHqCode;
  814. //int divid = 1;
  815. //if (exchangeCode == Exchange.Sge)
  816. //{
  817. // divid = goods.LotSize;
  818. // if (goods.PriceUnit == "千克")
  819. // {
  820. // divid = divid * 1000 ;
  821. // }
  822. //}
  823. //商品代码
  824. string goodCode = _currentGoods.GoodsCode;
  825. //if (goods.HasVolume)
  826. //{
  827. // if (goods.Volume <= 0)
  828. // return;
  829. //}
  830. double iVolume = item.CurrentVolume; // *divid;
  831. //商品买入价
  832. double dbBid = (double)item.BidPrice;
  833. //商品卖价
  834. double dbAsk = (double)item.AskPrice;
  835. //行情最高价
  836. double dbHiPr = (double)item.High;
  837. //行情最高价
  838. double dbLowPr = (double)item.Low;
  839. //现价
  840. double dbPr = (double)item.CurrentPrice;
  841. //昨收
  842. double dbPrLast = (double)_currentGoods.LastClose;
  843. //今开
  844. double dbOpen = (double)item.Open;
  845. ////总额
  846. //double dbTrd = (double)goods.TotalTurnover;
  847. //现量
  848. //double dbVolume = (double)goods.Volume * divid;
  849. //现额
  850. double dbNowSum = (double)item.TradeTurnover;
  851. //持仓量
  852. double dbHolVolm = 0; // (double)item.Positions; --图表有成交量时,不支持持仓量
  853. //买量
  854. double dbBidVolume = (double)item.BidVolume; // *divid;
  855. //卖量
  856. double dbAskVolume = (double)item.AskVolume; // *divid;
  857. //当前时间
  858. string time = item.Date.ToString("yyyyMMddHHmmss");
  859. _financeChart.UpdateTickPrice3(goodCode, dbBid, dbAsk, dbHiPr, dbLowPr, dbPr, dbPrLast, dbOpen,
  860. iVolume, dbHolVolm, dbBidVolume, dbAskVolume, time, int.Parse(exchangeCode), dbNowSum);
  861. }
  862. }
  863. });
  864. MessengerHelper.DefaultRegister<string>(this, MessengerTokens.RegisterResourceChange, (e) =>
  865. {
  866. InitSetChartColorStyle();
  867. // RegisterResourceChange(e);
  868. });
  869. ////市场开市信号(清盘信号)
  870. MessengerHelper.DefaultRegister<string>(this, MessengerTokens.Market_OpenToken, (e) =>
  871. {
  872. MarketOpenChangeSettle();
  873. });
  874. }
  875. /// <summary>
  876. /// 注册消息
  877. /// </summary>
  878. private void RegiterMsg()
  879. {
  880. MessengerHelper.DefaultRegister<bool>(this, MessengerTokens.ChartGoodsChange, ChangeGoodsNextFront);
  881. MessengerHelper.DefaultRegister<QuoteGoodsForSelectOrder>(this, MessengerTokens.SelectGoodsOrderChange, SelectGoodsOrder);
  882. }
  883. private void MarketOpenChangeSettle()
  884. {
  885. CacheManager.TradeDayPlan.Clear();
  886. if (_currentGoods != null)
  887. {
  888. ChartInitTradePlan();
  889. }
  890. }
  891. /// <summary>
  892. ///
  893. /// </summary>
  894. /// <param name="tradeDays"></param>
  895. protected override void QueryReckenSuccess(List<QuoteTradePlan> tradeDays)
  896. {
  897. base.QueryReckenSuccess(tradeDays);
  898. AddWare(_currentGoods); ////添加商品
  899. //tradeDays = tradeDays.OrderBy((item) => item.OpenWeekDay).ToList();
  900. var goodsPlan = tradeDays.FirstOrDefault((item) => item.GoodsId == _currentGoods.GoodsId);
  901. if (goodsPlan == null)
  902. {
  903. goodsPlan = tradeDays.FirstOrDefault((item) => item.GoodsId == 0);
  904. }
  905. if (PriceViewModel != null)
  906. {
  907. PriceViewModel.TaskInitQuotePriceView(goodsPlan, _currentGoods);
  908. }
  909. }
  910. protected override void QueryTradeTimeSuccess(List<QuoteTradeTime> quoteTimes)
  911. {
  912. base.QueryTradeTimeSuccess(quoteTimes);
  913. AddWare(_currentGoods); ////添加商品
  914. //tradeDays = tradeDays.OrderBy((item) => item.OpenWeekDay).ToList();
  915. var tradeDays = SetDefautTradePlan();
  916. if (CacheManager.TradeDayPlan.ContainsKey(_currentGoods.GoodsParameters.GoodsId))
  917. {
  918. tradeDays = CacheManager.TradeDayPlan[_currentGoods.GoodsParameters.GoodsId];
  919. }
  920. if (PriceViewModel != null)
  921. {
  922. PriceViewModel.TaskInitQuotePriceView(tradeDays, _currentGoods);
  923. }
  924. }
  925. /// <summary>
  926. /// 查询结算时间失败
  927. /// </summary>
  928. /// <param name="errorEntity"></param>
  929. protected override void QueryReckenError(ErrorEntity errorEntity)
  930. {
  931. //todo:查询结算时间失败计算
  932. var tradeDays = SetDefautTradePlan();
  933. AddWare(_currentGoods); ////添加商品
  934. if (PriceViewModel != null)
  935. {
  936. PriceViewModel.TaskInitQuotePriceView(tradeDays, _currentGoods);
  937. }
  938. LogHelper.WriteError(typeof(CChartViewModel), string.Format(ErrorManager.FormatErrorMsg(errorEntity)));
  939. }
  940. /// <summary>
  941. /// 查询结算时间失败使用默认时间(使用8-22)
  942. /// </summary>
  943. private QuoteTradePlan SetDefautTradePlan()
  944. {
  945. //查询结算时间失败使用默认时间(使用8-22)
  946. var goodsOpenCloses = new QuoteTradePlan();
  947. goodsOpenCloses.ExchangeCode = this._currentGoods.ExchHqCode;
  948. var openClose = new OpenCloseDate()
  949. {
  950. CloseDate = ApplicationParameter.ServerTimeNow.Date.AddHours(22),
  951. OpenDate = ApplicationParameter.ServerTimeNow.Date.AddHours(8)
  952. };
  953. goodsOpenCloses.OpenCloseDates = new List<OpenCloseDate>() { openClose };
  954. return goodsOpenCloses;
  955. }
  956. private void QueryGoodsReckonPlan(QuoteGoods goods)
  957. {
  958. /*if (CacheManager.TradeDayPlan.ContainsKey(goods.GoodsParameters.GoodsId))
  959. {
  960. AddWare(goods); ////添加商品
  961. // SetChartControlCycle(_goodsOpenCloses);
  962. if (PriceViewModel != null)
  963. {
  964. PriceViewModel.TaskInitQuotePriceView(CacheManager.TradeDayPlan[goods.GoodsParameters.GoodsId], goods);
  965. }
  966. }
  967. else
  968. {*/
  969. ChartInitTradePlan();
  970. /*}*/
  971. }
  972. public void Dispose()
  973. {
  974. if (_financeChart != null)
  975. {
  976. _financeChart.Destory();
  977. }
  978. MessengerHelper.DefaultUnregister<List<QuoteGoods>>(this, MessengerTokens.ReceiveRealTikQuote);
  979. MessengerHelper.DefaultUnregister<bool>(this, MessengerTokens.ChartGoodsChange);
  980. this.Cleanup();
  981. }
  982. private QuotePriceViewModel _priceViewModel;
  983. /// <summary>
  984. /// 报价牌视图模型
  985. /// </summary>
  986. public QuotePriceViewModel PriceViewModel
  987. {
  988. get { return _priceViewModel; }
  989. set { Set(() => PriceViewModel, ref _priceViewModel, value); }
  990. }
  991. private ICommand _enterCommand;
  992. public RelayCommand<int> EnterCommand
  993. {
  994. get
  995. {
  996. return new RelayCommand<int>((keyV) =>
  997. {
  998. ////分时图与日线切换
  999. if (_financeChart != null)
  1000. {
  1001. var pressKey = (Key)keyV;
  1002. // Console.WriteLine("回车:" + _cycleType);
  1003. //_chartCycleType = _chartCycleType == ChartCycleType.MinuteTik ? ChartCycleType.Day : ChartCycleType.MinuteTik;
  1004. //_financeChart.PostFrameMessage(_controlHandle, (int)UserDefineWindowsMessage.ChangeViewCycle, (int)_chartCycleType, 0);
  1005. switch (pressKey)
  1006. {
  1007. case Key.Enter:
  1008. case Key.F5:
  1009. _chartCycleType = _chartCycleType == ChartCycleType.MinuteTik
  1010. ? ChartCycleType.Day
  1011. : ChartCycleType.MinuteTik;
  1012. _financeChart.PostFrameMessage(_controlHandle, (int)UserDefineWindowsMessage.ChangeViewCycle, (int)_chartCycleType, 0);
  1013. break;
  1014. case Key.PageDown:
  1015. ChangeGoodsNextFront(true);
  1016. break;
  1017. case Key.PageUp:
  1018. ChangeGoodsNextFront(false);
  1019. break;
  1020. }
  1021. }
  1022. }, SetIndecatorEnable);
  1023. }
  1024. }
  1025. /// <summary>
  1026. /// 指标是否可用
  1027. /// </summary>
  1028. public bool IsIndecatorEnable
  1029. {
  1030. get
  1031. {
  1032. return ChartCycle != (int)ChartCycleType.MinuteTik;
  1033. }
  1034. }
  1035. public bool SetIndecatorEnable(int paramter)
  1036. {
  1037. RaisePropertyChanged(() => IsIndecatorEnable);
  1038. return true;
  1039. }
  1040. //public ICommand EnterCommand
  1041. //{
  1042. // get
  1043. // {
  1044. // return new RelayCommand(() =>
  1045. // {
  1046. // ////分时图与日线切换
  1047. // if (_financeChart != null)
  1048. // {
  1049. // // Console.WriteLine("回车:" + _cycleType);
  1050. // _chartCycleType = _chartCycleType == ChartCycleType.MinuteTik ? ChartCycleType.Day : ChartCycleType.MinuteTik;
  1051. // _financeChart.PostFrameMessage(_controlHandle, (int)UserDefineWindowsMessage.ChangeViewCycle, (int)_chartCycleType, 0);
  1052. // }
  1053. // });
  1054. // }
  1055. //}
  1056. //public ICommand PageDownCommand
  1057. //{
  1058. // get
  1059. // {
  1060. // return new RelayCommand(() =>
  1061. // {
  1062. // ChangeGoodsNextFront(true);
  1063. // });
  1064. // }
  1065. //}
  1066. //public ICommand PageUpCommand
  1067. //{
  1068. // get
  1069. // {
  1070. // return new RelayCommand(() =>
  1071. // {
  1072. // ChangeGoodsNextFront(false);
  1073. // });
  1074. // }
  1075. //}
  1076. }
  1077. }