/// <summary> /// Calculates the strategy. /// </summary> /// <param name="recalcIndicators">true - to recalculate all the indicators.</param> void Calculate(bool recalcIndicators) { bool isUPBVChanged = Data.Strategy.AdjustUsePreviousBarValue(); // Calculates the indicators by slots if it's necessary if (recalcIndicators) { foreach (IndicatorSlot indSlot in Data.Strategy.Slot) { string indicatorName = indSlot.IndicatorName; SlotTypes slotType = indSlot.SlotType; Indicator indicator = Indicator_Store.ConstructIndicator(indicatorName, slotType); indicator.IndParam = indSlot.IndParam; indicator.Calculate(slotType); indSlot.IndicatorName = indicator.IndicatorName; indSlot.IndParam = indicator.IndParam; indSlot.Component = indicator.Component; indSlot.SeparatedChart = indicator.SeparatedChart; indSlot.SpecValue = indicator.SpecialValues; indSlot.MinValue = indicator.SeparatedChartMinValue; indSlot.MaxValue = indicator.SeparatedChartMaxValue; indSlot.IsDefined = true; } } // Searches the indicators' components to determine the Data.FirstBar Data.FirstBar = Data.Strategy.SetFirstBar(); // Logging Data.Log("Calculate the strategy"); // Calculates the backtest Backtester.Calculate(); Backtester.CalculateAccountStats(); Data.IsResult = true; if (isUPBVChanged) { RebuildStrategyLayout(); } smallIndicatorChart.InitChart(); smallIndicatorChart.Invalidate(); smallBalanceChart.SetChartData(); smallBalanceChart.InitChart(); smallBalanceChart.Invalidate(); SetupJournal(); infpnlAccountStatistics.Update( Backtester.AccountStatsParam, Backtester.AccountStatsValue, Backtester.AccountStatsFlags, Language.T("Account Statistics")); return; }
/// <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(); }
/// <summary> /// Calculates the selected indicator. /// </summary> private void CalculateIndicator(bool bCalculateStrategy) { if (!Data.IsData || !Data.IsResult || !_isPaint) { return; } SetOppositeSignalBehaviour(); SetClosingLogicConditions(); Indicator indicator = IndicatorStore.ConstructIndicator(_indicatorName, _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; }