Parameters
Parameters
Tunable inputs (periods, risk multiples, toggles, labels, file paths). The numeric ones can be swept by the optimizer; the host applies every value before your logic runs.
Parameters
A strategy parameter is an input the host supplies before the run starts: a number, a flag, a string or a
file path. Declare it in OnInitialize with one of the *Parameter helpers, store
the returned handle in a field, and read .Value where you need it.
IntParameter and DoubleParameter each have an overload taking
(name, default, min, max, step). That optimizable form lets the Strategy Analyzer
sweep the value across the range during a parameter search. The (name, default) overloads,
BoolParameter, StringParameter and FilePathParameter are fixed
configuration: the user sets them and the optimizer leaves them alone.
C#private StrategyParameter<int> _period = null!, _maxPos = null!;
private StrategyParameter<double> _riskR = null!;
private StrategyParameter<bool> _useTrail = null!;
private StrategyParameter<string> _label = null!, _plan = null!;
protected override void OnInitialize()
{
_period = IntParameter("Period", 20, 5, 200, 1); // optimizable int
_riskR = DoubleParameter("RiskR", 2.0, 0.5, 5.0, 0.5); // optimizable double
_maxPos = IntParameter("MaxPositions", 3); // fixed int
_useTrail = BoolParameter("UseTrail", true); // checkbox
_label = StringParameter("Label", "swing"); // free text
_plan = FilePathParameter("Plan", "", "Plans (*.json)|*.json"); // path + Browse
}
protected override void OnBar()
{
int n = _period.Value; // or: int n = _period; (implicit conversion to T)
if (_useTrail.Value) { /* ... */ }
}
| Helper | Type | Optimizable | Editor |
|---|---|---|---|
IntParameter(name, default, min, max, step) |
int |
Yes | Numeric |
IntParameter(name, default) |
int |
No | Numeric |
DoubleParameter(name, default, min, max, step) |
double |
Yes | Numeric |
DoubleParameter(name, default) |
double |
No | Numeric |
BoolParameter(name, default) |
bool |
No | Checkbox |
StringParameter(name, default) |
string |
No | Textbox |
FilePathParameter(name, default, fileFilter) |
string |
No | Path + Browse |
fileFilter is a standard file-dialog filter such as
"Plans (*.json)|*.json|All files (*.*)|*.*". At run time a file-path parameter behaves like
a string parameter; only the editor differs. A null string default is stored as an empty string.
Every declaration helper returns the parameter, and WithFormat on it sets how the
parameter's row is drawn in a settings list: bold for a bold label,
labelSize: ParameterLabelSize.Large for a label one size larger, and
separatorBefore for a thin line above the row. All three are optional and can be
combined. Formatting changes the look of the row only; the value and an optimization run are the
same with or without it.
Formatting.cs_riskR = DoubleParameter("RiskR", 2.0, 0.5, 5.0, 0.5)
.WithFormat(bold: true, labelSize: ParameterLabelSize.Large);
_plan = FilePathParameter("Plan", "", "Plans (*.json)|*.json")
.WithFormat(separatorBefore: true);
OnInitialize declares each parameter at its default.
The host then applies the run's overrides and calls OnStart; the optimizer runs each pass with
a different set of overrides. An override whose name matches no parameter, or whose value cannot be
converted to the parameter's type, is logged and skipped, and the parameter keeps its default. Parameters
are inputs: Value has no public setter.Optimization ranges
The optimizable overloads build an OptimizationRange(Min, Max, Step), a record stored in
decimal so steps such as 0.5 or 0.25 stay exact across a sweep. The parameter's constructor
calls Validated(name), which throws ArgumentException for a backwards range
(Min > Max) or a step that is not positive, so a bad range fails at declaration. The host
reads the range through the parameter's IsOptimizable and OptimizationRange
properties.
The parameter handle & the set
Each helper returns a typed StrategyParameter<T> with Value,
DefaultValue, ValueType and an implicit conversion to T. Its
abstract base StrategyParameter is the untyped view the host enumerates: Name
(unique within a strategy, compared case-insensitively), IsOptimizable,
OptimizationRange, EditorKind (a ParameterEditorKind, the same enum
indicator parameters use: Auto by default, FilePicker for a path),
FileFilter and BoxedValue.
A strategy's declared parameters form a ParameterSet: All (declaration order),
Count, Find(name) and TrySetValue(name, value). The runner reaches it
through IStrategyHost.ParameterSet to apply overrides without knowing the strategy's type.
The host's launch form lists the same parameters (see
the catalog). Strategy code itself only reads its
own handles.
InvalidOperationException from the helper, so the bug surfaces at once
instead of one value silently shadowing the other.