/// <summary>
        /// Call from:
        /// 1) ChangeIntervals
        /// 2) TaskLoadData
        /// </summary>
        public void RebuildCurrentClusters()
        {
            lock (_lockTimes)
            {
                lock (_lckClustersCurrent)
                {
                    BuildTimeIntervals();
                    _clusterPriceCurrent.Clear();
                    _clusterDateCurrent.Clear();


                    foreach (var kvp in _clusterPriceM1)
                    {
                        double price = kvp.Key;

                        foreach (var kvp2 in kvp.Value)
                        {
                            CCluster cluster = kvp2.Value;
                            DateTime dt      = kvp2.Key;
                            for (int j = _lstTimes.Count - 1; j >= 0; j--)
                            {
                                if ((j != 0 && dt >= _lstTimes[j] && dt < _lstTimes[j - 1])
                                    ||
                                    (j == 0 && dt >= _lstTimes[j])
                                    )
                                {
                                    _clusterPriceCurrent.UpdateCluster(price, _lstTimes[j], cluster);
                                    _clusterDateCurrent.UpdateCluster(_lstTimes[j], cluster);
                                }
                            }
                        }
                    }
                }
            }
        }
        private void DrawClusterPriceBarBg(/*double price, int amount*/ CCluster clusterData, double y, DateTime dtInterval, double actualWidthClusterGrid)
        {
            GenClusterBrushPen(clusterData, dtInterval);

            //calculate width of cluster bar
            long dealsAmountFullBar = CUtilConv.GetIntVolume(Math.Max(_dealsAmountFullBar, 1), _decimalVolume);//zero devide protect

            _volumeRectWidth = (((double)clusterData.AmountTotal) / ((double)dealsAmountFullBar)) * actualWidthClusterGrid;
            //draw cluster bar with proportional width
            //DrawRectangle(_brushCluster, _penCluster, new Rect(0.0, y, _volumeRectWidth, (double)_stirngHeight));
            _drwCntxtClustersBg.DrawRectangle(_brushCluster, _penCluster, new Rect(0.0, y, _volumeRectWidth, (double)_stirngHeight));


            //   FormattedText(text, _cultureInfoDefault, FlowDirection.LeftToRight, _typeFaceDefault, _fontSizeScaled, brush);

            //2018-02-21
            string stAmount = CTerminalUtil.GetAmount(clusterData.AmountTotal, _decimalVolume);



            //print cluster amount
            //DrawText(_FrmTxtCurrClustAmount, new Point(0.0, y));
            //KAA 2017-03-01 changed to Glyph
            //2018-05-01 changed to fontsizeScaled
            GlyphRun gr = _glyphGenBg.GetGlyph(0, y + _fontSizeScaled,
                                               _fontSizeScaled,
                                               " " + /*clusterData.AmountTotal.ToString()*/ stAmount);

            _drwCntxtClustersBg.DrawGlyphRun(_brushFontBg, gr);
        }
        private void GenClusterBrushPen(CCluster clusterData, DateTime Time)
        {
            double buyAmount  = clusterData.AmountBuy;
            double sellAmount = clusterData.AmountSell;

            double balance = 0;



            if (clusterData.TotalDir == EnmDealDirection.Sell)
            {
                if (sellAmount != 0)
                {
                    balance = (200.0 * buyAmount) / sellAmount;
                }

                _colorClusterCurr = Color.FromArgb(0x73, 200, Convert.ToByte(balance), Convert.ToByte(balance));
            }
            else
            {
                if (buyAmount != 0)
                {
                    balance = (200.0 * sellAmount) / buyAmount;
                }
                _colorClusterCurr = Color.FromArgb(0x73, Convert.ToByte(balance), 200, Convert.ToByte(balance));
            }


            _brushCluster = new SolidColorBrush(_colorClusterCurr);
            _penCluster   = new Pen(_brushCluster, 1.0);
        }
        private void DrawClusterDateBg(CCluster cluster, double actualCanvasWidth)
        {
            //double actualWidthClusterGrid = GridCanvasControlClusters.ColumnDefinitions[1].ActualWidth;

            GenClusterTotalBrushBg(cluster.AmountBuy, cluster.AmountSell);

            double offsetBottom = 12;//27.0;
            double y            = _actualHeight - _stirngHeight - offsetBottom;

            //Draw rectangle with summary of period on bottom of screen
            //DrawRectangle(_brushClusterTotal, _penClusterTotal, new Rect(0.0, y, actualCanvasWidth, (double)_stirngHeight));
            _drwCntxtClustersBg.DrawRectangle(_brushClusterTotalBg, _penClusterTotalBg,
                                              new Rect(0.0, y, actualCanvasWidth, (double)_stirngHeight));
            //2018-07-10
            string stAmountTotal = CTerminalUtil.GetAmount(cluster.AmountTotal, _decimalVolume);

            _FrmTxtCurrClustAmount = GetFormattedTextBg(" " + stAmountTotal);

            _drwCntxtClustersBg.DrawText(_FrmTxtCurrClustAmount, new Point(0.0, y));
        }
        private void DrawClustersBg()
        {
            double actualWidthClusterGrid = 0;


            _lstTimes = new List <DateTime>();

            Dictionary <double, double> dictPriceY = null;


            CopyDataPrevIteration();


            bool bNoNeedDraw = false;



            _guiDispatcher.Invoke(new Action
                                      (() =>
            {
                try
                {
                    if (GridControlMarket == null || GridControlMarket.ColumnDefinitions.Count == 0 ||
                        GridCanvasControlClusters == null || GridCanvasControlClusters.ColumnDefinitions.Count < 2)
                    {
                        bNoNeedDraw = true;
                        return;
                    }

                    _widthClusterVisible = GridControlMarket.ColumnDefinitions[0].ActualWidth;
                    _widthOneCluster     = GridCanvasControlClusters.ColumnDefinitions[1].ActualWidth;


                    if (_widthClusterVisible <= 0 || _widthClusterVisible <= 0)
                    {
                        bNoNeedDraw = true;
                        return;
                    }


                    _lstDrawClusterPrice = new List <List <CDrawCluster> >();
                    _lstDrawClusterDate  = new List <CCluster>();


                    lock (DictPriceY)
                        dictPriceY = new Dictionary <double, double>(DictPriceY);

                    if (dictPriceY.Count == 0)
                    {
                        bNoNeedDraw = true;
                        return;
                    }

                    actualWidthClusterGrid = GridCanvasControlClusters.ColumnDefinitions[1].ActualWidth;

                    _colsClustVisble = (int)(_widthClusterVisible / _widthOneCluster) + 1;

                    lock (LstTimes)
                        _lstTimes = new List <DateTime>(LstTimes);

                    _lstTimes.RemoveRange(_colsClustVisble, _lstTimes.Count - _colsClustVisble);

                    _dealsAmountFullBar = DealsAmountFullBar;
                    _decimalVolume      = DecimalVolume;

                    _stirngHeight = StringHeight;



                    _clusterPriceAmount = ClusterPriceAmount;
                    _clusterDate        = ClusterDate;


                    CCluster clusterDateSrc = /*ClusterDate*/ ClusterDate.GetValue(_lstTimes[0]);
                }
                catch (Exception exc)
                {
                    Error("DrawClustersBackground.GUIDisp", exc);
                }
            }
                                      ));



            //double actualWidthClusterGrid = GridCanvasControlClusters.ColumnDefinitions[1].ActualWidth;
            try
            {
                if (bNoNeedDraw)
                {
                    return;
                }



                for (int i = 0; i < _lstTimes.Count; i++)
                {
                    DateTime dt = _lstTimes[i];

                    _lstDrawClusterPrice.Add(new List <CDrawCluster>());

                    CCluster clusterDateSrc = /*ClusterDate*/ _clusterDate.GetValue(dt);

                    CCluster clusterDate = null;
                    if (clusterDateSrc != null)
                    {
                        clusterDate = clusterDateSrc.Copy();
                    }

                    _lstDrawClusterDate.Add(clusterDate);

                    //mod 2018-06-28 to make possible fractial prices

                    //foreach (var kvp in DictPriceY)
                    for (int j = 0; j < dictPriceY.Count - 1; j++)
                    {
                        var    kvp   = dictPriceY.ElementAt(j);
                        double price = kvp.Key;

                        if (price < 0)
                        {
                            continue;
                        }

                        double y         = kvp.Value;
                        var    kvpNext   = dictPriceY.ElementAt(j + 1);
                        double priceNext = kvpNext.Key;
                        double dlt       = priceNext - price;

                        double priceFrom = price - dlt / 2;
                        double priceTo   = price + dlt / 2;

                        // CCluster clusterPrice = ClusterPriceAmount.GetClusterData(price, dt);
                        CCluster clusterPrice = _clusterPriceAmount.GetAmountInPriceArea(priceFrom, priceTo, dt);



                        if (clusterPrice != null)
                        {
                            if (clusterPrice.AmountTotal != 0)
                            {
                                // DrawClusterPriceBar(clusterPrice, y, dt, actualWidthClusterGrid);
                                _lstDrawClusterPrice[i].Add(new CDrawCluster()
                                {
                                    Cluster = clusterPrice.Copy(),
                                    Y       = y,
                                    Dt      = dt,
                                    Width   = actualWidthClusterGrid
                                }
                                                            );
                            }
                        }
                    }
                }



                List <int> lstRedrawSegments = GetNeedDrawImagesInd( );



                int _cntUse = Math.Min(_lstDrawClusterPrice.Count, _colsClustVisble);


                for (int i = 0; i < _cntUse; i++)
                {
                    if (!lstRedrawSegments.Contains(i) && !_isNeedFullRedraw)
                    {
                        continue;
                    }

                    _drwCntxtClustersBg = _drwVisClustersBg.RenderOpen();



                    foreach (var el in _lstDrawClusterPrice[i])
                    {
                        DrawClusterPriceBarBg(el.Cluster, el.Y, el.Dt, el.Width);
                    }

                    if (_lstDrawClusterDate[i] != null)
                    {
                        DrawClusterDateBg(_lstDrawClusterDate[i], actualWidthClusterGrid);
                    }


                    //  CCluster clusterDate = ClusterDate.GetValue(dt);

                    //  if (clusterDate != null)
                    //  DrawClusterDate(clusterDate);
                    _drwCntxtClustersBg.Close();
                    _rendererClusterBg.Render(_lstImageSegmentsBg[i]);

                    _isNeedFullRedraw = false;
                }
            }
            catch (Exception e)
            {
                Error("DrawClusterBg", e);
            }
        }