MainWindow.xaml.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. 
  2. using GalaSoft.MvvmLight.Messaging;
  3. using IndexFormula.Finance.DataProvider;
  4. using IndexFormula.Finance.Win;
  5. using Muchinfo.Quote;
  6. using Muchinfo.Quote.HistoryClient;
  7. using Muchinfo.Quote.SqliteDal;
  8. using MuchInfo.Chart.App.Quote;
  9. using MuchInfo.Chart.App.ViewModels;
  10. using MuchInfo.Chart.Data.EnumTypes;
  11. using MuchInfo.Chart.Data.Interfaces;
  12. using MuchInfo.Chart.Data.Models;
  13. using MuchInfo.Chart.DataAccess;
  14. using MuchInfo.Chart.WPF.Primitives;
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Configuration;
  18. using System.IO;
  19. using System.Linq;
  20. using System.Threading;
  21. using System.Windows;
  22. using System.Windows.Input;
  23. using System.Windows.Threading;
  24. namespace MuchInfo.Chart.App
  25. {
  26. /// <summary>
  27. /// MainWindow.xaml 的交互逻辑
  28. /// </summary>
  29. public partial class MainWindow : Window
  30. {
  31. private QuoteComponentProxy _quoteComponentProxy;
  32. #region Constructors
  33. public MainWindow()
  34. {
  35. InitializeComponent();
  36. var isfake = ConfigurationManager.AppSettings["IsFakeData"];
  37. if (isfake.ToLower() == "true")
  38. {
  39. //另外一种更新数据集方法:通过注册周期改变事件而不须要拖动赋值图表的DataSet
  40. //this.NewChart.LoadHistoryCycleDataByTimeSpan += new LoadHistoryCycleDataByTimeSpanEventHandler(OnLoadGoodsData);
  41. this.NewChart.LoadHistoryCycleDataByCycleType += OnLoadGoodsData;
  42. Messenger.Default.Send<string>(string.Empty, "SetCurrentGoods");
  43. tempdataPoints = LoadSVC();
  44. _timerSharingTimer = new DispatcherTimer();
  45. _timerSharingTimer.Interval = new TimeSpan(0, 0, 0, 0,500);
  46. _timerSharingTimer.Tick -= _timerSharingTimer_Tick;
  47. _timerSharingTimer.Tick += _timerSharingTimer_Tick;
  48. _timerSharingTimer.Start();
  49. }
  50. else
  51. {
  52. Messenger.Default.Send<string>(string.Empty, "SetCurrentGoods");
  53. var uri = ConfigurationManager.AppSettings["Quote_Address"];
  54. _quoteComponentProxy = new QuoteComponentProxy(false, uri);
  55. _quoteComponentProxy.ResponseHistoryCycle += HistoryClient_ResponseHistoryCycle;
  56. this.NewChart.LoadHistoryCycleDataByCycleType += OnLoadHistoryCycleDataByCycleType;
  57. }
  58. }
  59. private void HistoryClient_ResponseHistoryCycle(object sender, HistoryCycleResponseEventArgs e)
  60. {
  61. var list = e.HisCycle.hisList;
  62. var result = (list == null || !list.Any()) ? new List<ILineDataPoint>() : list.ToArray().ToDataPointList();
  63. this.Dispatcher.BeginInvoke(new Action(() =>
  64. {
  65. if (this.NewChart.CycleType == CycleType.TimeSharing && e.HisCycle.tSpan == TimeSpan.FromMinutes(1))
  66. {
  67. //更新当前商品集合
  68. this.NewChart.UpdateTimeSharingDataPoints(result);
  69. }
  70. else if (this.NewChart.CycleType == e.HisCycle.cycle.ToCycleType() ||
  71. (e.HisCycle.cycle == Cycle.Custom && this.NewChart.CurrentTimeFrame == e.HisCycle.tSpan))
  72. {
  73. if (this.NewChart.CurrentGoods != null)
  74. {
  75. if (this.NewChart.CurrentGoods.Symbol == e.HisCycle.Symbol)
  76. {
  77. //更新当前商品集合
  78. this.NewChart.UpdateDataPoints(result);
  79. }
  80. else
  81. {
  82. //更新叠加商品集合
  83. this.NewChart.UpdateComparisonDataPoints(e.HisCycle.Symbol, result);
  84. }
  85. }
  86. }
  87. }));
  88. }
  89. /// <summary>
  90. /// 测试数据。
  91. /// </summary>
  92. /// <param name="source"></param>
  93. public List<ILineDataPoint> TestData(List<ILineDataPoint> source)
  94. {
  95. var outputSource = new List<ILineDataPoint>();
  96. Random random = new Random(DateTime.Now.Millisecond);
  97. if (source == null) return outputSource;
  98. int i = 5;
  99. foreach (var lineDataPoint in source)
  100. {
  101. var bar = lineDataPoint as BarDataPoint;
  102. var temdata = random.Next(-1, 1);
  103. if (bar != null)
  104. {
  105. bar.Low += temdata;
  106. bar.High += temdata;
  107. }
  108. outputSource.Add(bar);
  109. }
  110. return outputSource;
  111. }
  112. /// <summary>
  113. /// Called when [load goods data].
  114. /// </summary>
  115. /// <param name="exchangeCode">The exchange code.</param>
  116. /// <param name="goodsCode">The goods code.</param>
  117. /// <param name="type">The type.</param>
  118. /// <param name="startTime">The start time.</param>
  119. /// <param name="endTime">The end time.</param>
  120. /// <param name="totalCount">The total count.</param>
  121. /// <returns>ChartDataSet.</returns>
  122. private List<ILineDataPoint> OnLoadHistoryCycleDataByCycleType(int exchangeCode, string goodsCode, CycleType type, TimeSpan timeSpan, DateTime startTime, DateTime endTime, int totalCount)
  123. {
  124. RetMessageCode messageCode;
  125. var newType = type.ToCycle();
  126. var goods = new GoodsInfo(exchangeCode, goodsCode, GoodsType.PreciousMetal, 25, null, 1);
  127. HistoryCycle[] list;
  128. if (newType == Cycle.Custom)
  129. {
  130. list = _quoteComponentProxy.GetHistoryCycle(exchangeCode, goodsCode, timeSpan, startTime, endTime, (short)totalCount, out messageCode);
  131. }
  132. else
  133. {
  134. list = _quoteComponentProxy.GetHistoryCycle(goods.Symbol, type.ToCycle(), startTime, endTime, (short)totalCount, out messageCode);
  135. }
  136. return list.ToDataPointList();
  137. }
  138. void _timerSharingTimer_Tick(object sender, EventArgs e)
  139. {
  140. if (_currentIndex > 0)
  141. {
  142. if (NewChart.CycleType == CycleType.TimeSharing)
  143. {
  144. var barPoint = tempdataPoints[_currentIndex];
  145. var count = tempdataPoints.Count - _currentIndex;
  146. _currentIndex--;
  147. barPoint.Date = DateTime.Now.Date.AddMinutes((int)DateTime.Now.TimeOfDay.TotalMinutes);
  148. if (barPoint is IBarDataPoint)
  149. {
  150. //(barPoint as IBarDataPoint).Volume = _random.Next(0, 60);
  151. }
  152. NewChart.UpdateTimeShareData(barPoint as IBarDataPoint, NewChart.CurrentGoods.GoodsCode);
  153. if (NewChart.ComparisonGoodsList != null && NewChart.ComparisonGoodsList.Any())
  154. {
  155. var compisonPoint = new BarDataPoint(barPoint.Date, barPoint.Value);
  156. compisonPoint.Value += _random.Next(-2, 2);
  157. foreach (var goods in NewChart.ComparisonGoodsList)
  158. {
  159. NewChart.UpdateTimeShareData(compisonPoint as IBarDataPoint, goods.GoodsCode);
  160. }
  161. }
  162. }
  163. else
  164. {
  165. var bar = tempdataPoints.Last();
  166. var randomValue = _random.Next( 95, 105);
  167. var lastValue = randomValue*bar.Value/100;
  168. var date = DateTime.Now.Date.AddHours(23).AddMinutes(50 + tmpMinutes);
  169. var barDataPoint = new BarDataPoint(date, lastValue);
  170. var dt = this.DataContext as MainViewModel;
  171. dt.TikBarDataPoint = barDataPoint;
  172. tmpMinutes++;
  173. }
  174. }
  175. }
  176. private int tmpMinutes = 0;
  177. private DispatcherTimer _timerSharingTimer;
  178. private int _currentIndex = 0;
  179. private Random _random = new Random(200);
  180. #endregion Constructors
  181. #region Methods
  182. #region Private Methods
  183. /// <summary>
  184. /// Gets the data set.
  185. /// </summary>
  186. /// <param name="stockName">Name of the stock.</param>
  187. /// <param name="type">The type.</param>
  188. /// <returns>ChartDataSet.</returns>
  189. private ChartDataSet GetDataSet(string stockName, CycleType type)
  190. {
  191. var dataAccess = new FileDataAccess();
  192. //var a = dataAccess.GetDataPoints(stockName, type, DateTime.MinValue, DateTime.Now, 10000);
  193. var a = dataAccess.GetDataPoints(stockName, type, DateTime.MinValue, DateTime.Now, 1000);
  194. if (a == null) return null;
  195. var result = a.Cast<ILineDataPoint>().ToList();
  196. var dataSet = new ChartDataSet(result);
  197. return dataSet;
  198. }
  199. /// <summary>
  200. /// Gets the cycle data.
  201. /// </summary>
  202. /// <param name="stockName">Name of the stock.</param>
  203. /// <param name="timeSpan">The time span.</param>
  204. /// <param name="startTime">The start time.</param>
  205. /// <param name="endTime">The end time.</param>
  206. /// <param name="totalCount">The total count.</param>
  207. /// <returns>IList{ILineDataPoint}.</returns>
  208. private List<ILineDataPoint> GetCycleData(string stockName, CycleType cycle, TimeSpan timeSpan, DateTime startTime, DateTime endTime, int totalCount)
  209. {
  210. //var dataAccess = new FileDataAccess();
  211. //var a = dataAccess.GetDataPoints(stockName, timeSpan, startTime, endTime, totalCount);
  212. ////if (a == null) return null;
  213. ////var result = a.Cast<ILineDataPoint>().ToList();
  214. ////return result;
  215. ////return LoadSVC();
  216. // _timerSharingTimer.Start();
  217. if (cycle == CycleType.Minute) //CycleType.TimeSharing)
  218. {
  219. // _timerSharingTimer.Start();
  220. // return CreateTestPoint();
  221. }
  222. else
  223. {
  224. // _timerSharingTimer.Stop();
  225. return LoadSVC();
  226. // return LoadSVC(timeSpan);
  227. }
  228. return CreateTestPoint(stockName, endTime, totalCount);
  229. //return new List<ILineDataPoint>();
  230. }
  231. private List<ILineDataPoint> LoadSVC()
  232. {
  233. string fileName = Environment.CurrentDirectory + "\\data\\MSFT.CSV";
  234. var currentDataManager = new YahooCSVDataManager(Path.GetDirectoryName(fileName), Path.GetExtension(fileName));
  235. var provider = currentDataManager.LoadYahooCSV(fileName);
  236. var timeSpan = DateTime.Now - DateTime.FromOADate(provider["DATE"][provider.Count - 1]);
  237. var dataPoints = new List<ILineDataPoint>();
  238. for (int i = 0; i < provider.Count - 1; i++)
  239. {
  240. var dataPoint = new BarDataPoint(
  241. DateTime.FromOADate(provider["DATE"][i]).AddDays(timeSpan.Days),
  242. (float)provider["OPEN"][i],
  243. (float)provider["HIGH"][i],
  244. (float)provider["LOW"][i],
  245. (float)provider["CLOSE"][i],
  246. (float)provider["VOLUME"][i], 0, 0
  247. );
  248. dataPoints.Add(dataPoint);
  249. }
  250. dataPoints = dataPoints.Where((item) => item.Date > DateTime.Now.Date.AddDays(-200)).ToList();
  251. return dataPoints;
  252. }
  253. private List<ILineDataPoint> LoadSVC(TimeSpan timeSpan)
  254. {
  255. string fileName = Environment.CurrentDirectory + "\\data\\MSFT.CSV";
  256. var currentDataManager = new YahooCSVDataManager(Path.GetDirectoryName(fileName), Path.GetExtension(fileName));
  257. var mintues = (int)timeSpan.TotalMinutes;
  258. var provider = currentDataManager.LoadYahooCSV(fileName);
  259. var dataPoints = new List<ILineDataPoint>();
  260. int index = provider.Count - 1;
  261. var now = new DateTime(2014, 06, 04, 12, 02, 30, 250);
  262. for (int i = 0; i < provider.Count - 1; i += mintues)
  263. {
  264. index -= mintues;
  265. var dataPoint = new BarDataPoint(
  266. now.AddMinutes(-index),
  267. (float)provider["OPEN"][i],
  268. (float)provider["HIGH"][i],
  269. (float)provider["LOW"][i],
  270. (float)provider["CLOSE"][i],
  271. (float)provider["VOLUME"][i], 0, 0
  272. );
  273. dataPoints.Add(dataPoint);
  274. }
  275. return dataPoints;
  276. }
  277. private List<ILineDataPoint> tempdataPoints;
  278. /// <summary>
  279. /// 创建测试数据
  280. /// </summary>
  281. /// <returns></returns>
  282. private List<ILineDataPoint> CreateTestPoint(string symbol, DateTime endTime, int totalCount)
  283. {
  284. var result = new List<ILineDataPoint>();
  285. var date = DateTime.Today.AddHours(8);
  286. var ramdon = new Random(DateTime.Now.Millisecond);
  287. if (tempdataPoints != null)
  288. {
  289. var index = tempdataPoints.Count - 1;
  290. _currentIndex = tempdataPoints.Count - 1;
  291. result = tempdataPoints.Where(z => z.Date <= endTime).OrderByDescending(z => z.Date).Take(totalCount).ToList();
  292. }
  293. return result;
  294. }
  295. /// <summary>
  296. /// Gets the tik data.
  297. /// </summary>
  298. /// <param name="stockName">Name of the stock.</param>
  299. /// <param name="startTime">The start time.</param>
  300. /// <param name="endTime">The end time.</param>
  301. /// <param name="totalCount">The total count.</param>
  302. /// <returns>IList{ILineDataPoint}.</returns>
  303. private List<ILineDataPoint> GetTikData(string stockName, DateTime startTime, DateTime endTime, int totalCount)
  304. {
  305. var dataAccess = new FileDataAccess();
  306. var a = dataAccess.GetDataPoints(stockName, new TimeSpan(0, 1, 0), startTime, endTime, totalCount);
  307. if (a == null) return null;
  308. var result = a.Cast<ILineDataPoint>().ToList();
  309. return result;
  310. }
  311. /// <summary>
  312. /// Called when [load goods data].
  313. /// </summary>
  314. /// <param name="exchangeCode">The exchange code.</param>
  315. /// <param name="goodsCode">The goods code.</param>
  316. /// <param name="timeSpan">The time span.</param>
  317. /// <param name="startTime">The start time.</param>
  318. /// <param name="endTime">The end time.</param>
  319. /// <param name="totalCount">The total count.</param>
  320. /// <returns>ChartDataSet.</returns>
  321. private List<ILineDataPoint> OnLoadGoodsData(int exchangeCode, string goodsCode, CycleType cycle, TimeSpan timeSpan, DateTime startTime, DateTime endTime, int totalCount)
  322. {
  323. var result = GetCycleData(exchangeCode + goodsCode, cycle, timeSpan, startTime, endTime, totalCount);
  324. return result;
  325. }
  326. /// <summary>
  327. /// Called when [load history tik price data].
  328. /// </summary>
  329. /// <param name="exchangeCode">The exchange code.</param>
  330. /// <param name="goodsCode">The goods code.</param>
  331. /// <param name="startTime">The start time.</param>
  332. /// <param name="endTime">The end time.</param>
  333. /// <param name="totalCount">The total count.</param>
  334. /// <returns>IList{ILineDataPoint}.</returns>
  335. private List<ILineDataPoint> OnLoadHistoryTikPriceData(int exchangeCode, string goodsCode, DateTime startTime, DateTime endTime, int totalCount)
  336. {
  337. var result = GetTikData(exchangeCode + goodsCode, startTime, endTime, totalCount);
  338. return result;
  339. }
  340. #endregion Private Methods
  341. #endregion Methods
  342. private void BtnOpen_OnClick(object sender, RoutedEventArgs e)
  343. {
  344. Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(SetLanguage.languageName);
  345. FormulaSourceEditor.Open("", "");
  346. }
  347. }
  348. }