Bollinger-style bands around MACD · Example 10 / 1 / 12 / 26
AK MACD Bollinger Bands: Calculation, Colors and Limits
AK MACD BB compares an EMA-based MACD value with a rolling envelope calculated from that same MACD series. The bands measure dispersion in the oscillator, not support and resistance prices for the asset.
Algokid's original TradingView publication dates to February 24, 2015. The implementation below is the version documented by Pineify. Check its source and color rules rather than assuming every script using the name is identical.
Calculate MACD first, then its envelope
M = EMA(close, fast) − EMA(close, slow)
B = SMA(M, n)
σ = sqrt(Σ(Mᵢ − B)² / n)
Upper = B + kσ; Lower = B − kσ
Width = Upper − Lower = 2kσ
The displayed code uses fast 12, slow 26, n = 10 and k = 1. Pine's default standard-deviation calculation uses the population divisor n. The current bar belongs to the rolling window, so both its MACD value and its comparison bands can change before the close.
Positive MACD means the fast EMA exceeds the slow EMA; negative MACD means the reverse. Being above the upper band is a separate comparison. For example, a MACD value of -1 can exceed an upper band of -1.5 while remaining below zero. A teal dot therefore does not by itself mean MACD is positive or that price is rising.
The function returns MACD, basis, upper and lower. The shown plots draw the two bands and MACD circles; the basis is returned but is not separately plotted. There is no conventional nine-period signal EMA or MACD histogram in this implementation.
Reproduce the bands with a short numerical window
Use a synthetic three-observation MACD window [1, 2, 3] to isolate the band calculation. This is an arithmetic example, not the default ten-bar configuration or an observed price series. Basis is 2. Population variance is (1 + 0 + 1) / 3 = 2/3, so standard deviation is approximately 0.816497. With k = 1, the bands are 2.816497 and 1.183503. The current MACD 3 is strictly above the upper band.
If the next MACD value is 2, the rolling window becomes [2, 3, 2]. Basis is 7/3 and standard deviation is √(2/9), approximately 0.471405. The bands become approximately 2.804738 and 1.861929, placing the current value inside. A return inside the oscillator bands does not tell us the asset's price change; the input closes and EMA states would be needed for that.
Keeping the first window fixed and changing k from 1 to 2 doubles width from 1.632993 to 3.265986. It does not establish how much a dataset's touch count changes. Changing either EMA length changes the MACD observations themselves, so the effect is not just a rescaling of the band width.
Pine Script implementation
The four settings are fixed function arguments in this example; there are no editable input declarations. The MACD plot uses circle markers with a linewidth of three.
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// (c) Pineify
//@version=6
indicator(title="AK MACD Bollinger Bands", overlay=false, max_labels_count=500)
p_ta_ak_macd_bb(simple int length, series float dev, simple int fastLength, simple int slowLength) =>
fastMA = ta.ema(close, fastLength)
slowMA = ta.ema(close, slowLength)
macd = fastMA - slowMA
basis = ta.sma(macd, length)
Std = ta.stdev(macd, length)
upper = basis + Std * dev
lower = basis - Std * dev
[macd, basis, upper, lower]
[p_ind_1_macd, p_ind_1_middle, p_ind_1_upper, p_ind_1_lower] = p_ta_ak_macd_bb(10, 1, 12, 26)
hline(0, "AK MACD BB - Zero Line", color.rgb(255, 160, 0, 0))
p_ind_1_upper_plot = plot(p_ind_1_upper, "AK MACD BB - Upper", color.rgb(158, 158, 158, 0), 2)
p_ind_1_lower_plot = plot(p_ind_1_lower, "AK MACD BB - Lower", color.rgb(158, 158, 158, 0), 2)
fill(p_ind_1_upper_plot, p_ind_1_lower_plot, color.rgb(33, 150, 243, 75), title="AK MACD BB - Band Fill")
plot(p_ind_1_macd, "AK MACD BB - MACD", style=plot.style_circles, linewidth=3, color=p_ind_1_macd >= p_ind_1_upper ? color.rgb(38, 166, 154, 0) : p_ind_1_macd <= p_ind_1_lower ? color.rgb(255, 82, 82, 0) : color.gray)
barcolor(p_ind_1_macd > p_ind_1_upper ? color.rgb(255, 235, 59, 0) : p_ind_1_macd < p_ind_1_lower ? color.rgb(0, 188, 212, 0) : na)Open TradingView's Pine Editor, add the script and compare its values on completed bars. Match the symbol, session, price adjustments and interval. This is an indicator: it contains no squeeze detector, strategy orders, alerts, stop prices or measured performance.

