StockSharp/StockSharp · error · ArgumentOutOfRangeException

parameters

Error message

parameters

What it means

The Kaufman Adaptive Moving Average (KAMA) GPU calculator's Calculate method throws ArgumentOutOfRangeException(nameof(parameters)) when the GpuKamaParams[] array has zero elements. The kernel grid extent is constructed as Index2D(parameters.Length, seriesCount), so a zero-length parameters array creates a degenerate grid. Additionally the output buffer size equals totalSize * parameters.Length, which would be zero — making the entire computation meaningless. The guard fires before any GPU allocation.

Source

Thrown at Algo.Gpu/Indicators/GpuKamaCalculator.cs:78

	/// <param name="context">ILGPU context.</param>
	/// <param name="accelerator">ILGPU accelerator.</param>
	public GpuKamaCalculator(Context context, Accelerator accelerator)
		: base(context, accelerator)
	{
		_kernel = Accelerator.LoadAutoGroupedStreamKernel
			<Index2D, ArrayView<GpuCandle>, ArrayView<GpuIndicatorResult>, ArrayView<int>, ArrayView<int>, ArrayView<GpuKamaParams>>(KamaParamsSeriesKernel);
	}

	/// <inheritdoc />
	public override GpuIndicatorResult[][][] Calculate(GpuCandle[][] candlesSeries, GpuKamaParams[] 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 offset = 0;

View on GitHub (pinned to 601a191de6)

Solutions

  1. Inspect the GpuKamaParams[] source before the call — log parameters.Length to confirm whether the array is genuinely empty or never populated.
  2. Ensure at least one parameter set exists: if the application requires a default, seed parameters with the indicator's canonical default {params_type} instance.
  3. Guard the caller: if parameters is null or has zero length, skip or short-circuit before calling Calculate.
  4. Check the configuration/deserialization pipeline — verify the JSON/YAML schema maps parameters correctly and the array is not silently omitted or mapped to a null/empty default.

Example fix

// before
var results = calc.Calculate(candlesSeries, parameters); // parameters may be empty

// after
if (parameters is null || parameters.Length == 0)
    parameters = new[] { new GpuKamaParams() }; // canonical default
var results = calc.Calculate(candlesSeries, parameters);
Defensive patterns

Strategy: validation

Validate before calling

// Validate parameters before calling Calculate
if (parameters is null || parameters.Length == 0)
    throw new ArgumentException("At least one GpuKamaParams set is required.", nameof(parameters));

var results = calc.Calculate(candlesSeries, parameters);

Type guard

static bool HasValidParameters(GpuKamaParams[] parameters)
    => parameters is not null && parameters.Length > 0;

// usage
if (!HasValidParameters(parameters))
    parameters = new[] { new GpuKamaParams() }; // apply default
var results = calc.Calculate(candlesSeries, parameters);

Try / catch

// Wrap if the empty-parameters case is a recoverable runtime condition
try
{
    var results = calc.Calculate(candlesSeries, parameters);
    // process results ...
}
catch (ArgumentOutOfRangeException ex) when (ex.ParamName == "parameters")
{
    logger.LogWarning("No parameters provided for Kaufman Adaptive Moving Average (KAMA); applying default");
    var defaultParams = new[] { new GpuKamaParams() };
    var results = calc.Calculate(candlesSeries, defaultParams);
}

Prevention

When it happens

Trigger: Calling Kaufman Adaptive Moving Average (KAMA)Calculator.Calculate(candlesSeries, Array.Empty<GpuKamaParams>()) or passing new GpuKamaParams[0]. Also occurs when parameters originate from a parameter-sweep or configuration builder that yields zero entries (e.g., a deserialized config has an empty parameters array, or a sweep filter removed all candidate sets).

Common situations: Configuration file or JSON payload defines the indicator but leaves its parameters array empty or omitted. A parameter-optimization sweep filtered out all candidates by a constraint (e.g., min/max period bounds). Default-parameters fallback returned an empty array due to a missing or misconfigured defaults provider. A UI or CLI layer did not populate parameter sets before dispatching the batch calculation.

Related errors


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