Skip to main content
BTC / USDT——ETH / USDT——SOL / USDT——BNB / USDT——XRP / USDT——DOGE / USDT——TON / USDT——AVAX / USDT——LINK / USDT——ADA / USDT——TRX / USDT——DOT / USDT——BTC / USDT——ETH / USDT——SOL / USDT——BNB / USDT——XRP / USDT——DOGE / USDT——TON / USDT——AVAX / USDT——LINK / USDT——ADA / USDT——TRX / USDT——DOT / USDT——
Harga

Bollinger Bands

Volatility bands around a moving average — MT4, MT5, TradeStation and TradingView code.

A moving-average basis wrapped by an upper and lower band set a number of standard deviations away. The bands widen when volatility rises and contract when it falls; price tagging or riding a band signals stretch or trend strength. Copy the version for your platform.

MQL4 · BollingerBands.mq4
#property indicator_chart_window
#property indicator_buffers 3

extern int BB_Period = 20;
extern double BB_Dev = 2.0;
double BasisBuf[], UpperBuf[], LowerBuf[];

int OnInit() {
  SetIndexBuffer(0, BasisBuf);
  SetIndexBuffer(1, UpperBuf);
  SetIndexBuffer(2, LowerBuf);
  return 0;
}

int OnCalculate(const int rates_total, const int prev_calculated, const double &close[]) {
  for (int i = prev_calculated; i < rates_total; i++) {
    double ma = iMA(NULL, 0, BB_Period, 0, MODE_SMA, PRICE_CLOSE, i);
    double sd = iStdDev(NULL, 0, BB_Period, 0, MODE_SMA, PRICE_CLOSE, i);
    BasisBuf[i] = ma;
    UpperBuf[i] = ma + BB_Dev * sd;
    LowerBuf[i] = ma - BB_Dev * sd;
  }
  return rates_total;
}
MQL5 · BollingerBands.mq5
#property indicator_chart_window
#property indicator_buffers 3

input int BB_Period = 20;
input double BB_Dev = 2.0;
double BasisBuf[], UpperBuf[], LowerBuf[];

int OnInit() {
  SetIndexBuffer(0, BasisBuf);
  SetIndexBuffer(1, UpperBuf);
  SetIndexBuffer(2, LowerBuf);
  return INIT_SUCCEEDED;
}

int OnCalculate(const int rates_total, const int prev_calculated,
  const datetime &time[], const double &open[], const double &high[],
  const double &low[], const double &close[], const long &tick_volume[],
  const long &volume[], const int &spread[]) {
  for (int i = prev_calculated; i < rates_total; i++) {
    double ma = iMA(NULL, 0, BB_Period, 0, MODE_SMA, PRICE_CLOSE, i);
    double sd = iStdDev(NULL, 0, BB_Period, 0, MODE_SMA, PRICE_CLOSE, i);
    BasisBuf[i] = ma;
    UpperBuf[i] = ma + BB_Dev * sd;
    LowerBuf[i] = ma - BB_Dev * sd;
  }
  return rates_total;
}
EasyLanguage
inputs: Length(20), NumDevs(2);
variables: Mid(0), Upper(0), Lower(0);
Mid = Average(Close, Length);
Upper = BBandTop(Close, Length, NumDevs);
Lower = BBandBot(Close, Length, NumDevs);
Plot1(Mid, "Basis");
Plot2(Upper, "Upper");
Plot3(Lower, "Lower");
Pine Script
//@version=5
indicator("Bollinger Bands", overlay=true)
length = input.int(20, "Length")
mult = input.float(2.0, "StdDev")
basis = ta.sma(close, length)
dev = mult * ta.stdev(close, length)
upper = basis + dev
lower = basis - dev
plot(basis, title="Basis", color=color.orange, linewidth=2)
plot(upper, title="Upper", color=color.blue)
plot(lower, title="Lower", color=color.blue)

To use it inside dtcharts, paste any version into the Script Converter and it becomes a native indicator on your chart.