Colouring the background of price bands and price bars in TradingView
Colouring the background of price bands and price bars in TradingView
Section titled “Colouring the background of price bands and price bars in TradingView”TL;DR
Blend price zones and bar states withbgcolor()andfill()to make trends instantly recognisable.
At a Glance
Section titled “At a Glance”| Difficulty | Beginner |
| Time to implement | 10-15 min |
| Category | Visual Effects |
Quick Actions
Section titled “Quick Actions”Quick Start
Section titled “Quick Start”//@version=5indicator("Background on trend", overlay=true)
bgColor = close > close[1] ? color.new(color.green, 80) : color.new(color.red, 80)bgcolor(bgColor)Why It Matters
Section titled “Why It Matters”Colour cues let the eye pick out trends faster than raw numbers. bgcolor() and fill() allow you to shade price zones, highlight breakouts, and keep the price series legible. Done right, background accents show whether price sits inside a balanced range or breaking into a new regime without cluttering the chart.
What You’ll Learn
Section titled “What You’ll Learn”- Build dynamic upper and lower bands around a baseline
- Shade the band interior to reveal compression or expansion
- Recolour the chart background when price leaves the band
- Combine bar colours and transparency to keep price readable
Quick Reference
Section titled “Quick Reference”| Call | Purpose |
|---|---|
bgcolor(color) | Paints the entire plotting pane per bar. |
fill(plot1, plot2, color) | Shades the area between two plotted series. |
color.new(color, transparency) | Produces translucent hues (0 opaque → 100 invisible). |
plot(series) | Draws the baseline and band edges used by fill(). |
barcolor(color) | Optionally matches candle bodies to the current background story. |
Implementation Blueprint
Section titled “Implementation Blueprint”-
Define a baseline with envelopes
Start with a smoothing function, then add and subtract a deviation to form band edges.length = 20mult = 2.0basis = ta.sma(hl2, length)dev = mult * ta.stdev(hl2, length)upper = basis + devlower = basis - devplot(basis, "Basis", color=color.orange, linewidth=2)plotUpper = plot(upper, "Upper band", color=color.new(color.blue, 0))plotLower = plot(lower, "Lower band", color=color.new(color.blue, 0)) -
Shade the interior of the band
Use the plot references returned above and fill the space with a muted colour.fill(plotUpper, plotLower, color.new(color.blue, 88)) -
Signal breakouts with background and bar colour
When price crosses outside the band, change the background (and optionally the bars) to draw attention.isAbove = close > upperisBelow = close < lowerbgcolor(isAbove ? color.new(color.green, 80) :isBelow ? color.new(color.red, 80) : na)barcolor(isAbove ? color.new(color.green, 20) :isBelow ? color.new(color.red, 20) : na)
Example Playbook
Section titled “Example Playbook”//@version=5indicator("Dynamic bands + breakout background", overlay=true)
length = input.int(34, "Basis length")mult = input.float(2.2, "Deviation multiplier", step=0.1)bgStrength = input.int(75, "Background transparency", minval=0, maxval=100)matchBars = input.bool(true, "Recolour bars outside band")
basis = ta.ema(hl2, length)dev = mult * ta.stdev(hl2, length)upper = basis + devlower = basis - dev
plotUpper = plot(upper, "Upper", color=color.new(color.blue, 0))plotLower = plot(lower, "Lower", color=color.new(color.blue, 0))plot(basis, "Basis", color=color.orange, linewidth=2)
fill(plotUpper, plotLower, color.new(color.blue, 88))
isAbove = close > upperisBelow = close < lower
bgcolor( isAbove ? color.new(color.green, bgStrength) : isBelow ? color.new(color.red, bgStrength) : na)
if matchBars barcolor( isAbove ? color.new(color.green, 25) : isBelow ? color.new(color.red, 25) : na )- The shaded band conveys volatility range without masking candles.
- Breakout colours only appear when price escapes the band, keeping emphasis rare.
- Optional bar tinting adds redundancy while retaining the underlying candle structure.
Pro Tips & Pitfalls
Section titled “Pro Tips & Pitfalls”- Persist plot handles in variables;
fill()only accepts references returned byplot()orhline(). - Combine
bgcolor()withbarcolor()sparingly—too many colours dilute meaning. - If multiple indicators use
bgcolor(), order matters. Draw the most important background last. - Use
overlay=trueso backgrounds and fills paint on top of price instead of in a detached pane.
Troubleshooting & FAQ
Section titled “Troubleshooting & FAQ”The fill between my bands is missing
Section titled “The fill between my bands is missing”Ensure both band edges are plotted and you pass those plot references to fill(). Calling fill(upper, lower, …) on raw series does nothing.
The background hides my candles
Section titled “The background hides my candles”Transparency is inverted: 0 is solid and 100 is invisible. Aim for 70–90, and consider resetting barcolor(na) when price returns inside the band.
Key Takeaways
Section titled “Key Takeaways”fill()andbgcolor()together show where price sits relative to volatility envelopes.color.new()keeps overlays translucent so the underlying price remains readable.- Signal only the events that matter (band breakouts, regime shifts) to preserve colour meaning.
- Layer backgrounds carefully when multiple scripts share the same chart.
Keep Going
Section titled “Keep Going”- Open AI Editor to apply the pattern with guided assistance
- Browse Strategy Examples for ready-to-run templates
- Connect to Live Trading when you are ready to automate
Adapted from tradingcode.net, optimised for Algo Trade Analytics users.