/// <summary> /// Optimize all the checked parameters /// </summary> 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]; double min = (double)anudParameterMin[param].Value; double max = (double)anudParameterMax[param].Value; double step = (double)anudParameterStep[param].Value; for (double value = min; value <= max; value += step) { if (worker.CancellationPending) break; aParameter[param].Value = value; CalculateIndicator(aParameter[param].SlotNumber); Backtester.Calculate(); Backtester.CalculateAccountStats(); if (Configs.OptimizerWritesReport) FillInReport(); int balance = isOOS ? Backtester.Balance(barOOS) : Backtester.NetBalance; if (balance > bestBalance && CalculateLimitations()) { bestBalance = balance; aParameter[param].BestValue = value; ShowParamBestValue(param); smallBalanceChart.InitChart(); smallBalanceChart.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; CalculateIndicator(aParameter[param].SlotNumber); Backtester.Calculate(); Backtester.CalculateAccountStats(); } if (worker.CancellationPending) { e.Cancel = true; return; } if (checkedParams < 2) return; // Counts the neccessary round int rounds= 0; for (int i = 0; i < checkedParams - 1; i++) for (int j = 0; j < checkedParams ; j++) if (i < j) rounds++; CoupleOfParams[] aCP = new CoupleOfParams[rounds]; CoupleOfParams[] 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 params for (int round = 0; round < rounds; round++) { int couple = 0; do { couple = random.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].IP.IndicatorName == aParameter[param2].IP.IndicatorName); double min1 = (double)anudParameterMin[param1].Value; double max1 = (double)anudParameterMax[param1].Value; double step1 = (double)anudParameterStep[param1].Value; double min2 = (double)anudParameterMin[param2].Value; double max2 = (double)anudParameterMax[param2].Value; double step2 = (double)anudParameterStep[param2].Value; for (double value1 = min1; value1 <= max1; value1 += step1) { if (worker.CancellationPending) break; if (!isOneIndicator) { aParameter[param1].Value = value1; 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; CalculateIndicator(aParameter[param1].SlotNumber); } else { aParameter[param2].Value = value2; CalculateIndicator(aParameter[param2].SlotNumber); } // Calculates the Strategy Backtester.Calculate(); Backtester.CalculateAccountStats(); if (Configs.OptimizerWritesReport) FillInReport(); int balance = isOOS ? Backtester.Balance(barOOS) : Backtester.NetBalance; if (balance > bestBalance && CalculateLimitations()) { bestBalance = balance; aParameter[param1].BestValue = value1; aParameter[param2].BestValue = value2; ShowParamBestValue(param1); ShowParamBestValue(param2); smallBalanceChart.InitChart(); smallBalanceChart.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) { CalculateIndicator(aParameter[param1].SlotNumber); } else { CalculateIndicator(aParameter[param1].SlotNumber); CalculateIndicator(aParameter[param2].SlotNumber); } Backtester.Calculate(); Backtester.CalculateAccountStats(); } if (worker.CancellationPending) { e.Cancel = true; } return; }