StockSharp/StockSharp · error · ArgumentOutOfRangeException
Random count must be positive.
Error message
Random count must be positive.
What it means
Thrown by ToBruteForceRandom when randomCount <= 0. randomCount is the number of random samples drawn per optimizable parameter; a non-positive count produces no samples, so it is rejected with ArgumentOutOfRangeException.
Source
Thrown at Algo.Strategies/StrategyParamHelper.cs:674
/// <returns>Lazy enumerable of strategy clones with their parameter arrays.</returns>
public static IEnumerable<(Strategy strategy, IStrategyParam[] parameters)> ToBruteForceRandom(
this Strategy strategy,
IStrategyParam[] parameters,
int randomCount,
out IStrategyParam[] optimizedParams,
out int totalCount)
{
if (strategy is null)
throw new ArgumentNullException(nameof(strategy));
if (parameters is null)
throw new ArgumentNullException(nameof(parameters));
if (parameters.Length == 0)
throw new ArgumentOutOfRangeException(nameof(parameters), "No optimization parameters.");
if (randomCount <= 0)
throw new ArgumentOutOfRangeException(nameof(randomCount), "Random count must be positive.");
var singleParams = new List<IStrategyParam>();
var optimizeDict = new Dictionary<string, (IStrategyParam param, HashSet<object> values)>();
foreach (var param in parameters)
{
if (!param.CanOptimize || param.OptimizeFrom is null || param.OptimizeTo is null)
{
singleParams.Add(param);
continue;
}
var values = new HashSet<object>();
for (var i = 0; i < randomCount; i++)
{
var randomValue = param.GetRandom(strategy.RandomProvider);
if (randomValue is not null)View on GitHub (pinned to 601a191de6)
Solutions
- Pass a positive integer for randomCount (e.g. at least 1; typically tens or hundreds for meaningful coverage).
- Clamp/validate the source value before calling: randomCount = Math.Max(1, configuredCount).
- Bind the UI input to a numeric up-down with a minimum of 1.
Example fix
// before var runs = strategy.ToBruteForceRandom(opt, configuredSamples, out _, out _); // configuredSamples == 0 -> throws // after var samples = Math.Max(1, configuredSamples); var runs = strategy.ToBruteForceRandom(opt, samples, out _, out _);
Defensive patterns
Strategy: validation
Validate before calling
var samples = Math.Max(1, configuredSampleCount); strategy.ToBruteForceRandom(opt, samples, out _, out _);
Prevention
- Bind sample-size inputs to a numeric control with minimum 1.
- Clamp computed sample counts to at least 1 before passing.
When it happens
Trigger: Calling strategy.ToBruteForceRandom(parameters, randomCount, ...) with randomCount set to 0 or a negative value — e.g. a UI/config value that defaulted to 0 or was parsed from an empty input.
Common situations: User-facing 'sample size' field left blank or set to 0; randomCount derived from a calculation (e.g. budget / cost) that rounded down to 0; passing an uninitialized int field.
Related errors
- No optimization parameters.
- No any params for optimize.
- Count must be positive.
- Specified argument was out of the range of valid values. (Pa
- Specified argument was out of the range of valid values. (Pa
AI-assisted analysis of StockSharp/StockSharp@601a191de6 (2026-08-13).
Data as JSON: /api/errors/06f6365d34e6e326.
Report an issue: GitHub.