| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403 |
-
- using GalaSoft.MvvmLight.Messaging;
- using IndexFormula.Finance.DataProvider;
- using IndexFormula.Finance.Win;
- using Muchinfo.Quote;
- using Muchinfo.Quote.HistoryClient;
- using Muchinfo.Quote.SqliteDal;
- using MuchInfo.Chart.App.Quote;
- using MuchInfo.Chart.App.ViewModels;
- using MuchInfo.Chart.Data.EnumTypes;
- using MuchInfo.Chart.Data.Interfaces;
- using MuchInfo.Chart.Data.Models;
- using MuchInfo.Chart.DataAccess;
- using MuchInfo.Chart.WPF.Primitives;
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.IO;
- using System.Linq;
- using System.Threading;
- using System.Windows;
- using System.Windows.Input;
- using System.Windows.Threading;
- namespace MuchInfo.Chart.App
- {
- /// <summary>
- /// MainWindow.xaml 的交互逻辑
- /// </summary>
- public partial class MainWindow : Window
- {
- private QuoteComponentProxy _quoteComponentProxy;
- #region Constructors
- public MainWindow()
- {
- InitializeComponent();
- var isfake = ConfigurationManager.AppSettings["IsFakeData"];
- if (isfake.ToLower() == "true")
- {
- //另外一种更新数据集方法:通过注册周期改变事件而不须要拖动赋值图表的DataSet
- //this.NewChart.LoadHistoryCycleDataByTimeSpan += new LoadHistoryCycleDataByTimeSpanEventHandler(OnLoadGoodsData);
- this.NewChart.LoadHistoryCycleDataByCycleType += OnLoadGoodsData;
- Messenger.Default.Send<string>(string.Empty, "SetCurrentGoods");
- tempdataPoints = LoadSVC();
- _timerSharingTimer = new DispatcherTimer();
- _timerSharingTimer.Interval = new TimeSpan(0, 0, 0, 0,500);
- _timerSharingTimer.Tick -= _timerSharingTimer_Tick;
- _timerSharingTimer.Tick += _timerSharingTimer_Tick;
- _timerSharingTimer.Start();
- }
- else
- {
- Messenger.Default.Send<string>(string.Empty, "SetCurrentGoods");
- var uri = ConfigurationManager.AppSettings["Quote_Address"];
- _quoteComponentProxy = new QuoteComponentProxy(false, uri);
- _quoteComponentProxy.ResponseHistoryCycle += HistoryClient_ResponseHistoryCycle;
- this.NewChart.LoadHistoryCycleDataByCycleType += OnLoadHistoryCycleDataByCycleType;
- }
- }
- private void HistoryClient_ResponseHistoryCycle(object sender, HistoryCycleResponseEventArgs e)
- {
- var list = e.HisCycle.hisList;
- var result = (list == null || !list.Any()) ? new List<ILineDataPoint>() : list.ToArray().ToDataPointList();
- this.Dispatcher.BeginInvoke(new Action(() =>
- {
- if (this.NewChart.CycleType == CycleType.TimeSharing && e.HisCycle.tSpan == TimeSpan.FromMinutes(1))
- {
- //更新当前商品集合
- this.NewChart.UpdateTimeSharingDataPoints(result);
- }
- else if (this.NewChart.CycleType == e.HisCycle.cycle.ToCycleType() ||
- (e.HisCycle.cycle == Cycle.Custom && this.NewChart.CurrentTimeFrame == e.HisCycle.tSpan))
- {
- if (this.NewChart.CurrentGoods != null)
- {
- if (this.NewChart.CurrentGoods.Symbol == e.HisCycle.Symbol)
- {
- //更新当前商品集合
- this.NewChart.UpdateDataPoints(result);
- }
- else
- {
- //更新叠加商品集合
- this.NewChart.UpdateComparisonDataPoints(e.HisCycle.Symbol, result);
- }
- }
- }
- }));
- }
- /// <summary>
- /// 测试数据。
- /// </summary>
- /// <param name="source"></param>
- public List<ILineDataPoint> TestData(List<ILineDataPoint> source)
- {
- var outputSource = new List<ILineDataPoint>();
- Random random = new Random(DateTime.Now.Millisecond);
- if (source == null) return outputSource;
- int i = 5;
- foreach (var lineDataPoint in source)
- {
- var bar = lineDataPoint as BarDataPoint;
- var temdata = random.Next(-1, 1);
- if (bar != null)
- {
- bar.Low += temdata;
- bar.High += temdata;
- }
- outputSource.Add(bar);
- }
- return outputSource;
- }
- /// <summary>
- /// Called when [load goods data].
- /// </summary>
- /// <param name="exchangeCode">The exchange code.</param>
- /// <param name="goodsCode">The goods code.</param>
- /// <param name="type">The type.</param>
- /// <param name="startTime">The start time.</param>
- /// <param name="endTime">The end time.</param>
- /// <param name="totalCount">The total count.</param>
- /// <returns>ChartDataSet.</returns>
- private List<ILineDataPoint> OnLoadHistoryCycleDataByCycleType(int exchangeCode, string goodsCode, CycleType type, TimeSpan timeSpan, DateTime startTime, DateTime endTime, int totalCount)
- {
- RetMessageCode messageCode;
- var newType = type.ToCycle();
- var goods = new GoodsInfo(exchangeCode, goodsCode, GoodsType.PreciousMetal, 25, null, 1);
- HistoryCycle[] list;
- if (newType == Cycle.Custom)
- {
- list = _quoteComponentProxy.GetHistoryCycle(exchangeCode, goodsCode, timeSpan, startTime, endTime, (short)totalCount, out messageCode);
- }
- else
- {
- list = _quoteComponentProxy.GetHistoryCycle(goods.Symbol, type.ToCycle(), startTime, endTime, (short)totalCount, out messageCode);
- }
- return list.ToDataPointList();
- }
- void _timerSharingTimer_Tick(object sender, EventArgs e)
- {
- if (_currentIndex > 0)
- {
-
- if (NewChart.CycleType == CycleType.TimeSharing)
- {
- var barPoint = tempdataPoints[_currentIndex];
- var count = tempdataPoints.Count - _currentIndex;
- _currentIndex--;
- barPoint.Date = DateTime.Now.Date.AddMinutes((int)DateTime.Now.TimeOfDay.TotalMinutes);
- if (barPoint is IBarDataPoint)
- {
- //(barPoint as IBarDataPoint).Volume = _random.Next(0, 60);
- }
- NewChart.UpdateTimeShareData(barPoint as IBarDataPoint, NewChart.CurrentGoods.GoodsCode);
- if (NewChart.ComparisonGoodsList != null && NewChart.ComparisonGoodsList.Any())
- {
- var compisonPoint = new BarDataPoint(barPoint.Date, barPoint.Value);
- compisonPoint.Value += _random.Next(-2, 2);
- foreach (var goods in NewChart.ComparisonGoodsList)
- {
- NewChart.UpdateTimeShareData(compisonPoint as IBarDataPoint, goods.GoodsCode);
- }
- }
- }
- else
- {
-
- var bar = tempdataPoints.Last();
- var randomValue = _random.Next( 95, 105);
- var lastValue = randomValue*bar.Value/100;
- var date = DateTime.Now.Date.AddHours(23).AddMinutes(50 + tmpMinutes);
- var barDataPoint = new BarDataPoint(date, lastValue);
- var dt = this.DataContext as MainViewModel;
- dt.TikBarDataPoint = barDataPoint;
- tmpMinutes++;
- }
- }
- }
- private int tmpMinutes = 0;
- private DispatcherTimer _timerSharingTimer;
- private int _currentIndex = 0;
- private Random _random = new Random(200);
- #endregion Constructors
- #region Methods
- #region Private Methods
- /// <summary>
- /// Gets the data set.
- /// </summary>
- /// <param name="stockName">Name of the stock.</param>
- /// <param name="type">The type.</param>
- /// <returns>ChartDataSet.</returns>
- private ChartDataSet GetDataSet(string stockName, CycleType type)
- {
- var dataAccess = new FileDataAccess();
- //var a = dataAccess.GetDataPoints(stockName, type, DateTime.MinValue, DateTime.Now, 10000);
- var a = dataAccess.GetDataPoints(stockName, type, DateTime.MinValue, DateTime.Now, 1000);
- if (a == null) return null;
- var result = a.Cast<ILineDataPoint>().ToList();
- var dataSet = new ChartDataSet(result);
- return dataSet;
- }
- /// <summary>
- /// Gets the cycle data.
- /// </summary>
- /// <param name="stockName">Name of the stock.</param>
- /// <param name="timeSpan">The time span.</param>
- /// <param name="startTime">The start time.</param>
- /// <param name="endTime">The end time.</param>
- /// <param name="totalCount">The total count.</param>
- /// <returns>IList{ILineDataPoint}.</returns>
- private List<ILineDataPoint> GetCycleData(string stockName, CycleType cycle, TimeSpan timeSpan, DateTime startTime, DateTime endTime, int totalCount)
- {
- //var dataAccess = new FileDataAccess();
- //var a = dataAccess.GetDataPoints(stockName, timeSpan, startTime, endTime, totalCount);
- ////if (a == null) return null;
- ////var result = a.Cast<ILineDataPoint>().ToList();
- ////return result;
- ////return LoadSVC();
- // _timerSharingTimer.Start();
- if (cycle == CycleType.Minute) //CycleType.TimeSharing)
- {
- // _timerSharingTimer.Start();
- // return CreateTestPoint();
- }
- else
- {
- // _timerSharingTimer.Stop();
- return LoadSVC();
- // return LoadSVC(timeSpan);
- }
- return CreateTestPoint(stockName, endTime, totalCount);
- //return new List<ILineDataPoint>();
- }
- private List<ILineDataPoint> LoadSVC()
- {
- string fileName = Environment.CurrentDirectory + "\\data\\MSFT.CSV";
- var currentDataManager = new YahooCSVDataManager(Path.GetDirectoryName(fileName), Path.GetExtension(fileName));
- var provider = currentDataManager.LoadYahooCSV(fileName);
- var timeSpan = DateTime.Now - DateTime.FromOADate(provider["DATE"][provider.Count - 1]);
- var dataPoints = new List<ILineDataPoint>();
- for (int i = 0; i < provider.Count - 1; i++)
- {
- var dataPoint = new BarDataPoint(
- DateTime.FromOADate(provider["DATE"][i]).AddDays(timeSpan.Days),
- (float)provider["OPEN"][i],
- (float)provider["HIGH"][i],
- (float)provider["LOW"][i],
- (float)provider["CLOSE"][i],
- (float)provider["VOLUME"][i], 0, 0
- );
- dataPoints.Add(dataPoint);
- }
- dataPoints = dataPoints.Where((item) => item.Date > DateTime.Now.Date.AddDays(-200)).ToList();
- return dataPoints;
- }
- private List<ILineDataPoint> LoadSVC(TimeSpan timeSpan)
- {
- string fileName = Environment.CurrentDirectory + "\\data\\MSFT.CSV";
- var currentDataManager = new YahooCSVDataManager(Path.GetDirectoryName(fileName), Path.GetExtension(fileName));
- var mintues = (int)timeSpan.TotalMinutes;
- var provider = currentDataManager.LoadYahooCSV(fileName);
- var dataPoints = new List<ILineDataPoint>();
- int index = provider.Count - 1;
- var now = new DateTime(2014, 06, 04, 12, 02, 30, 250);
- for (int i = 0; i < provider.Count - 1; i += mintues)
- {
- index -= mintues;
- var dataPoint = new BarDataPoint(
- now.AddMinutes(-index),
- (float)provider["OPEN"][i],
- (float)provider["HIGH"][i],
- (float)provider["LOW"][i],
- (float)provider["CLOSE"][i],
- (float)provider["VOLUME"][i], 0, 0
- );
- dataPoints.Add(dataPoint);
- }
- return dataPoints;
- }
- private List<ILineDataPoint> tempdataPoints;
- /// <summary>
- /// 创建测试数据
- /// </summary>
- /// <returns></returns>
- private List<ILineDataPoint> CreateTestPoint(string symbol, DateTime endTime, int totalCount)
- {
- var result = new List<ILineDataPoint>();
- var date = DateTime.Today.AddHours(8);
- var ramdon = new Random(DateTime.Now.Millisecond);
- if (tempdataPoints != null)
- {
- var index = tempdataPoints.Count - 1;
- _currentIndex = tempdataPoints.Count - 1;
- result = tempdataPoints.Where(z => z.Date <= endTime).OrderByDescending(z => z.Date).Take(totalCount).ToList();
- }
- return result;
- }
- /// <summary>
- /// Gets the tik data.
- /// </summary>
- /// <param name="stockName">Name of the stock.</param>
- /// <param name="startTime">The start time.</param>
- /// <param name="endTime">The end time.</param>
- /// <param name="totalCount">The total count.</param>
- /// <returns>IList{ILineDataPoint}.</returns>
- private List<ILineDataPoint> GetTikData(string stockName, DateTime startTime, DateTime endTime, int totalCount)
- {
- var dataAccess = new FileDataAccess();
- var a = dataAccess.GetDataPoints(stockName, new TimeSpan(0, 1, 0), startTime, endTime, totalCount);
- if (a == null) return null;
- var result = a.Cast<ILineDataPoint>().ToList();
- return result;
- }
- /// <summary>
- /// Called when [load goods data].
- /// </summary>
- /// <param name="exchangeCode">The exchange code.</param>
- /// <param name="goodsCode">The goods code.</param>
- /// <param name="timeSpan">The time span.</param>
- /// <param name="startTime">The start time.</param>
- /// <param name="endTime">The end time.</param>
- /// <param name="totalCount">The total count.</param>
- /// <returns>ChartDataSet.</returns>
- private List<ILineDataPoint> OnLoadGoodsData(int exchangeCode, string goodsCode, CycleType cycle, TimeSpan timeSpan, DateTime startTime, DateTime endTime, int totalCount)
- {
- var result = GetCycleData(exchangeCode + goodsCode, cycle, timeSpan, startTime, endTime, totalCount);
- return result;
- }
- /// <summary>
- /// Called when [load history tik price data].
- /// </summary>
- /// <param name="exchangeCode">The exchange code.</param>
- /// <param name="goodsCode">The goods code.</param>
- /// <param name="startTime">The start time.</param>
- /// <param name="endTime">The end time.</param>
- /// <param name="totalCount">The total count.</param>
- /// <returns>IList{ILineDataPoint}.</returns>
- private List<ILineDataPoint> OnLoadHistoryTikPriceData(int exchangeCode, string goodsCode, DateTime startTime, DateTime endTime, int totalCount)
- {
- var result = GetTikData(exchangeCode + goodsCode, startTime, endTime, totalCount);
- return result;
- }
- #endregion Private Methods
- #endregion Methods
- private void BtnOpen_OnClick(object sender, RoutedEventArgs e)
- {
- Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(SetLanguage.languageName);
- FormulaSourceEditor.Open("", "");
- }
- }
- }
|