/// <summary>
        ///     Calculates Data Tables.
        /// </summary>
        private void CalculateStatsTables(int percentDeviation, int countParam)
        {
            string unit = " " + Configs.AccountCurrency;

            var tableNames = new[]
            {
                Language.T("Account balance") + unit,
                Language.T("Profit per day") + unit,
                Language.T("Maximum drawdown") + unit,
                Language.T("Gross profit") + unit,
                Language.T("Gross loss") + unit,
                Language.T("Executed orders"),
                Language.T("Traded lots"),
                Language.T("Time in position") + " %",
                Language.T("Sent orders"),
                Language.T("Charged spread") + unit,
                Language.T("Charged rollover") + unit,
                Language.T("Winning trades"),
                Language.T("Losing trades"),
                Language.T("Win/loss ratio"),
                Language.T("Max equity drawdown") + " %"
            };

            tablesCount = tableNames.Length;
            tableReport = new OverOptimizationDataTable[tablesCount];

            for (int t = 0; t < tableNames.Length; t++)
            {
                tableReport[t] = new OverOptimizationDataTable(percentDeviation, countParam)
                {
                    Name = tableNames[t]
                };
            }

            int  parNumber        = 0;
            bool isBgWorkCanceled = false;

            for (int slot = 0; slot < Data.Strategy.Slots && !isBgWorkCanceled; slot++)
            {
                for (int numParam = 0; numParam < 6 && !isBgWorkCanceled; numParam++)
                {
                    if (Data.Strategy.Slot[slot].IndParam.NumParam[numParam].Enabled && parNumber < countParam)
                    {
                        for (int index = percentDeviation; index >= -percentDeviation && !isBgWorkCanceled; index--)
                        {
                            isBgWorkCanceled = bgWorker.CancellationPending;
                            Data.Strategy.Slot[slot].IndParam.NumParam[numParam].Value = tableParameters.GetData(
                                index, parNumber);

                            CalculateIndicator(slot);
                            Backtester.Calculate();
                            Backtester.CalculateAccountStats();

                            var statValues = new[]
                            {
                                Backtester.NetMoneyBalance,
                                Backtester.MoneyProfitPerDay,
                                Backtester.MaxMoneyDrawdown,
                                Backtester.GrossMoneyProfit,
                                Backtester.GrossMoneyLoss,
                                Backtester.ExecutedOrders,
                                Backtester.TradedLots,
                                Backtester.TimeInPosition,
                                Backtester.SentOrders,
                                Backtester.TotalChargedMoneySpread,
                                Backtester.TotalChargedMoneyRollOver,
                                Backtester.WinningTrades,
                                Backtester.LosingTrades,
                                Backtester.WinLossRatio,
                                Backtester.MoneyEquityPercentDrawdown
                            };

                            for (int tn = 0; tn < tablesCount; tn++)
                            {
                                tableReport[tn].SetData(index, parNumber, statValues[tn]);
                            }

                            // Report progress as a percentage of the total task.
                            computedCycles++;
                            int percentComplete = Math.Min(100 * computedCycles / cycles, 100);
                            if (percentComplete > progressPercent)
                            {
                                progressPercent = percentComplete;
                                bgWorker.ReportProgress(percentComplete);
                            }
                        }

                        // Set default value
                        Data.Strategy.Slot[slot].IndParam.NumParam[numParam].Value = tableParameters.GetData(0,
                                                                                                             parNumber);
                        CalculateIndicator(slot);
                        parNumber++;
                    }
                }
            }
        }
        /// <summary>
        ///     Sets the chart parameters
        /// </summary>
        public void InitChart(OverOptimizationDataTable table)
        {
            Table = table;

            // Chart Title
            chartTitle = table.Name;
            font = new Font(Font.FontFamily, 9);
            captionHeight = Math.Max(font.Height, 18);
            rectfCaption = new RectangleF(0, 0, ClientSize.Width, captionHeight);
            stringFormatCaption = new StringFormat
                {
                    Alignment = StringAlignment.Center,
                    LineAlignment = StringAlignment.Center,
                    Trimming = StringTrimming.EllipsisCharacter,
                    FormatFlags = StringFormatFlags.NoWrap
                };

            brushFore = new SolidBrush(LayoutColors.ColorChartFore);
            penGrid = new Pen(LayoutColors.ColorChartGrid)
                {DashStyle = DashStyle.Dash, DashPattern = new float[] {4, 2}};
            penBorder = new Pen(Data.GetGradientColor(LayoutColors.ColorCaptionBack, -LayoutColors.DepthCaption),
                                Border);

            devSteps = table.CountDeviationSteps;
            percentDev = (devSteps - 1)/2;
            parameters = table.CountStrategyParams;

            if (parameters == 0)
                return;

            double minValue = double.MaxValue;
            double maxValue = double.MinValue;

            for (int param = 0; param < parameters; param++)
            {
                double min = double.MaxValue;
                double max = double.MinValue;
                for (int dev = 0; dev < devSteps; dev++)
                {
                    int index = dev - percentDev;
                    double value = table.GetData(index, param);
                    if (min > value) min = value;
                    if (max < value) max = value;
                }
                if (minValue > min) minValue = min;
                if (maxValue < max) maxValue = max;
            }

            maximum = (int) Math.Round(maxValue + 0.1*Math.Abs(maxValue));
            minimum = (int) Math.Round(minValue - 0.1*Math.Abs(minValue));
            int roundStep = Math.Abs(minimum) > 1 ? 10 : 1;
            minimum = (int) (Math.Floor(minimum/(float) roundStep)*roundStep);
            if (Math.Abs(maximum - minimum) < 1) maximum = minimum + 1;

            yTop = (int) captionHeight + 2*Space + 1;
            yBottom = ClientSize.Height - 2*Space - 1 - Border - Space - Font.Height;

            Graphics g = CreateGraphics();
            labelWidth =
                (int)
                Math.Max(g.MeasureString(minimum.ToString(CultureInfo.InvariantCulture), font).Width,
                         g.MeasureString(maximum.ToString(CultureInfo.InvariantCulture), font).Width);
            g.Dispose();

            labelWidth = Math.Max(labelWidth, 30);
            xLeft = Border + 3*Space;
            xRight = ClientSize.Width - Border - 2*Space - labelWidth;
            xScale = (xRight - xLeft)/(float) (devSteps - 1);
            xMiddle = (int) (xLeft + percentDev*xScale);

            countLabels = Math.Max((yBottom - yTop)/20, 2);
            delta = (float) Math.Max(Math.Round((maximum - minimum)/(float) countLabels), roundStep);
            step = (int) Math.Ceiling(delta/roundStep)*roundStep;
            countLabels = (int) Math.Ceiling((maximum - minimum)/(float) step);
            maximum = minimum + countLabels*step;
            yScale = (yBottom - yTop)/(countLabels*(float) step);

            apntParameters = new PointF[parameters][];

            for (int param = 0; param < parameters; param++)
            {
                apntParameters[param] = new PointF[devSteps];
                for (int dev = 0; dev < devSteps; dev++)
                {
                    int index = dev - percentDev;
                    apntParameters[param][dev].X = xLeft + dev*xScale;
                    apntParameters[param][dev].Y =
                        (float) (yBottom - (table.GetData(index, param) - minimum)*yScale);
                }
            }
        }
