StockSharp/StockSharp · warning · ArgumentOutOfRangeException

nameof(parameters)

Error message

nameof(parameters)

What it means

The GpuSmaCalculator.Calculate method throws ArgumentOutOfRangeException(nameof(parameters)) when the parameters array is empty. This is the wrong exception type for empty-collection validation — ArgumentOutOfRangeException is intended for scalar values outside an allowed range. The idiomatic .NET choice is ArgumentException with a message. The single-argument constructor provides no descriptive error text.

Source

Thrown at Algo.Gpu/Indicators/GpuSmaCalculator.cs:65

	/// <param name="accelerator">ILGPU accelerator.</param>
	public GpuSmaCalculator(Context context, Accelerator accelerator)
		: base(context, accelerator)
	{
		_paramsSeriesKernel = Accelerator.LoadAutoGroupedStreamKernel
			<Index3D, ArrayView<GpuCandle>, ArrayView<GpuIndicatorResult>, ArrayView<int>, ArrayView<int>, ArrayView<GpuSmaParams>>(SmaParamsSeriesKernel);
	}

	/// <inheritdoc />
	public override GpuIndicatorResult[][][] Calculate(GpuCandle[][] candlesSeries, GpuSmaParams[] parameters)
	{
		ArgumentNullException.ThrowIfNull(candlesSeries);
		ArgumentNullException.ThrowIfNull(parameters);

		if (candlesSeries.Length == 0)
			throw new ArgumentOutOfRangeException(nameof(candlesSeries));

		if (parameters.Length == 0)
			throw new ArgumentOutOfRangeException(nameof(parameters));

		var seriesCount = candlesSeries.Length;

		// Flatten input
		var totalSize = 0;
		var seriesOffsets = new int[seriesCount];
		var seriesLengths = new int[seriesCount];

		for (var s = 0; s < seriesCount; s++)
		{
			seriesOffsets[s] = totalSize;
			var len = candlesSeries[s]?.Length ?? 0;
			seriesLengths[s] = len;
			totalSize += len;
		}

		var flatCandles = new GpuCandle[totalSize];
		var maxLen = 0;

View on GitHub (pinned to 601a191de6)

Solutions

  1. Replace ArgumentOutOfRangeException with ArgumentException and a descriptive message: throw new ArgumentException("Parameters array must not be empty.", nameof(parameters));
  2. Validate the params collection upstream and skip the call when empty.
  3. Document the non-empty contract on the base class method.

Example fix

// before
if (parameters.Length == 0)
    throw new ArgumentOutOfRangeException(nameof(parameters));

// after
if (parameters.Length == 0)
    throw new ArgumentException("Parameters array must not be empty.", nameof(parameters));
Defensive patterns

Strategy: validation

Validate before calling

// Validate before calling Calculate to avoid the exception entirely
if (parameters is null || parameters.Length == 0)
{
    return Array.Empty<GpuIndicatorResult[][]>();
}
var results = calculator.Calculate(candlesSeries, parameters);

Type guard

static bool IsValidParams<TParam>(TParam[] parameters) where TParam : struct
    => parameters is { Length: > 0 };

Prevention

When it happens

Trigger: Calling GpuSmaCalculator.Calculate(candlesSeries, parameters) where parameters is Array.Empty<GpuSmaParams>(). Null guard passes; parameters.Length == 0 throws at line 65.

Common situations: Dynamic param-set builders filtering to zero entries. Deserialization producing empty arrays. Test edge cases. Config-driven selection with no SMA variant active.

Related errors


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