StockSharp/StockSharp · error · ArgumentOutOfRangeException

LocalizedStrings.InvalidValue

Error message

LocalizedStrings.InvalidValue

What it means

MassIndex.EmaLength sets the period on both the inner single and double EMAs that the Mass Index accumulates. Each EMA needs at least 2 points, so the setter throws ArgumentOutOfRangeException with LocalizedStrings.InvalidValue for value < 2 before resetting both inner EMAs and the sum.

Source

Thrown at Algo.Indicators/MassIndex.cs:46

		_sum = new();
		Length = 25;
	}

	/// <summary>
	/// <see cref="ExponentialMovingAverage"/>
	/// </summary>
	[Display(
		ResourceType = typeof(LocalizedStrings),
		Name = LocalizedStrings.EMAKey,
		Description = LocalizedStrings.ExponentialMovingAverageKey,
		GroupName = LocalizedStrings.GeneralKey)]
	public int EmaLength
	{
		get => _singleEma.Length;
		set
		{
			if (value < 2)
				throw new ArgumentOutOfRangeException(nameof(value), value, LocalizedStrings.InvalidValue);

			_singleEma.Length = _doubleEma.Length = value;
			Reset();
		}
	}

	/// <inheritdoc />
	public override int NumValuesToInitialize
		=> _singleEma.NumValuesToInitialize + _sum.NumValuesToInitialize - 1;

	/// <inheritdoc />
	public override IndicatorMeasures Measure => IndicatorMeasures.MinusOnePlusOne;

	/// <inheritdoc />
	protected override bool CalcIsFormed() => _sum.IsFormed;

	/// <inheritdoc />
	protected override decimal? OnProcessDecimal(IIndicatorValue input)

View on GitHub (pinned to 601a191de6)

Solutions

  1. Set EmaLength to at least 2 (standard Mass Index uses 9).
  2. Clamp: `mi.EmaLength = Math.Max(2, raw)` before assigning.
  3. Validate the bound at the input/config boundary.

Example fix

// before
mi.EmaLength = 1;
// after
mi.EmaLength = 9;
Defensive patterns

Strategy: validation

Validate before calling

static int SanitizeEmaLength(int v) => Math.Max(2, v);
// usage: mi.EmaLength = SanitizeEmaLength(raw);

Type guard

static bool IsValidEmaLength(int v) => v >= 2;

Try / catch

try { mi.EmaLength = raw; }
catch (ArgumentOutOfRangeException) { mi.EmaLength = 9; }

Prevention

When it happens

Trigger: Setting MassIndex.EmaLength to 0 or 1, e.g. `mi.EmaLength = 1`. The conventional Mass Index EMAs use 9.

Common situations: UI period field below 2, deserialized config with a 1, or copying a period from a non-EMA indicator that allowed 1.

Related errors


AI-assisted analysis of StockSharp/StockSharp@601a191de6 (2026-08-13). Data as JSON: /api/errors/d07fc89c930a2b9d. Report an issue: GitHub.