EMA High–Low Envelope: Pine Script and Band Definitions
This page implements three EMAs: one each of close, high and low. Its outer lines are smoothed highs and lows, not percentage offsets from the closing-price EMA. Check that distinction before comparing this script with TradingView’s percentage-based Envelope indicator.
Inspect the Pine ScriptTwo different calculations share the envelope name
The supplied helper returns EMA(close, 20), EMA(high, 20) and EMA(low, 20). All use the same chart timeframe and length. The high and low lines form a smoothed price-range channel; the close line is not necessarily halfway between them.
With matching history, initialization and continuous valid OHLC, the channel width equals EMA(high − low, 20), by the linearity of the EMA recurrence. It is not a fixed percentage and is not ATR: true range can include a gap from the previous close, whereas high − low is the current bar range.
TradingView’s Envelope documentation describes a different construction: choose an SMA or EMA basis B, then calculate B × (1 + p/100) and B × (1 − p/100). For a hypothetical positive basis of 100 and p = 5, those lines are 105 and 95. The example on this page has no p input and cannot produce that definition by changing the length alone.
A percentage envelope has a fixed relative offset when p is fixed, but its absolute price width changes with B. Neither calculation makes an outside close statistically significant without a separately specified distribution and test.
What the three background colors actually test
- Green: the current high exceeds EMA(high), and the current low exceeds EMA(low).
- Orange: the current high is below EMA(high), and the current low is below EMA(low).
- Blue: neither combined condition holds. This includes mixed comparisons and exact ties.
Green does not require the whole candle to be above the upper line. That stronger condition would require low > EMA(high). Blue does not necessarily mean the entire candle is inside the channel: a bar spanning both outer lines also falls into the mixed category. These colors describe comparisons, not certified bullish, bearish or sideways market regimes.
The comparisons use current high and low against current EMAs, so both sides can update on an open bar. The code does not compare against yesterday’s frozen bands or require bar confirmation. If those are your intended rules, specify them as separate changes.
Worked bar: green while the candle overlaps the channel
Assume the previous EMAs are high 110, low 100 and close 105. The next hypothetical candle has high 112, low 104 and close 108. With length 20, alpha = 2/21.
EMA(high) = 110 + (2/21) × (112 − 110) = 110.190476
EMA(low) = 100 + (2/21) × (104 − 100) = 100.380952
EMA(close) = 105 + (2/21) × (108 − 105) = 105.285714
The high exceeds 110.190476 and the low exceeds 100.380952, so the background is green. Yet the low of 104 remains below the upper line. The width is about 9.809524, while the close EMA is not the arithmetic midpoint of the outer lines. These are arithmetic inputs, not historical observations or a strategy result.
For a second diagnostic, a hypothetical bar with high above its high EMA and low below its low EMA produces blue, even though it crosses both boundaries. Check all four inequalities instead of interpreting a color name as a complete candle-position description.
Using a channel without inventing a reversal probability
A band touch, a close outside a band and a crossover from one side to the other are different events. The script draws lines and background states; it has no RSI, ADX, partial take-profit, stop-loss or automatic orders. A two-bar confirmation rule or volume condition is a hypothesis to test, not a known percentage improvement.
If testing a mean-reversion or breakout interpretation, define which band value was known when the decision occurred, the entry time, exits, sizing and costs. Keep symbol, feed, session and timeframe fixed. A visually measured slope angle changes with chart scaling; use a numerical difference or a stated normalization instead.
Compare Bollinger Bands for standard-deviation offsets and Keltner Channels for a range-based construction. Their names alone do not establish a ranking, hit rate or frequency of trades.
Pine Script v6: implementation and limits
Paste the complete indicator into Pine Editor and add it to a chart. The helper’s length is fixed at 20 in the call. There are no input controls, alertcondition calls or orders. Preserve the three source-series definitions when reproducing its values.
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Pineify
//@version=6
indicator(title="Envelope", overlay=true, max_labels_count=500)
// — Core EMA Envelope Calculation Function —
p_ta_ema_envelope(simple int length) =>
ema_close = ta.ema(close, length)
ema_high = ta.ema(high, length)
ema_low = ta.ema(low, length)
[ema_close, ema_high, ema_low]
// — Indicator call with 20-period length —
[p_ind_1_emaClose, p_ind_1_emaHigh, p_ind_1_emaLow] = p_ta_ema_envelope(20)
// — Plot the Envelope lines (opaque) —
plot(p_ind_1_emaClose, "Envelope - Close", color.rgb(0, 188, 212, 0), 1)
plot(p_ind_1_emaHigh, "Envelope - High", color.rgb(242, 54, 69, 0), 2)
plot(p_ind_1_emaLow, "Envelope - Low", color.rgb(0, 230, 118, 0), 2)
// — Background Highlighting Logic —
p_ind_1_emaClose_bull = (high > p_ind_1_emaHigh and low > p_ind_1_emaLow)
p_ind_1_emaClose_bear = (high < p_ind_1_emaHigh and low < p_ind_1_emaLow)
p_ind_1_emaClose_side = not p_ind_1_emaClose_bull and not p_ind_1_emaClose_bear
p_ind_1_emaClose_bg = p_ind_1_emaClose_bull ? color.rgb(76, 175, 80, 75) :
p_ind_1_emaClose_bear ? color.rgb(255, 152, 0, 75) :
color.rgb(41, 98, 255, 75)
bgcolor(p_ind_1_emaClose_bg, title="Envelope - Background")
Specify a checkable adaptation in Pineify
For this version, request EMAs of high, low and close with the same positive length, and spell out the exact two-comparison background rules. If you instead want percentage bands, provide that separate formula. Ask the Coding Agent for syntax checking and verify the generated values on closed bars; account quotas and access depend on the current plan.
Open Pine Script AI Coding AgentEducational indicator analysis. No measured trading performance is established here. Syntax validation does not prove formula equivalence, execution assumptions or profitability.