Forex_Strategy_Builder.Chart.PnlBalance_Paint C# (CSharp) Method

PnlBalance_Paint() private method

Paints the panel PnlBalance
private PnlBalance_Paint ( object sender, PaintEventArgs e ) : void
sender object
e PaintEventArgs
return void
        void PnlBalance_Paint(object sender, PaintEventArgs e)
        {
            if (!isBalanceEquityShown) return;

            Panel   pnl = (Panel)sender;
            Graphics g  = e.Graphics;

            g.Clear(LayoutColors.ColorChartBack);

            if (chartBars == 0) return;

            int topSpace   = Font.Height / 2 + 2;
            int bottmSpace = Font.Height / 2;
            int yTop       = topSpace;
            int yBottom    = pnl.ClientSize.Height - bottmSpace;
            int xLeft      = XLeft;
            int xRight     = XRight;

            // Min and Max values
            int maxValue = int.MinValue;
            int minValue = int.MaxValue;
            int value;
            for (int iBar = Math.Max(firstBar, Data.FirstBar); iBar <= lastBar; iBar++)
            {
                value = Configs.AccountInMoney ? (int)Backtester.MoneyBalance(iBar) : Backtester.Balance(iBar);
                if (value > maxValue) maxValue = value;
                if (value < minValue) minValue = value;
                value = Configs.AccountInMoney ? (int)Backtester.MoneyEquity(iBar) : Backtester.Equity(iBar);
                if (value > maxValue) maxValue = value;
                if (value < minValue) minValue = value;
            }

            if (maxValue == 0 && minValue == 0)
            {
                maxValue = 10;
                minValue = -10;
            }

            if (maxValue == minValue)
            {
                maxValue += 10;
                minValue -= 10;
            }

            int countLabels = (int)Math.Max((yBottom - yTop) / 30, 1);
            int deltaLabels = 10 * (((int)Math.Max((maxValue - minValue) / countLabels, 10)) / 10);

            minValue  = 10 * (int)Math.Floor(minValue / 10.0);
            countLabels = (int)Math.Ceiling((maxValue - minValue) / (double)deltaLabels);
            maxValue  = minValue + countLabels * deltaLabels;

            double scale = (yBottom - yTop) / ((double)countLabels * deltaLabels);

            // Grid
            for (int label = minValue; label <= maxValue; label += deltaLabels)
            {
                int yLabel = (int)Math.Round(yBottom - (label - minValue) * scale);
                g.DrawString(label.ToString(), Font, brushFore, XRight, yLabel - Font.Height / 2 - 1);
                if (isGridShown)
                    g.DrawLine(penGrid, xLeft, yLabel, xRight, yLabel);
                else
                    g.DrawLine(penGrid, xRight - 5, yLabel, xRight, yLabel);
            }

                // Vertical grid lines
            if (isGridShown)
            {
                for (int vertLineBar = lastBar; vertLineBar > firstBar; vertLineBar -= (int)Math.Round((szDate.Width + 10.0) / barPixels + 1))
                {
                    int xVertLine = (vertLineBar - firstBar) * barPixels + xLeft + barPixels / 2 - 1;
                    g.DrawLine(penGrid, xVertLine, yTop, xVertLine, yBottom);
                }
            }

            // Chart
            Point[] apntBalance = new Point[lastBar - firstBar + 1];
            Point[] apntEquity  = new Point[lastBar - firstBar + 1];
            for (int bar = firstBar; bar <= lastBar; bar++)
            {
                int x, y;
                value = Configs.AccountInMoney ? (int)Backtester.MoneyBalance(bar) : Backtester.Balance(bar);
                x = (bar - firstBar) * barPixels + barPixels / 2 - 1 + xLeft;
                y = (int)Math.Round(yBottom - (value - minValue) * scale);
                apntBalance[bar - firstBar] = new Point(x, y);
                value = Configs.AccountInMoney ? (int)Backtester.MoneyEquity(bar) : Backtester.Equity(bar);
                y = (int)Math.Round(yBottom - (value - minValue) * scale);
                apntEquity[bar - firstBar] = new Point(x, y);
            }
            g.DrawLines(new Pen(LayoutColors.ColorChartEquityLine ), apntEquity);
            g.DrawLines(new Pen(LayoutColors.ColorChartBalanceLine), apntBalance);

            // Vertical cross line
            if (isCrossShown && mouseX > XLeft - 1 && mouseX < XRight + 1)
                g.DrawLine(penCross, mouseX, 0, mouseX, pnl.ClientSize.Height);

            // Chart title
            string sTitle = Language.T("Balance") + " / " + Language.T("Equity") +
                " [" + (Configs.AccountInMoney ? Configs.AccountCurrency + "]" : Language.T("pips") + "]");
            Size szTitle = g.MeasureString(sTitle, Font).ToSize();
            g.FillRectangle(brushBack, new Rectangle(spcLeft, 0, szTitle.Width, szTitle.Height));
            g.DrawString(sTitle, Font, brushFore, spcLeft, 0);

            return;
        }