Skip to content
Docs
Open Portal
Copy manually

Your browser did not allow direct clipboard access. Select and copy the text below.

Combine conditions and filters

Add one trend filter to your EMA crossover and see which existing signals it allows or blocks.

You already have an event: price crosses above an EMA. Now add one requirement: the closing price must also be above a longer EMA.

Use My EMA practice, BITSTAMP:BTCUSD, and 1-minute candles. Leave all earlier practice alerts paused. This lesson uses chart output; keep Enable signal alerts off.

Click Copy on this complete code block.

Complete crossover indicator with an optional trend filterPine v6
TradingView → Pine Editor → My EMA practice Pine Script
//@version=6
indicator("Pine practice: filters", overlay = true)
int length = input.int(20, "Signal EMA length", minval = 1)
int trendLength = input.int(100, "Trend EMA length", minval = 1)
bool useTrendFilter = input.bool(false, "Use trend filter")
string practiceLabel = input.string("Practice A", "Practice label")
bool enableSignalAlerts = input.bool(false, "Enable signal alerts")
float ema = ta.ema(close, length)
float trendEma = ta.ema(close, trendLength)
bool crossedUp = ta.crossover(close, ema)
bool confirmedCross = crossedUp and barstate.isconfirmed
bool trendAllowed = not useTrendFilter or close > trendEma
bool entrySignal = confirmedCross and trendAllowed
string message = practiceLabel + " | " + syminfo.ticker + " | entry signal"
plot(ema, title = "Signal EMA", color = color.blue, linewidth = 2)
plot(useTrendFilter ? trendEma : na, title = "Trend EMA", color = color.orange, linewidth = 2)
plotshape(entrySignal, title = "Allowed cross", style = shape.triangleup,
location = location.belowbar, color = color.blue, size = size.small, text = "Cross")
plotshape(confirmedCross and not trendAllowed, title = "Filtered cross", style = shape.xcross,
location = location.belowbar, color = color.gray, size = size.small, text = "Filtered")
var table preview = table.new(position.top_right, 1, 2, border_width = 1)
if barstate.islast
table.cell(preview, 0, 0, "Entry message preview", bgcolor = color.blue, text_color = color.white)
table.cell(preview, 0, 1, message, bgcolor = color.white, text_color = color.black)
// Enable only for text-only practice. Recreate a saved alert after changes.
if enableSignalAlerts and entrySignal
alert(message, alert.freq_once_per_bar_close)
1

Replace, save and check the script

Open Pine, click inside My EMA practice, select all, and paste. Use Cmd+A / Cmd+V on Mac or Ctrl+A / Ctrl+V on Windows.

Save with the cloud-shaped control or Cmd+S / Ctrl+S. Apply an update if offered. Add the indicator only if it is missing from the chart.

What you should see

Pine practice: filters appears on the chart. With the trend filter off, the blue EMA and Cross markers follow the original crossover rule.

TradingView right-hand toolbar with the Pine Editor icon highlighted
Enlarge screenshot
Click Pine in the far-right toolbar. This opens the code editor; it does not create a trade.

Click Pine in the far-right toolbar. This opens the code editor; it does not create a trade.

TradingView right-hand toolbar with the Pine Editor icon highlighted
2

Turn on the trend filter

Hover over Pine practice: filters and open its Settings → Inputs. Set Signal EMA length to 20 and Trend EMA length to 100.

Check Use trend filter. Keep Enable signal alerts unchecked, then click Ok.

What you should see

An orange trend EMA appears alongside the blue signal EMA. Upward crosses below the orange line are marked Filtered.

Signal EMA 20 and trend EMA 100 with the trend filter checked and signal alerts unchecked.
Enlarge screenshot
Use lengths 20 and 100 (1), switch the trend filter on (2), and leave signal alerts off (3).

Use lengths 20 and 100 (1), switch the trend filter on (2), and leave signal alerts off (3).

Signal EMA 20 and trend EMA 100 with the trend filter checked and signal alerts unchecked.
3

Find a blocked crossover

Find a Filtered mark. The candle crossed above the blue EMA, but its close was not above the orange EMA.

Now find an allowed blue Cross marker. Its candle passed both requirements. Look farther back if the current view does not contain both examples.

What you should see

A gray Filtered mark identifies an upward cross that did not pass the extra requirement.

Historical BTCUSD candles show a gray Filtered cross below the orange trend EMA followed by an allowed Cross above it.
Enlarge screenshot
The gray Filtered marker (1) has a cross above the blue EMA but not above the orange trend EMA. The later blue Cross (2) passes both checks.

The gray Filtered marker (1) has a cross above the blue EMA but not above the orange trend EMA. The later blue Cross (2) passes both checks.

Historical BTCUSD candles show a gray Filtered cross below the orange trend EMA followed by an allowed Cross above it.

Only blue Cross markers? Check that Use trend filter is on. If it is, the visible crosses may all pass; that does not mean the filter is broken.

The filter and the final entry conditionExcerpt from the complete script above.
Read in your existing script Pine Script
bool trendAllowed = not useTrendFilter or close > trendEma
bool entrySignal = confirmedCross and trendAllowed

The two lines answer two questions:

  1. trendAllowed: is the filter off, or is price above the trend EMA?
  2. entrySignal: did a confirmed cross occur, and is the trend filter satisfied?
Confirmed upward cross? Trend requirement satisfied? Final signal
Yes Yes Yes.
Yes No No.
No Yes No. Being above the trend EMA alone is not a new crossover.

The blue and orange EMA calculations remain separate from this decision. Calculate them on each bar, then combine the resulting conditions. Do not move ta.crossover(...) inside the final if block to make it look shorter.

4

Temporarily turn the filter off

Choose one closed candle marked Filtered. Reopen Inputs, uncheck Use trend filter, and click Ok. Inspect that same candle.

Then turn Use trend filter back on and inspect it again. Keep the EMA lengths unchanged for this comparison.

What you should see

A previously filtered upward cross becomes an allowed Cross when the additional requirement is removed.

For the same data and lengths, this filter can remove eligible crosses; it does not invent additional crossover events. Fewer signals do not establish a more profitable rule. This exercise checks whether the code implements your stated requirement.

Question: price is above the orange trend EMA on six consecutive candles, but does not cross above the blue EMA. How many new entry signals does that fact alone produce?

Show the answer

None. The trend condition is a filter. A confirmed upward cross of the blue EMA is still required.

Try: change Trend EMA length to 200, predict which requirement changed, and inspect the result. Then restore 100 and keep Enable signal alerts off.

What should change?

The orange trend calculation and which crosses pass its filter can change. The blue 20-period EMA and its underlying crossover rule stay the same.