{"record":{"id":"e4c533ad887279bb","repo":"StockSharp/StockSharp","slug":"security-is-not-specified-e4c533","errorCode":null,"errorMessage":"Security is not specified.","messagePattern":"Security is not specified\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Algo.Strategies/Optimization/GeneticOptimizer.cs","lineNumber":28,"sourceCode":"\tprivate class StrategyFitness : IAsyncFitness, IFitness\n\t{\n\t\tprivate readonly GeneticOptimizer _optimizer;\n\t\tprivate readonly Strategy _strategy;\n\t\tprivate readonly Func<Strategy, decimal> _calcFitness;\n\t\tprivate readonly DateTime _startTime;\n\t\tprivate readonly DateTime _stopTime;\n\t\tprivate readonly SynchronizedDictionary<DynamicTuple, decimal> _cache = [];\n\n\t\tpublic StrategyFitness(GeneticOptimizer optimizer, Strategy strategy, Func<Strategy, decimal> calcFitness, DateTime startTime, DateTime stopTime)\n\t\t{\n\t\t\t_optimizer = optimizer ?? throw new ArgumentNullException(nameof(optimizer));\n\t\t\t_strategy = strategy ?? throw new ArgumentNullException(nameof(strategy));\n\t\t\t_calcFitness = calcFitness ?? throw new ArgumentNullException(nameof(calcFitness));\n\t\t\t_startTime = startTime;\n\t\t\t_stopTime = stopTime;\n\n\t\t\tif (_strategy.Security is null)\n\t\t\t\tthrow new ArgumentException(LocalizedStrings.SecurityNotSpecified, nameof(strategy));\n\n\t\t\tif (_strategy.Portfolio is null)\n\t\t\t\tthrow new ArgumentException(LocalizedStrings.PortfolioNotSpecified, nameof(strategy));\n\t\t}\n\n\t\tdouble IFitness.Evaluate(IChromosome chromosome)\n\t\t\t=> throw new NotSupportedException();\n\n\t\tasync Task<double> IAsyncFitness.EvaluateAsync(IChromosome chromosome, CancellationToken cancellationToken)\n\t\t{\n\t\t\tvar spc = (StrategyParametersChromosome)chromosome;\n\n\t\t\tStrategy strategy;\n\n\t\t\tusing (new Scope<StrategyContext>(new() { ExcludeUI = true }))\n\t\t\t\tstrategy = _strategy.Clone();\n\n\t\t\tstrategy.Security = _strategy.Security;","sourceCodeStart":10,"sourceCodeEnd":46,"githubUrl":"https://github.com/StockSharp/StockSharp/blob/601a191de678bff83da28b14828f8885214ca71c/Algo.Strategies/Optimization/GeneticOptimizer.cs#L10-L46","documentation":"The StrategyFitness constructor (inner class of GeneticOptimizer) requires the strategy template to have a Security assigned. The optimizer clones the strategy for each chromosome evaluation, and every clone inherits the Security — if the template's Security is null, all evaluations would fail. The guard surfaces this configuration error at optimizer setup time with an ArgumentException naming the 'strategy' parameter.","triggerScenarios":"Passing a strategy to the GeneticOptimizer where strategy.Security has not been set. The strategy template was loaded from configuration without resolving the security.","commonSituations":"Configuring the genetic optimizer from code and forgetting to set Security on the template strategy. Loading a strategy from XML/JSON where the SecurityId was invalid and resolved to null. Using a newly constructed strategy without completing initialization.","solutions":["Set strategy.Security to a valid Security object before passing the strategy to the GeneticOptimizer.","Resolve the SecurityId through the connector's LookupSecurityById before constructing the optimizer.","Validate strategy.Security != null in a pre-optimization check."],"exampleFix":"// before\nvar optimizer = new GeneticOptimizer(strategyWithoutSecurity, ...);\n\n// after\nstrategy.Security = connector.LookupSecurityById(securityId);\nvar optimizer = new GeneticOptimizer(strategy, ...);","handlingStrategy":"validation","validationCode":"if (strategy.Security is null)\n    throw new InvalidOperationException(\n        \"Set strategy.Security before creating the GeneticOptimizer.\");\n\nvar optimizer = new GeneticOptimizer(strategy, ...);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Resolve Security through the connector before passing the strategy to the optimizer.","Implement a pre-optimization validation that checks all required strategy properties.","Use a builder pattern that enforces Security as required."],"tags":["optimization","genetic","configuration","security"],"backgroundTag":null,"analyzedSha":"601a191de678bff83da28b14828f8885214ca71c","analyzedAt":"2026-08-13T20:43:24.460Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}