Esempio n. 1
0
        /// <summary>
        ///     Optimize all the checked parameters
        /// </summary>
        private void OptimizeParams(BackgroundWorker worker, DoWorkEventArgs e)
        {
            int bestBalance = (isOOS ? Backtester.Balance(barOOS) : Backtester.NetBalance);

            // First Optimization Cycle
            for (int round = 0; round < checkedParams && isOptimizing; round++)
            {
                if (worker.CancellationPending)
                {
                    break;
                }

                int param = aiChecked[round];

                var min  = (double)anudParameterMin[param].Value;
                var max  = (double)anudParameterMax[param].Value;
                var step = (double)anudParameterStep[param].Value;

                for (double value = min; value <= max; value += step)
                {
                    if (worker.CancellationPending)
                    {
                        break;
                    }

                    aParameter[param].Value = value;
                    if (aParameter[param].Type == OptimizerParameterType.Indicator)
                    {
                        CalculateIndicator(aParameter[param].SlotNumber);
                    }

                    Backtester.Calculate();
                    Backtester.CalculateAccountStats();
                    if (chbOptimizerWritesReport.Checked)
                    {
                        FillInReport();
                    }

                    int  balance             = isOOS ? Backtester.Balance(barOOS) : Backtester.NetBalance;
                    bool isCriteriaFulfilled = criteriaControls.IsCriteriaFulfilled();
                    if (balance > bestBalance && isCriteriaFulfilled)
                    {
                        bestBalance = balance;
                        aParameter[param].BestValue = value;
                        ShowParamBestValue(param);
                        balanceChart.SetChartData();
                        balanceChart.InitChart();
                        balanceChart.Invalidate();
                        isStartegyChanged = true;
                        SetStrategyToGeneratorHistory();
                    }

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

                aParameter[param].Value = aParameter[param].BestValue;
                if (aParameter[param].Type == OptimizerParameterType.Indicator)
                {
                    CalculateIndicator(aParameter[param].SlotNumber);
                }
                Backtester.Calculate();
                Backtester.CalculateAccountStats();
            }

            if (worker.CancellationPending)
            {
                e.Cancel = true;
                return;
            }

            if (checkedParams < 2)
            {
                return;
            }

            // Counts the necessary round
            int rounds = 0;

            for (int i = 0; i < checkedParams - 1; i++)
            {
                for (int j = 0; j < checkedParams; j++)
                {
                    if (i < j)
                    {
                        rounds++;
                    }
                }
            }

            var aCp     = new CoupleOfParams[rounds];
            var aCpTemp = new CoupleOfParams[rounds];

            rounds = 0;
            for (int i = 0; i < checkedParams - 1; i++)
            {
                for (int j = 0; j < checkedParams; j++)
                {
                    if (i < j)
                    {
                        aCpTemp[rounds].Param1   = aiChecked[i];
                        aCpTemp[rounds].Param2   = aiChecked[j];
                        aCpTemp[rounds].IsPassed = false;
                        rounds++;
                    }
                }
            }

            // Shaking the parameters
            for (int round = 0; round < rounds; round++)
            {
                int couple;
                do
                {
                    couple = rand.Next(rounds);
                } while (aCpTemp[couple].IsPassed);
                aCpTemp[couple].IsPassed = true;
                aCp[round] = aCpTemp[couple];
            }

            // The Optimization Cycle
            for (int round = 0; round < rounds; round++)
            {
                if (worker.CancellationPending)
                {
                    break;
                }

                int  param1         = aCp[round].Param1;
                int  param2         = aCp[round].Param2;
                bool isOneIndicator = (aParameter[param1].Type == OptimizerParameterType.Indicator &&
                                       aParameter[param2].Type == OptimizerParameterType.Indicator &&
                                       aParameter[param1].IndParam.IndicatorName ==
                                       aParameter[param2].IndParam.IndicatorName);

                var min1  = (double)anudParameterMin[param1].Value;
                var max1  = (double)anudParameterMax[param1].Value;
                var step1 = (double)anudParameterStep[param1].Value;

                var min2  = (double)anudParameterMin[param2].Value;
                var max2  = (double)anudParameterMax[param2].Value;
                var step2 = (double)anudParameterStep[param2].Value;

                for (double value1 = min1; value1 <= max1; value1 += step1)
                {
                    if (worker.CancellationPending)
                    {
                        break;
                    }

                    if (!isOneIndicator)
                    {
                        aParameter[param1].Value = value1;
                        if (aParameter[param1].Type == OptimizerParameterType.Indicator)
                        {
                            CalculateIndicator(aParameter[param1].SlotNumber);
                        }
                    }

                    for (double value2 = min2; value2 <= max2; value2 += step2)
                    {
                        if (worker.CancellationPending)
                        {
                            break;
                        }

                        if (isOneIndicator)
                        {
                            aParameter[param1].Value = value1;
                            aParameter[param2].Value = value2;
                            if (aParameter[param1].Type == OptimizerParameterType.Indicator)
                            {
                                CalculateIndicator(aParameter[param1].SlotNumber);
                            }
                        }
                        else
                        {
                            aParameter[param2].Value = value2;
                            if (aParameter[param2].Type == OptimizerParameterType.Indicator)
                            {
                                CalculateIndicator(aParameter[param2].SlotNumber);
                            }
                        }

                        // Calculates the Strategy
                        Backtester.Calculate();
                        Backtester.CalculateAccountStats();
                        if (chbOptimizerWritesReport.Checked)
                        {
                            FillInReport();
                        }

                        int  balance             = isOOS ? Backtester.Balance(barOOS) : Backtester.NetBalance;
                        bool isCriteriaFulfilled = criteriaControls.IsCriteriaFulfilled();
                        if (balance > bestBalance && isCriteriaFulfilled)
                        {
                            bestBalance = balance;
                            aParameter[param1].BestValue = value1;
                            aParameter[param2].BestValue = value2;
                            ShowParamBestValue(param1);
                            ShowParamBestValue(param2);
                            balanceChart.SetChartData();
                            balanceChart.InitChart();
                            balanceChart.Invalidate();
                            isStartegyChanged = true;
                            SetStrategyToGeneratorHistory();
                        }

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

                aParameter[param1].Value = aParameter[param1].BestValue;
                aParameter[param2].Value = aParameter[param2].BestValue;

                if (isOneIndicator)
                {
                    if (aParameter[param1].Type == OptimizerParameterType.Indicator)
                    {
                        CalculateIndicator(aParameter[param1].SlotNumber);
                    }
                }
                else
                {
                    if (aParameter[param1].Type == OptimizerParameterType.Indicator)
                    {
                        CalculateIndicator(aParameter[param1].SlotNumber);
                    }
                    if (aParameter[param2].Type == OptimizerParameterType.Indicator)
                    {
                        CalculateIndicator(aParameter[param2].SlotNumber);
                    }
                }

                Backtester.Calculate();
                Backtester.CalculateAccountStats();
            }

            if (worker.CancellationPending)
            {
                e.Cancel = true;
            }
        }
        /// <summary>
        ///     Optimize all the checked parameters
        /// </summary>
        private void OptimizeParams(BackgroundWorker worker, DoWorkEventArgs e)
        {
            int bestBalance = (isOOS ? Backtester.Balance(barOOS) : Backtester.NetBalance);

            // First Optimization Cycle
            for (int round = 0; round < checkedParams && isOptimizing; round++)
            {
                if (worker.CancellationPending) break;

                int param = aiChecked[round];

                var min = (double) anudParameterMin[param].Value;
                var max = (double) anudParameterMax[param].Value;
                var step = (double) anudParameterStep[param].Value;

                for (double value = min; value <= max; value += step)
                {
                    if (worker.CancellationPending) break;

                    aParameter[param].Value = value;
                    if (aParameter[param].Type == OptimizerParameterType.Indicator)
                        CalculateIndicator(aParameter[param].SlotNumber);

                    Backtester.Calculate();
                    Backtester.CalculateAccountStats();
                    if (chbOptimizerWritesReport.Checked)
                        FillInReport();

                    int balance = isOOS ? Backtester.Balance(barOOS) : Backtester.NetBalance;
                    bool isCriteriaFulfilled = criteriaControls.IsCriteriaFulfilled();
                    if (balance > bestBalance && isCriteriaFulfilled)
                    {
                        bestBalance = balance;
                        aParameter[param].BestValue = value;
                        ShowParamBestValue(param);
                        balanceChart.SetChartData();
                        balanceChart.InitChart();
                        balanceChart.Invalidate();
                        isStartegyChanged = true;
                        SetStrategyToGeneratorHistory();
                    }

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

                aParameter[param].Value = aParameter[param].BestValue;
                if (aParameter[param].Type == OptimizerParameterType.Indicator)
                    CalculateIndicator(aParameter[param].SlotNumber);
                Backtester.Calculate();
                Backtester.CalculateAccountStats();
            }

            if (worker.CancellationPending)
            {
                e.Cancel = true;
                return;
            }

            if (checkedParams < 2)
                return;

            // Counts the necessary round
            int rounds = 0;
            for (int i = 0; i < checkedParams - 1; i++)
                for (int j = 0; j < checkedParams; j++)
                    if (i < j) rounds++;

            var aCp = new CoupleOfParams[rounds];
            var aCpTemp = new CoupleOfParams[rounds];

            rounds = 0;
            for (int i = 0; i < checkedParams - 1; i++)
                for (int j = 0; j < checkedParams; j++)
                    if (i < j)
                    {
                        aCpTemp[rounds].Param1 = aiChecked[i];
                        aCpTemp[rounds].Param2 = aiChecked[j];
                        aCpTemp[rounds].IsPassed = false;
                        rounds++;
                    }

            // Shaking the parameters
            for (int round = 0; round < rounds; round++)
            {
                int couple;
                do
                {
                    couple = rand.Next(rounds);
                } while (aCpTemp[couple].IsPassed);
                aCpTemp[couple].IsPassed = true;
                aCp[round] = aCpTemp[couple];
            }

            // The Optimization Cycle
            for (int round = 0; round < rounds; round++)
            {
                if (worker.CancellationPending) break;

                int param1 = aCp[round].Param1;
                int param2 = aCp[round].Param2;
                bool isOneIndicator = (aParameter[param1].Type == OptimizerParameterType.Indicator &&
                                       aParameter[param2].Type == OptimizerParameterType.Indicator &&
                                       aParameter[param1].IndParam.IndicatorName ==
                                       aParameter[param2].IndParam.IndicatorName);

                var min1 = (double) anudParameterMin[param1].Value;
                var max1 = (double) anudParameterMax[param1].Value;
                var step1 = (double) anudParameterStep[param1].Value;

                var min2 = (double) anudParameterMin[param2].Value;
                var max2 = (double) anudParameterMax[param2].Value;
                var step2 = (double) anudParameterStep[param2].Value;

                for (double value1 = min1; value1 <= max1; value1 += step1)
                {
                    if (worker.CancellationPending) break;

                    if (!isOneIndicator)
                    {
                        aParameter[param1].Value = value1;
                        if (aParameter[param1].Type == OptimizerParameterType.Indicator)
                            CalculateIndicator(aParameter[param1].SlotNumber);
                    }

                    for (double value2 = min2; value2 <= max2; value2 += step2)
                    {
                        if (worker.CancellationPending) break;

                        if (isOneIndicator)
                        {
                            aParameter[param1].Value = value1;
                            aParameter[param2].Value = value2;
                            if (aParameter[param1].Type == OptimizerParameterType.Indicator)
                                CalculateIndicator(aParameter[param1].SlotNumber);
                        }
                        else
                        {
                            aParameter[param2].Value = value2;
                            if (aParameter[param2].Type == OptimizerParameterType.Indicator)
                                CalculateIndicator(aParameter[param2].SlotNumber);
                        }

                        // Calculates the Strategy
                        Backtester.Calculate();
                        Backtester.CalculateAccountStats();
                        if (chbOptimizerWritesReport.Checked)
                            FillInReport();

                        int balance = isOOS ? Backtester.Balance(barOOS) : Backtester.NetBalance;
                        bool isCriteriaFulfilled = criteriaControls.IsCriteriaFulfilled();
                        if (balance > bestBalance && isCriteriaFulfilled)
                        {
                            bestBalance = balance;
                            aParameter[param1].BestValue = value1;
                            aParameter[param2].BestValue = value2;
                            ShowParamBestValue(param1);
                            ShowParamBestValue(param2);
                            balanceChart.SetChartData();
                            balanceChart.InitChart();
                            balanceChart.Invalidate();
                            isStartegyChanged = true;
                            SetStrategyToGeneratorHistory();
                        }

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

                aParameter[param1].Value = aParameter[param1].BestValue;
                aParameter[param2].Value = aParameter[param2].BestValue;

                if (isOneIndicator)
                {
                    if (aParameter[param1].Type == OptimizerParameterType.Indicator)
                        CalculateIndicator(aParameter[param1].SlotNumber);
                }
                else
                {
                    if (aParameter[param1].Type == OptimizerParameterType.Indicator)
                        CalculateIndicator(aParameter[param1].SlotNumber);
                    if (aParameter[param2].Type == OptimizerParameterType.Indicator)
                        CalculateIndicator(aParameter[param2].SlotNumber);
                }

                Backtester.Calculate();
                Backtester.CalculateAccountStats();
            }

            if (worker.CancellationPending)
            {
                e.Cancel = true;
            }
        }