예제 #3
0
        /// <summary>
        ///     Sets the chart parameters
        /// </summary>
        public void InitChart(OverOptimizationDataTable table)
        {
            Table = table;

            // Chart Title
            chartTitle          = table.Name;
            font                = new Font(Font.FontFamily, 9);
            captionHeight       = Math.Max(font.Height, 18);
            rectfCaption        = new RectangleF(0, 0, ClientSize.Width, captionHeight);
            stringFormatCaption = new StringFormat
            {
                Alignment     = StringAlignment.Center,
                LineAlignment = StringAlignment.Center,
                Trimming      = StringTrimming.EllipsisCharacter,
                FormatFlags   = StringFormatFlags.NoWrap
            };

            brushFore = new SolidBrush(LayoutColors.ColorChartFore);
            penGrid   = new Pen(LayoutColors.ColorChartGrid)
            {
                DashStyle = DashStyle.Dash, DashPattern = new float[] { 4, 2 }
            };
            penBorder = new Pen(Data.GetGradientColor(LayoutColors.ColorCaptionBack, -LayoutColors.DepthCaption),
                                Border);

            devSteps   = table.CountDeviationSteps;
            percentDev = (devSteps - 1) / 2;
            parameters = table.CountStrategyParams;

            if (parameters == 0)
            {
                return;
            }

            double minValue = double.MaxValue;
            double maxValue = double.MinValue;

            for (int param = 0; param < parameters; param++)
            {
                double min = double.MaxValue;
                double max = double.MinValue;
                for (int dev = 0; dev < devSteps; dev++)
                {
                    int    index = dev - percentDev;
                    double value = table.GetData(index, param);
                    if (min > value)
                    {
                        min = value;
                    }
                    if (max < value)
                    {
                        max = value;
                    }
                }
                if (minValue > min)
                {
                    minValue = min;
                }
                if (maxValue < max)
                {
                    maxValue = max;
                }
            }

            maximum = (int)Math.Round(maxValue + 0.1 * Math.Abs(maxValue));
            minimum = (int)Math.Round(minValue - 0.1 * Math.Abs(minValue));
            int roundStep = Math.Abs(minimum) > 1 ? 10 : 1;

            minimum = (int)(Math.Floor(minimum / (float)roundStep) * roundStep);
            if (Math.Abs(maximum - minimum) < 1)
            {
                maximum = minimum + 1;
            }

            yTop    = (int)captionHeight + 2 * Space + 1;
            yBottom = ClientSize.Height - 2 * Space - 1 - Border - Space - Font.Height;

            Graphics g = CreateGraphics();

            labelWidth =
                (int)
                Math.Max(g.MeasureString(minimum.ToString(CultureInfo.InvariantCulture), font).Width,
                         g.MeasureString(maximum.ToString(CultureInfo.InvariantCulture), font).Width);
            g.Dispose();

            labelWidth = Math.Max(labelWidth, 30);
            xLeft      = Border + 3 * Space;
            xRight     = ClientSize.Width - Border - 2 * Space - labelWidth;
            xScale     = (xRight - xLeft) / (float)(devSteps - 1);
            xMiddle    = (int)(xLeft + percentDev * xScale);

            countLabels = Math.Max((yBottom - yTop) / 20, 2);
            delta       = (float)Math.Max(Math.Round((maximum - minimum) / (float)countLabels), roundStep);
            step        = (int)Math.Ceiling(delta / roundStep) * roundStep;
            countLabels = (int)Math.Ceiling((maximum - minimum) / (float)step);
            maximum     = minimum + countLabels * step;
            yScale      = (yBottom - yTop) / (countLabels * (float)step);

            apntParameters = new PointF[parameters][];

            for (int param = 0; param < parameters; param++)
            {
                apntParameters[param] = new PointF[devSteps];
                for (int dev = 0; dev < devSteps; dev++)
                {
                    int index = dev - percentDev;
                    apntParameters[param][dev].X = xLeft + dev * xScale;
                    apntParameters[param][dev].Y =
                        (float)(yBottom - (table.GetData(index, param) - minimum) * yScale);
                }
            }
        }