//private static Timer _timer2 = new Timer(CheckSubs, null, 1000, 1000); //private static void CheckSubs(object state) //{ // var brokenSubs = TickList.Where(x => x.Ticks.Count == 0).ToList(); // foreach (var sub in brokenSubs) // { // sub.Tradeno = 1; // } // TXmlConnector.ConnectorSendCommand(ReturnTickSubsXml()); // foreach (var sub in brokenSubs) // { // sub.Tradeno = 0; // } //} private static void ClearSubs(object state) { var windows = _dispatcher.Invoke(() => Application.Current.Windows); List <IAnchor> list = new List <IAnchor>(); foreach (Window window in windows) { var context = _dispatcher.Invoke(() => window.DataContext); if (context is IAnchor) { list.Add((IAnchor)context); } } TickSubscription[] arr = new TickSubscription[TickList.Count]; TickList.CopyTo(arr, 0); foreach (var tickSubscription in arr) { var sub = list.Count(c => c.Board == tickSubscription.Board && c.Seccode == tickSubscription.Seccode); if (sub == 0) { var subscription = tickSubscription; TickList.TryTake(out subscription); TXmlConnector.ConnectorSendCommand(ReturnTickSubsXml()); } } }
private static void AddTick(string board, string seccode) { var sub = TickList.FirstOrDefault(x => x.Board == board && x.Seccode == seccode); if (sub == null) { sub = new TickSubscription { Board = board, Seccode = seccode, Tradeno = 1 }; TickList.Add(sub); TXmlConnector.ConnectorSendCommand(ReturnTickSubsXml()); sub.Tradeno = 0; } }
private static void HandleTicks(TickSubscription tickSub, List <Tick> currentTicks) { //add ticks to ticks collection for this instument tickSub.Ticks.AddRange(currentTicks); ////populate simple ticks observable collection //currentTicks.ForEach(ct => _dispatcher.BeginInvoke(new Action(() => tickSub.SimpleTicks.Insert(0, ct)), DispatcherPriority.Background)); //send data to all windows with corresponding instrument opened var invocationList = SendTickDataEvent?.GetInvocationList() .Where(x => ((dynamic)x.Target).Seccode == tickSub.Seccode) .ToList(); invocationList.ForEachDo(x => x.DynamicInvoke(currentTicks)); //add data to volumes collections (chart) if (tickSub.Board != "MCT" && tickSub.Board != "INDEXR" && tickSub.Board != "INDEXM") { Task.Run(() => { var points = currentTicks.Select(t => t.Price).Distinct(); foreach (var point in points) { if (!tickSub.HorizontalVolumesBuy.XValues.Contains(point)) { tickSub.HorizontalVolumesBuy.Append(point, 0); } if (!tickSub.HorizontalVolumesSell.XValues.Contains(point)) { tickSub.HorizontalVolumesSell.Append(point, 0); } } foreach (var tick in currentTicks) { if (tick.Buysell == "B") { var index = tickSub.HorizontalVolumesBuy.FindIndex(tick.Price); if (index != -1) { tickSub.HorizontalVolumesBuy.Update(index, tickSub.HorizontalVolumesBuy.YValues[index] + tick.Price); } } else { var index = tickSub.HorizontalVolumesSell.FindIndex(tick.Price); if (index != -1) { tickSub.HorizontalVolumesSell.Update(index, tickSub.HorizontalVolumesSell.YValues[index] + tick.Price); } } } }); } //process alltrades var currentTradeItems = (from x in currentTicks select new TradeItem(x.Seccode, x.Price, x.Quantity, DateTime.Parse(x.Tradetime).ToString("HH:mm:ss.fff"), x.Buysell, x.Quantity.ToString()) { OpenInterest = x.OpenInterest, Board = x.Board }).ToList(); //calculate interest only for FUT if (tickSub.Board == "FUT") { var i = 0; //if no trades, set first interest delta to 0 and skip first trade when counting further if (tickSub.TradeItems.Count == 0) { currentTradeItems.First().InterestDelta = "0"; i++; } for (; i < currentTradeItems.Count; i++) { var tradeItem = currentTradeItems[i]; //for first new trade, calculate delta using newest trade in alltrades if (i == 0) { var lastTrade = tickSub.TradeItems.First().OpenInterest; tradeItem.InterestDelta = (int.Parse(tradeItem.OpenInterest) - int.Parse(lastTrade.Split(',').Last())).ToString(); } else { tradeItem.InterestDelta = (int.Parse(tradeItem.OpenInterest) - int.Parse(currentTradeItems[i - 1].OpenInterest)).ToString(); } } } var tradeItems = currentTradeItems.GroupBy(item => new { item.Seccode, item.Time, item.Buysell }) .Select( g => new TradeItem(g.Key.Seccode, g.Key.Buysell == "B" ? g.Select(t => t.Price).Min() : g.Select(t => t.Price).Max(), g.Sum(t => t.Quantity), g.Key.Time, g.Key.Buysell, string.Join(",", g.Select(t => t.Quantity))) { PriceList = string.Join(";", g.Select(t => t.Price).Distinct()), MiSide = g.Key.Buysell == "B" ? "" + (g.Select(t => t.Price).Max() - g.Select(t => t.Price).Min()).ToString("F2") : "-" + (g.Select(t => t.Price).Max() - g.Select(t => t.Price).Min()).ToString("F2"), OpenInterest = string.Join(",", g.Select(t => t.OpenInterest)), InterestDelta = string.Join(",", g.Select(t => t.InterestDelta)), }).ToList(); foreach (var t in tradeItems) { if (t.Quantity.ToString() != t.Sum) { t.IsMul = true; } foreach (var size in t.Sum.Split(',')) { if (int.Parse(size) > t.Quantity * 0.8) { t.IsEaten = true; } } if (tickSub.Board == "MCT") { var level2 = Application.Current.Dispatcher.Invoke(() => Level2DataHandler.Level2List .FirstOrDefault(x => x.Board == tickSub.Board && x.Seccode == tickSub.Seccode) ?.Level2Data .FirstOrDefault(item => item.Price == t.Price)); if (level2 != null) { t.Buysell = level2.BuySell == "buy" ? "S" : "B"; var str = t.MiSide; str = str.TrimStart('-'); var sign = level2.BuySell == "buy" ? "-" : ""; str = str.Insert(0, sign); t.MiSide = str; } } } tradeItems.Sort((a, b) => a.Time.CompareTo(b.Time)); //tickSub.TradeItems.DoOperation(col => col.InsertRange(0, tradeItems)); //if (tradeItems.Count < 5 && !tickSub.Sorted) //{ // tickSub.TradeItems.DoOperation(col => col.Sort((a, b) => b.Time.CompareTo(a.Time))); // tickSub.Sorted = true; //} //if (tradeItems.Count > 500) // //_dispatcher.BeginInvoke(new Action(() => tickSub.TradeItems.InsertRange(tradeItems)), DispatcherPriority.Background); // _dispatcher.Invoke(() => tickSub.TradeItems.InsertRange(tradeItems)); //else foreach (var item in tradeItems) { //_dispatcher.BeginInvoke(new Action(() => tickSub.TradeItems.Insert(0, item)), DispatcherPriority.Background); _dispatcher.Invoke(() => tickSub.TradeItems.Insert(0, item)); } //if (tradeItems.Count < 5 && !tickSub.Sorted) //{ // tickSub.TradeItems = new ObservableCollection<TradeItem>(tickSub.TradeItems.OrderByDescending(x => x.Time)); // tickSub.Sorted = true; //} }