Skip to content

defineIndicator

defineIndicator

At a glance — Define a custom indicator. update(ctx) is called once per closed bar and returns the output value; the engine accumulates returns into a Series exposed to your strategy via api.use(indicator, params).

Signature

function defineIndicator<TParams, TOutput = number>(def: {
name: string;
warmup?: (params: TParams) => number;
update: (ctx: IndicatorContext<TParams>) => TOutput;
}): IndicatorDef<TParams, TOutput>;
FieldDescription
nameStable identifier, used in the manifest and logs.
warmupOptional. Bars to skip before the value is meaningful (default 1).
updateCalled per closed bar; returns the indicator value for that bar.

Example

indicators/custom-indicator
import { defineIndicator, defineStrategy } from '@nexpips/sdk-trading';
/**
* Indicateur custom via `defineIndicator` : l'amplitude (range) de la barre
* courante. `update` est appelé une fois par barre clôturée et reçoit un
* `IndicatorContext`. L'engine accumule les retours dans une `Series`.
*/
const BarRange = defineIndicator<{ history?: number }, number>({
name: 'BarRange',
warmup: () => 1,
update: (ctx) => ctx.series.high.at(0) - ctx.series.low.at(0),
});
export default defineStrategy({
symbol: 'EURUSD',
timeframe: 'H1',
risk: { maxRiskPercentPerTrade: 1, maxOpenPositions: 1, maxDailyLossPercent: 5 },
setup: (api) => {
const range = api.use(BarRange, {});
return {
onBar(ctx) {
if (!ctx.position.isFlat || ctx.position.hasPendingOrder) return;
if (range.at(0) > 0) {
ctx.order.marketBuy({
riskPercent: 1,
stopLoss: { type: 'pips', value: 20 },
takeProfit: { type: 'rr', value: 2 },
});
}
},
};
},
});

A custom bar-range indicator.

See also