define()
Declare your strategy parameters. Called once when the strategy loads.
Signature
function define(ctx)
Parameters
| Name | Type | Description |
|---|---|---|
ctx | DefineContext | Context for parameter declaration |
ctx.param()
Use ctx.param() to declare each parameter:
ctx.param(name, options)
Parameter Options
| Option | Type | Required | Description |
|---|---|---|---|
type | string | Yes | 'int', 'float', 'string', 'boolean' |
label | string | No | Display label in UI |
description | string | No | Tooltip text |
default | any | Yes | Default value |
min | number | No | Minimum (int/float) |
max | number | No | Maximum (int/float) |
step | number | No | Step size for sliders/optimization |
optimize | boolean | No | Include in optimization runs |
group | string | No | UI grouping |
options | array | No | For string type: enum values |
Example
function define(ctx) { ctx.param('fastLength', { type: 'int', label: 'Fast EMA', description: 'Period for the fast EMA', default: 9, min: 5, max: 50, step: 1, optimize: true, group: 'Trend' }); ctx.param('slowLength', { type: 'int', label: 'Slow EMA', default: 21, min: 10, max: 200, optimize: true, group: 'Trend' }); ctx.param('riskPercent', { type: 'float', label: 'Risk %', default: 0.02, min: 0.01, max: 0.10, step: 0.01, group: 'Risk' }); ctx.param('direction', { type: 'string', label: 'Trade Direction', default: 'long', options: ['long', 'short', 'both'] }); }
Parameter Types
int
Integer numbers. Use for periods, counts, lookbacks.
ctx.param('period', { type: 'int', default: 14, min: 2, max: 100 });
float
Decimal numbers. Use for percentages, multipliers.
ctx.param('threshold', { type: 'float', default: 0.5, min: 0.1, max: 1.0, step: 0.1 });
string
Text or enum values.
ctx.param('mode', { type: 'string', default: 'aggressive', options: ['conservative', 'moderate', 'aggressive'] });
boolean
True/false toggles.
ctx.param('useFilter', { type: 'boolean', default: true, label: 'Use Trend Filter' });
Accessing Parameters
Parameters are available in init() and onBar() via ctx.p:
function init(ctx) { ctx.indicator('ema', 'EMA', { period: ctx.p.fastLength }); } function onBar(ctx, i) { if (ctx.p.useFilter && ctx.ind.ema[i] > ctx.series.close[i]) { return; // Skip if filter is active and price below EMA } }
Related
- init() — Indicator declaration
- onBar() — Trading logic
- Strategy Lifecycle — Execution order
lifecycledefineparameterssetupqsl