Skip to content

ATR

ATR

At a glance — Average True Range (Wilder smoothing): a volatility measure. Returns a Series of positive values in the symbol’s price unit. Most often used to size stops that adapt to current volatility.

Signature

const atr: Series = api.use(ATR, { period: 14 });
FieldTypeDescription
periodnumber (≥ 1)Smoothing window. Standard is 14.

Warm-up: period + 1 bars.

Example

indicators/atr-stop
import { ATR, defineStrategy } from '@nexpips/sdk-trading';
/**
* Dimensionner le stop avec la volatilité : un stop-loss exprimé en
* multiples d'ATR (`{ type: 'atr' }`) s'élargit quand le marché est agité
* et se resserre quand il est calme.
*/
export default defineStrategy({
symbol: 'EURUSD',
timeframe: 'H1',
risk: { maxRiskPercentPerTrade: 1, maxOpenPositions: 1, maxDailyLossPercent: 5 },
setup: (api) => {
const atr = api.use(ATR, { period: 14 });
return {
onBar(ctx) {
if (!ctx.position.isFlat || ctx.position.hasPendingOrder) return;
// N'entrer que si la volatilité courante est mesurable.
if (atr.at(0) <= 0) return;
ctx.order.marketBuy({
riskPercent: 1,
stopLoss: { type: 'atr', period: 14, multiple: 2 },
takeProfit: { type: 'rr', value: 2 },
});
},
};
},
});

Size the stop-loss at 2×ATR.

See also