private void RemoveOldPrices(ChartValues <OrderBookPriceLevel> cv, List <OrderBookPriceLevel> pl)
        {
            var removePoints = cv.Where(v => !pl.Any(p => p.Price == v.Price)).ToList();

            foreach (var point in removePoints)
            {
                cv.Remove(point);
            }
        }
Exemplo n.º 2
0
        public void UpdateChartValues(ChartValues <OrderBookPriceLevel> cv, List <OrderBookPriceLevel> pl, bool isAsk)
        {
            Func <OrderBookPriceLevel, OrderBookPriceLevel, OrderBookPriceLevel> f = ((p, n) =>
            {
                p.Quantity = n.Quantity;
                return(p);
            });

            var removePoints = cv.Where(v => !pl.Any(p => p.Price == v.Price)).ToList();

            foreach (var point in removePoints)
            {
                cv.Remove(point);
            }

            (from v in cv
             join p in pl
             on v.Price equals p.Price
             select f(v, p)).ToList();

            var addPoints   = pl.Where(p => !cv.Any(v => v.Price == p.Price)).ToList();
            var appendRange = new List <OrderBookPriceLevel>();

            foreach (var point in addPoints)
            {
                for (int i = 0; i < cv.Count; i++)
                {
                    if (isAsk)
                    {
                        if (point.Price < cv[i].Price)
                        {
                            cv.Insert(i, point);
                            break;
                        }
                    }
                    else
                    {
                        if (point.Price > cv[i].Price)
                        {
                            cv.Insert(i, point);
                            break;
                        }
                    }

                    if (i == cv.Count - 1)
                    {
                        appendRange.Add(point);
                    }
                }
            }

            if (appendRange.Any())
            {
                if (isAsk)
                {
                    cv.AddRange(appendRange.OrderBy(p => p.Price));
                }
                else
                {
                    cv.AddRange(appendRange.OrderByDescending(p => p.Price));
                }
            }
        }