Give price bars different colours with TradingView's Pine Script
Give price bars different colours with TradingView’s Pine Script
Section titled “Give price bars different colours with TradingView’s Pine Script”TL;DR
Combinebarcolor()with conditional logic so each bar reflects trend, volatility, or any signal you define.
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("Coloured bars", overlay=true)
color up = color.new(color.green, 0)color down = color.new(color.red, 0)
barcolor(close > close[1] ? up : down)Why It Matters
Section titled “Why It Matters”Colour-coding bars makes it easier to spot regimes: trend versus chop, high volatility versus calm, or bullish versus bearish signals. Pine Script lets you assign colours per bar, so you can provide immediate visual feedback without cluttering the chart with extra indicators.
What You’ll Learn
Section titled “What You’ll Learn”- Building colour palettes and reusing them
- Cascaded
if, conditional operator, andswitchpatterns for bar colours - Combining multiple conditions without conflicting outputs
- Applying bar colours alongside other stylings (backgrounds, plots)
Quick Reference
Section titled “Quick Reference”| Call | Purpose |
|---|---|
barcolor(color) | Sets the colour for the current bar. |
color.new(color, transparency) | Customises palette and transparency. |
if … else / switch | Choose a colour based on multiple conditions. |
?: (conditional operator) | Compact selection for simple cases. |
na | Leave a bar uncoloured (fallback to default). |
Implementation Blueprint
Section titled “Implementation Blueprint”-
Define your colour palette
Reuse named colours instead of hard-coding each branch.bullish = color.new(color.green, 0)bearish = color.new(color.red, 0)neutral = color.new(color.gray, 50) -
Pick the control structure
Cascadedif, conditional operator, orswitchensure only one colour applies per bar.color barColour =close > ema ? bullish :close < ema ? bearish :neutral -
Apply the colour and avoid conflicts
Callbarcolor()once with the final colour decision.barcolor(barColour)
Example Playbook
Section titled “Example Playbook”//@version=5indicator("Multi-state bar colours", overlay=true)
emaFast = ta.ema(close, 21)emaSlow = ta.ema(close, 55)atrVal = ta.atr(14)
color bullishTrend = color.new(color.green, 0)color bearishTrend = color.new(color.red, 0)color squeeze = color.new(color.orange, 0)color neutral = na
color barColour = neutral
if emaFast > emaSlow and close > emaFast + atrVal barColour := bullishTrendelse if emaFast < emaSlow and close < emaFast - atrVal barColour := bearishTrendelse if ta.tr(true) < atrVal * 0.5 barColour := squeeze
barcolor(barColour)
plot(emaFast, "EMA 21", color=color.new(color.green, 50))plot(emaSlow, "EMA 55", color=color.new(color.red, 50))- Cascaded `if` ensures only one colour wins per bar.
- Squeeze detection defaults to orange only when neither bullish nor bearish conditions apply.
- Using named colours keeps the palette consistent and easy to adjust.
Pro Tips & Pitfalls
Section titled “Pro Tips & Pitfalls”- Avoid calling
barcolor()multiple times—later calls override earlier ones. - Keep transparency high when colouring bars on darker chart themes.
- Combine bar colours with background or label cues for multi-layer storytelling.
- Reset to
nawhen you want bars to revert to default styling.
Troubleshooting & FAQ
Section titled “Troubleshooting & FAQ”My colours flicker or look inconsistent—why?
Section titled “My colours flicker or look inconsistent—why?”It usually means multiple colour conditions are firing on the same bar. Consolidate your logic into a single decision tree before calling barcolor().
Can I colour only body or only wick?
Section titled “Can I colour only body or only wick?”No—barcolor() changes the entire candle. Use plotcandle() for per-part customisation if you need separate wick/body colours.
Key Takeaways
Section titled “Key Takeaways”- All bar-colour logic should resolve to exactly one colour (or
na) per bar. - Reusable colour definitions make scripts cleaner and easier to tweak.
- Think in terms of regimes: trend, volatility, or custom flag states—and map each to a colour.
- Use
barcolor()alongside other visual tools for a cohesive indicator experience.
Keep Going
Section titled “Keep Going”- Open AI Editor to apply the pattern with guided assistance
- Browse PineScript 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.