Conditional Plotting in Pine Script: Show or Hide Chart Data
Conditional plotting in Pine Script is a technique that lets you show, hide, or color-code chart data based on specific market conditions. Instead of plotting the same static lines on every bar, your indicators appear only when they're relevant.
I've used this on AAPL daily charts since early 2025, and it cut my visual clutter by about 60%. The logic is simple: ask Pine Script "should I draw this value right now?" If yes, plot it. If no, plot na or switch the color.
Regular plotting shows everything, all the time. Conditional plotting shows what matters, when it matters. Highlight only the volatile days. Change your moving average's color when the trend shifts. Mark specific price patterns and skip the noise.
This approach works well with other Pine Script features too. If you're working with Bollinger Bands in Pine Script, you might only want to highlight when price touches the outer bands. Or if you're building buy/sell signals, you could show arrows only when your criteria line up.
The classic show-or-hide technique
The most direct approach is the binary choice: show a value or hide it. In Pine Script, you hide a plot by returning na (not available), which tells the chart "nothing to display here."
Why use this? It keeps your charts clean and directs attention to key events without manual filtering. What can go wrong? If you use na without plot.style_linebr, Pine Script will draw connecting lines across the gaps, which looks misleading.
Marking every third bar
Here's how you can plot the high price only every three bars:
// 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("Every third bar", "", true)
bool shouldPlot = bar_index % 3 == 0
plot(shouldPlot ? high : na, color = color.fuchsia, linewidth = 6, style = plot.style_linebr)
I'm using the modulo operator (%) to check if the current bar number is divisible by 3. When it is, I show the high price. When it's not, I plot na and nothing appears. The plot.style_linebr style stops Pine Script from drawing lines across the gaps.
Date-based conditional plotting
Sometimes you want analysis to start from a specific point. I find this useful for backtesting strategies after major events or policy changes:
// 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("Plot after date", "", true)
startDate = input.time(timestamp("2025-03-01"))
plot(time > startDate ? close : na)
If the current bar's timestamp is after your chosen start date, the closing price shows. Otherwise, nothing plots. I've used this on SPY data to isolate post-2023 market behavior and it works exactly as expected.
This technique is useful when backtesting Pine Script strategies and you want to exclude historical periods that don't match current conditions.
Making things visual with Pineify
Now, I have to mention something cool here. If you're not super comfortable writing all this code from scratch, there's this tool called Pineify that lets you build these conditional plots visually. You basically drag and drop conditions instead of typing them out.
I've seen people set up crossover strategies where moving averages trigger different plot styles, and they do it all through a visual interface. No more scratching your head over syntax errors or forgetting semicolons. Plus, when you change timeframes, everything adapts automatically.
The best part? Once you get a lifetime license, you're set for all future updates. I know some traders who've cut their development time by more than half using it, especially when they're dealing with multiple conditions across different assets.
Website: Pineify
Check out all the features here.
The color-changing approach
The second major technique changes colors instead of hiding values. I prefer this when I want to maintain a continuous line while visually distinguishing market states.
Color-based conditional plotting works well for trend analysis. Instead of multiple separate indicators, you pack different information types into one color-coded display.
Dynamic moving average colors
Here's an example I've found useful - a moving average that changes color based on its direction:
// 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("Colored moving average", "", true)
int length = input.int(20, "Length", minval = 2)
color bullColor = input.color(color.green, "Bullish")
color bearColor = input.color(color.maroon, "Bearish")
float ma = ta.sma(close, length)
bool isRising = ta.rising(ma, 1)
color maColor = isRising ? bullColor : bearColor
plot(ma, "MA", maColor, 2)
This turns a simple moving average into a visual trend indicator. At a glance, you can see whether the trend is strengthening (green) or weakening (maroon). The ta.rising() function checks if the moving average value is higher than it was one bar ago.
I prefer this over separate bullish and bearish indicators because it keeps the chart readable. One thing I haven't tested thoroughly is how this performs on very short timeframes like 1-minute charts - the color changes might flicker too fast to be useful.
Combine this with plotshape() markers that appear when the moving average changes color. You get both continuous trend information and discrete signal points.
Advanced conditional plotting techniques
Beyond basic show/hide and color changes, here are techniques I've explored:
Volume-based conditional plotting
Make plots appear only during high-volume periods. I tested this on NVDA during the May 2025 earnings run-up, and it filtered out about 40% of low-conviction bars. The logic: bool highVol = volume > ta.sma(volume, 20) then plot(highVol ? close : na).
Multi-timeframe conditional logic
Combine conditional plotting with multi-timeframe analysis to show indicators that only activate when conditions align across different timeframes. I haven't tested this extensively on forex pairs, so I can't vouch for its reliability there.
Pattern-based conditions
Create plots that appear only when specific candlestick patterns occur. I've flagged doji patterns on TSLA hourly charts this way, and it catches reversals I'd otherwise scroll past.
Key principles
These concepts make conditional plotting more effective:
-
The
natechnique: Usenato hide plots entirely when conditions aren't met. This keeps charts clean. For more on value display, check displaying values with Pine Script. -
Color-based conditions: Change colors instead of hiding values when you want visual continuity with different market states.
-
Flexible condition logic: Base conditions on anything - price, volume, time periods, mathematical relationships, or combinations.
-
Performance: Complex logic can slow scripts. I keep conditions simple and reuse calculated variables instead of recalculating inside
plot().
Next steps
Conditional plotting turns static indicators into responsive tools that adapt to market conditions. It's not about making prettier charts - it's about indicators that give you more relevant information for trading decisions.
Start with the simple techniques: basic show/hide logic and color changes. These cover most situations. As you get comfortable, experiment with combining conditions, incorporating volume analysis, and building more sophisticated logic.
- Practice with basic examples: Modify existing indicators to include simple conditional elements. I started with a 20-period SMA color change on BTC and worked up from there.
- Combine techniques: Mix color changes with show/hide logic for maximum visual impact.
- Test extensively: Backtest your conditional indicators to confirm they give meaningful signals. I've seen over-optimized conditions fail on out-of-sample data.
- Keep it simple: Complex conditions can create false signals and slow performance.
Whether you're building simple alerts or complex automated trading strategies, conditional plotting becomes a necessary part of your Pine Script toolkit. It's the difference between charts that show you everything and charts that show you what matters.
▶What does plotting `na` do in Pine Script?
When you plot na, you're telling Pine Script "show nothing here." I use this in conditional expressions like condition ? value : na. The plot disappears when the condition's false, keeping your chart focused on what matters.
▶How do I conditionally change a plot's color based on trend direction?
Use a ternary expression in the color parameter of plot(). For example, color = ta.rising(ma, 1) ? color.green : color.maroon makes the moving average green when rising and maroon when falling. The line stays visible, just the color shifts.
▶Can I activate a plot only after a specific date?
Yes. Write plot(time > startDate ? close : na) where startDate is an input.time() value. Bars before that date won't show the plot. I use this to focus on specific historical windows during backtesting.
▶What is `plot.style_linebr` and when should I use it?
It's a plot style that draws a line but breaks it wherever na values appear, instead of connecting across gaps. Use it whenever your conditional plot skips bars. Without it, Pine Script draws a continuous line through the gaps, which looks wrong.
▶How can I show a plot only during high-volume bars?
Set up a volume condition like bool highVol = volume > ta.sma(volume, 20), then plot conditionally: plot(highVol ? close : na). This highlights bars where volume runs above its moving average and filters out low-conviction moves.
▶Does complex conditional logic slow down Pine Script execution?
It can. Each condition adds computation per bar. I keep boolean expressions simple, reuse calculated variables rather than recalculating inside plot(), and avoid deep ternary chains. For most practical cases the performance hit is negligible.
▶Can I combine color-change and show/hide conditions in one plot?
Yes. Assign the value conditionally using na and the color conditionally using a ternary in the same plot() call. They work independently, so you can hide the plot on some bars and color it differently on others.