Circle colors and candle colors use different boundaries
| MACD position | Circle | Price-bar override |
|---|---|---|
| Above upper | Teal | Yellow |
| Exactly at upper | Teal | None |
| Strictly inside | Gray | None |
| Exactly at lower | Red | None |
| Below lower | Red | Cyan |
Circles use inclusive comparisons; barcolor uses strict comparisons. The last argument 0 in color.rgb is transparency, meaning fully opaque. The blue fill uses transparency 75. The yellow and cyan bar colors are not invisible.
With a constant valid MACD window, standard deviation is zero and both bands equal MACD. The first circle condition wins, making the dot teal, while neither strict bar-color condition is true. That degenerate result is not evidence of a bullish breakout. Band-width ratios must also guard against a zero denominator.
Dispersion is not a reversal probability
A multiplier of one means one rolling standard deviation from the basis. It does not show that 68% of future values will remain inside, or that a touch has any particular chance of reversing. John Bollinger's own rules warn against statistical assumptions based solely on the standard-deviation construction.
A narrow envelope records lower recent MACD dispersion. It does not guarantee a large move within three, five or seven bars. A wider envelope does not establish a more reliable zero cross. Any such claim requires a specified dataset, event definition, outcome horizon and independent evaluation.
MACD is in quoted price units. Comparing raw MACD values or width between a high-priced instrument and a low-priced one does not compare their percentage volatility. Dividing width by the MACD basis is unstable when the basis is zero or near zero and changes sign when the basis is negative.
Define the event before testing a strategy
Touch versus return inside
A prior upper-band excursion should compare the previous MACD with its own previous upper band. A later return inside requires the current MACD to be between the current lower and upper bands. Decide whether equality qualifies, whether the bar must close and whether one event or every qualifying bar is counted.
A squeeze experiment
Choose a width measure and a historical reference before looking at outcomes. If comparing current width with an earlier average, state whether that average excludes the current bar. Record a maximum time allowed for a subsequent breakout. Treat a threshold such as 35% as an experimental choice, not a proven optimum.
Price stops and costs
Define stops on the price chart. A MACD basis of 0.5 is not a tradable asset price for a security at 100. If a hypothetical entry is 100 and a price-based stop is 98, risk is 2 per share before costs; an oscillator crossing does not guarantee a fill at either price.
Specify entry timing, exits, repeated-entry rules, position size and transaction costs. Compare the full rule with and without an added price EMA or ADX filter on the same observations. Keep a held-out sample and include losing, untriggered and ambiguous events. No benefit percentage is claimed for any filter here.
Compare MACD, MACD bands and price bands
Standard MACD commonly displays its EMA difference, a smoothed signal line and their difference as a histogram. This version uses an SMA and deviation envelope on the MACD series. Price Bollinger Bands instead apply the envelope to a price source. The transformations differ; none automatically removes lag or predicts which trade will succeed.
To customize this implementation in Pineify, specify the EMA lengths, band window, deviation divisor, equality behavior and closed-bar conditions. Ask for positive input bounds and separate plot and alert logic. The AI Coding Agent can draft and revise the script; inspect generated code and compare numerical outputs. Access and usage depend on your plan.
Customize the MACD band specificationAK MACD BB questions
Primary sources and related reading
- Algokid: original AK MACD BB publication, February 24, 2015
- John Bollinger: band interpretation rules
- TradingView: MACD calculation
- TradingView: ta.stdev reference
- MACD reference
- Bollinger Bands reference
Numeric examples are synthetic checks of calculation and display rules. They are not historical returns or personal trading results.