From 946ab8cec577b6a54fcf7e46f535b3dc66e284f8 Mon Sep 17 00:00:00 2001 From: mkzung <103102868+mkzung@users.noreply.github.com> Date: Wed, 19 Aug 2026 18:52:28 +0500 Subject: [PATCH] Reject a ChoppinessIndex period of one ComputeNextValue divides by Math.Log10(_period). At a period of one that divisor is zero, so the double is infinite and the cast to decimal throws OverflowException on the first update after warm-up. The constructor accepts the value, so the failure lands on the data rather than on the call that is wrong. Beta, Correlation, Covariance and ValueAtRisk all reject a period their formula cannot use, with the same message shape. --- Indicators/ChoppinessIndex.cs | 9 +++++++-- Tests/Indicators/ChoppinessIndexTests.cs | 10 ++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/Indicators/ChoppinessIndex.cs b/Indicators/ChoppinessIndex.cs index b85b68affd4a..18c94199ed8d 100644 --- a/Indicators/ChoppinessIndex.cs +++ b/Indicators/ChoppinessIndex.cs @@ -46,10 +46,15 @@ public class ChoppinessIndex : BarIndicator, IIndicatorWarmUpPeriodProvider /// Creates a new ChoppinessIndex indicator using the specified period and moving average type /// /// The name of this indicator - /// The period used for rolling windows for highs and lows + /// The period used for rolling windows for highs and lows, must be greater than one public ChoppinessIndex(string name, int period) : base(name) { + if (period < 2) + { + throw new ArgumentException($"Period parameter for ChoppinessIndex indicator must be greater than 1 but was {period}"); + } + _period = period; _trueRange = new TrueRange(); @@ -64,7 +69,7 @@ public ChoppinessIndex(string name, int period) /// /// Creates a new ChoppinessIndex indicator using the specified period /// - /// The period used for rolling windows for highs and lows + /// The period used for rolling windows for highs and lows, must be greater than one public ChoppinessIndex(int period) : this($"CHOP({period})", period) { diff --git a/Tests/Indicators/ChoppinessIndexTests.cs b/Tests/Indicators/ChoppinessIndexTests.cs index 48f20692606b..a3a07ad62591 100644 --- a/Tests/Indicators/ChoppinessIndexTests.cs +++ b/Tests/Indicators/ChoppinessIndexTests.cs @@ -13,6 +13,7 @@ * limitations under the License. */ +using System; using NUnit.Framework; using QuantConnect.Data.Market; using QuantConnect.Indicators; @@ -32,5 +33,14 @@ protected override IndicatorBase CreateIndicator() protected override string TestFileName => "spy_with_chop.csv"; protected override string TestColumnName => "CHOP14"; + + [Test] + public void PeriodBelowMinimumThrows() + { + var period = 1; + + var exception = Assert.Throws(() => new ChoppinessIndex(period)); + Assert.That(exception.Message, Is.EqualTo($"Period parameter for ChoppinessIndex indicator must be greater than 1 but was {period}")); + } } }