Skip to main content
BTC / USDTCRYPTO107,400+2.19%ETH / USDTCRYPTO3,840+2.13%SOL / USDTCRYPTO182.40−1.99%BNB / USDTCRYPTO652.30+0.66%XRP / USDTCRYPTO2.2150+1.61%DOGE / USDTCRYPTO0.3850−1.79%TON / USDTCRYPTO5.240+2.34%AVAX / USDTCRYPTO42.60−2.07%LINK / USDTCRYPTO22.40+2.28%ADA / USDTCRYPTO1.0520−1.68%TRX / USDTCRYPTO0.3300+0.92%DOT / USDTCRYPTO8.420+2.93%BTC / USDTCRYPTO107,400+2.19%ETH / USDTCRYPTO3,840+2.13%SOL / USDTCRYPTO182.40−1.99%BNB / USDTCRYPTO652.30+0.66%XRP / USDTCRYPTO2.2150+1.61%DOGE / USDTCRYPTO0.3850−1.79%TON / USDTCRYPTO5.240+2.34%AVAX / USDTCRYPTO42.60−2.07%LINK / USDTCRYPTO22.40+2.28%ADA / USDTCRYPTO1.0520−1.68%TRX / USDTCRYPTO0.3300+0.92%DOT / USDTCRYPTO8.420+2.93%
ราคา

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.

ข้อมูลนี้มีประโยชน์หรือไม่?