/// <summary> /// Calculates the selected indicator. /// </summary> private void CalculateIndicator(bool bCalculateStrategy) { if (!Data.IsData || !Data.IsResult || !isPaint) { return; } SetOppositeSignalBehaviour(); SetClosingLogicConditions(); Indicator indicator = IndicatorManager.ConstructIndicator(indicatorName); indicator.Initialize(slotType); // List parameters for (int i = 0; i < 5; i++) { indicator.IndParam.ListParam[i].Index = ListParam[i].SelectedIndex; indicator.IndParam.ListParam[i].Text = ListParam[i].Text; indicator.IndParam.ListParam[i].Enabled = ListParam[i].Enabled; } // Numeric parameters for (int i = 0; i < 6; i++) { indicator.IndParam.NumParam[i].Value = (double)NumParam[i].Value; indicator.IndParam.NumParam[i].Enabled = NumParam[i].Enabled; } // Check parameters for (int i = 0; i < 2; i++) { indicator.IndParam.CheckParam[i].Checked = CheckParam[i].Checked; indicator.IndParam.CheckParam[i].Enabled = CheckParam[i].Enabled; indicator.IndParam.CheckParam[i].Enabled = CheckParam[i].Text == "Use previous bar value" || CheckParam[i].Enabled; } if (!CalculateIndicator(slotType, indicator)) { return; } if (bCalculateStrategy) { //Sets Data.Strategy Data.Strategy.Slot[slot].IndicatorName = indicator.IndicatorName; Data.Strategy.Slot[slot].IndParam = indicator.IndParam; Data.Strategy.Slot[slot].Component = indicator.Component; Data.Strategy.Slot[slot].SeparatedChart = indicator.SeparatedChart; Data.Strategy.Slot[slot].SpecValue = indicator.SpecialValues; Data.Strategy.Slot[slot].MinValue = indicator.SeparatedChartMinValue; Data.Strategy.Slot[slot].MaxValue = indicator.SeparatedChartMaxValue; Data.Strategy.Slot[slot].IsDefined = true; // Search the indicators' components to determine Data.FirstBar Data.FirstBar = Data.Strategy.SetFirstBar(); // Check "Use previous bar value" if (Data.Strategy.AdjustUsePreviousBarValue()) { for (int i = 0; i < 2; i++) { if (indicator.IndParam.CheckParam[i].Caption == "Use previous bar value") { AChbCheck[i].Checked = Data.Strategy.Slot[slot].IndParam.CheckParam[i].Checked; } } } Backtester.Calculate(); Backtester.CalculateAccountStats(); } SetIndicatorNotification(indicator); Data.IsResult = true; }
/// <summary> /// Calculates the balance lines /// </summary> private int Calculate(BackgroundWorker worker) { // Determine the number of lines // For each method per line // The random line shows the averaged values // Also we have two border lines for the random method // Plus the average balance line isRandom = false; minimum = float.MaxValue; maximum = float.MinValue; minimumRandom = float.MaxValue; maximumRandom = float.MinValue; var randomLines = (int)NumRandom.Value; checkedMethods = 0; lines = 1; for (int m = 0; m < countMethods; m++) { if (AchboxMethods[m].Checked) { checkedMethods++; lines++; if ((InterpolationMethod)AchboxMethods[m].Tag == InterpolationMethod.Random) { isRandom = true; } } } if (checkedMethods == 0 && Configs.PlaySounds) { SystemSounds.Hand.Play(); return(-1); } afBalance = new float[Data.Bars - Data.FirstBar]; afMethods = new float[countMethods, Data.Bars - Data.FirstBar]; if (isRandom) { afRandoms = new float[randomLines, Data.Bars - Data.FirstBar]; afMinRandom = new float[Data.Bars - Data.FirstBar]; afMaxRandom = new float[Data.Bars - Data.FirstBar]; } // Progress parameters int computedCycles = 0; int cycles = lines + (isRandom ? randomLines : 0); int highestPercentageReached = 0; int percentComplete; // Calculates the lines for (int m = 0; m < countMethods; m++) { if (worker.CancellationPending) { return(-1); } if (!AchboxMethods[m].Checked) { continue; } var method = (InterpolationMethod)AchboxMethods[m].Tag; if (method == InterpolationMethod.Random) { for (int r = 0; r < randomLines; r++) { if (worker.CancellationPending) { return(-1); } Backtester.InterpolationMethod = method; Backtester.Calculate(); if (Configs.AccountInMoney) { for (int iBar = 0; iBar < Data.Bars - Data.FirstBar; iBar++) { afRandoms[r, iBar] = (float)Backtester.MoneyBalance(iBar + Data.FirstBar); } } else { for (int iBar = 0; iBar < Data.Bars - Data.FirstBar; iBar++) { afRandoms[r, iBar] = Backtester.Balance(iBar + Data.FirstBar); } } // Report progress as a percentage of the total task. computedCycles++; percentComplete = 100 * computedCycles / cycles; percentComplete = percentComplete > 100 ? 100 : percentComplete; if (percentComplete > highestPercentageReached) { highestPercentageReached = percentComplete; worker.ReportProgress(percentComplete); } } for (int iBar = 0; iBar < Data.Bars - Data.FirstBar; iBar++) { float randomSum = 0; float minRandom = float.MaxValue; float maxRandom = float.MinValue; for (int r = 0; r < randomLines; r++) { float value = afRandoms[r, iBar]; randomSum += value; minRandom = value < minRandom ? value : minRandom; maxRandom = value > maxRandom ? value : maxRandom; } afMethods[m, iBar] = randomSum / randomLines; afMinRandom[iBar] = minRandom; afMaxRandom[iBar] = maxRandom; minimumRandom = minRandom < minimumRandom ? minRandom : minimumRandom; maximumRandom = maxRandom > maximumRandom ? maxRandom : maximumRandom; } // Report progress as a percentage of the total task. computedCycles++; percentComplete = 100 * computedCycles / cycles; percentComplete = percentComplete > 100 ? 100 : percentComplete; if (percentComplete > highestPercentageReached) { highestPercentageReached = percentComplete; worker.ReportProgress(percentComplete); } } else { Backtester.InterpolationMethod = method; Backtester.Calculate(); if (Configs.AccountInMoney) { for (int iBar = 0; iBar < Data.Bars - Data.FirstBar; iBar++) { afMethods[m, iBar] = (float)Backtester.MoneyBalance(iBar + Data.FirstBar); } } else { for (int iBar = 0; iBar < Data.Bars - Data.FirstBar; iBar++) { afMethods[m, iBar] = Backtester.Balance(iBar + Data.FirstBar); } } // Report progress as a percentage of the total task. computedCycles++; percentComplete = 100 * computedCycles / cycles; percentComplete = percentComplete > 100 ? 100 : percentComplete; if (percentComplete > highestPercentageReached) { highestPercentageReached = percentComplete; worker.ReportProgress(percentComplete); } } } // Calculates the average balance, Min and Max for (int bar = 0; bar < Data.Bars - Data.FirstBar; bar++) { float sum = 0; for (int m = 0; m < countMethods; m++) { if (!AchboxMethods[m].Checked) { continue; } float value = afMethods[m, bar]; sum += value; if (value < minimum) { minimum = value; } if (value > maximum) { maximum = value; } } afBalance[bar] = sum / checkedMethods; } // Report progress as a percentage of the total task. computedCycles++; percentComplete = 100 * computedCycles / cycles; percentComplete = percentComplete > 100 ? 100 : percentComplete; if (percentComplete > highestPercentageReached) { worker.ReportProgress(percentComplete); } return(0); }
private void CalculateStrategy() { Backtester.Calculate(); Backtester.CalculateAccountStats(); }