Skip to content

BollingerBands

BollingerBands

At a glancemiddle = SMA(source, period); the upper/lower bands sit stdDev population standard deviations away from the middle. The series yields a BollingerBandsOutput per bar. Population SD (divisor = N), consistent with TradingView / MQL5.

Signature

const bb: Series<BollingerBandsOutput> = api.use(BollingerBands, {
source: 'close',
period: 20,
stdDev: 2,
});
FieldTypeDescription
periodnumber (≥ 1)Window for the moving average and SD.
sourceSourcePrice source.
stdDevnumberBand width in standard deviations (e.g. 2).

Warm-up: period bars.

Example

indicators/bollinger-reversal
import { BollingerBands, defineStrategy } from '@nexpips/sdk-trading';
/**
* Retour à la moyenne sur bandes de Bollinger. La série renvoie un
* `BollingerBandsOutput` ({ upper, middle, lower }). Achat quand la clôture
* passe sous la bande basse ; take-profit visé sur la moyenne.
*/
export default defineStrategy({
symbol: 'EURUSD',
timeframe: 'M15',
risk: { maxRiskPercentPerTrade: 1, maxOpenPositions: 1, maxDailyLossPercent: 5 },
setup: (api) => {
const bb = api.use(BollingerBands, { source: 'close', period: 20, stdDev: 2 });
return {
onBar(ctx) {
if (!ctx.position.isFlat || ctx.position.hasPendingOrder) return;
const band = bb.at(0);
if (ctx.series.close.at(0) < band.lower) {
ctx.order.marketBuy({
riskPercent: 1,
stopLoss: { type: 'atr', period: 14, multiple: 2 },
takeProfit: { type: 'price', value: band.middle },
});
}
},
};
},
});

Mean-reversion: buy below the lower band, target the middle.

See also