Skip to content
Docs
Open Portal
Copy manually

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

Define entry and exit signals

Give upward and downward crosses different jobs, with separate chart markers and message previews.

Build signalsLesson 2 of 4Course map

Builds on: Combine conditions

You will give two events different jobs: an upward cross describes a long entry, and a downward cross describes a long exit. An exit from a buy position is not automatically an instruction to open a sell position.

Keep using the same My EMA practice file and 1-minute BITSTAMP:BTCUSD chart. Earlier alerts stay paused. Enable signal alerts stays off, so this lesson only draws and previews messages.

Click Copy on the complete code below.

Complete indicator with separate entry and exit signalsPine v6
TradingView → Pine Editor → My EMA practice Pine Script
//@version=6
indicator("Pine practice: entry and exit", 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 crossedDown = ta.crossunder(close, ema)
bool trendAllowed = not useTrendFilter or close > trendEma
bool entrySignal = crossedUp and barstate.isconfirmed and trendAllowed
bool exitSignal = crossedDown and barstate.isconfirmed
string entryMessage = practiceLabel + " | " + syminfo.ticker + " | long entry signal"
string exitMessage = practiceLabel + " | " + syminfo.ticker + " | long exit 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 = "Long entry signal", style = shape.triangleup,
location = location.belowbar, color = color.green, size = size.small, text = "Entry")
plotshape(exitSignal, title = "Long exit signal", style = shape.triangledown,
location = location.abovebar, color = color.orange, size = size.small, text = "Exit")
var table preview = table.new(position.top_right, 1, 3, border_width = 1)
if barstate.islast
table.cell(preview, 0, 0, "Message previews", bgcolor = color.blue, text_color = color.white)
table.cell(preview, 0, 1, entryMessage, bgcolor = color.white, text_color = color.black)
table.cell(preview, 0, 2, exitMessage, bgcolor = color.white, text_color = color.black)
// These signals do not read a broker's positions or place orders.
if enableSignalAlerts
if entrySignal
alert(entryMessage, alert.freq_once_per_bar_close)
else if exitSignal
alert(exitMessage, alert.freq_once_per_bar_close)
1

Replace and save the practice script

Open Pine. Click inside My EMA practice, select all with Cmd+A / Ctrl+A, and paste with Cmd+V / Ctrl+V.

Save with the cloud-shaped control and update the chart if asked. Keep one current practice indicator on the chart.

What you should see

The chart label reads Pine practice: entry and exit. The message preview has separate entry and exit rows.

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

Start with the unfiltered comparison

Open the settings gear for Pine practice: entry and exit, then Inputs. Set Signal EMA length to 20. Uncheck Use trend filter and Enable signal alerts, then click Ok.

Find a green Entry beneath a candle and an orange Exit above a later candle.

What you should see

The chart marks confirmed upward crosses as Entry and downward crosses as Exit.

A green Entry marker at an upward EMA cross is followed by an orange Exit marker at a downward cross.
Enlarge screenshot
Entry (1) and Exit (2) describe different signal conditions. These markers do not confirm a broker position or an executed trade.

Entry (1) and Exit (2) describe different signal conditions. These markers do not confirm a broker position or an executed trade.

A green Entry marker at an upward EMA cross is followed by an orange Exit marker at a downward cross.
Marker What the rule says
Entry Price crossed above the blue EMA on a closed candle. If the trend filter is enabled, it must also pass.
Exit Price crossed below the blue EMA on a closed candle.

This version concentrates on Entry and Exit markers; it does not show the previous lesson’s gray Filtered labels.

Find these lines in Pine Editor:

Different conditions and messages for entry and exitExcerpt from the complete example.
Read in your existing script Pine Script
bool crossedUp = ta.crossover(close, ema)
bool crossedDown = ta.crossunder(close, ema)
bool trendAllowed = not useTrendFilter or close > trendEma
bool entrySignal = crossedUp and barstate.isconfirmed and trendAllowed
bool exitSignal = crossedDown and barstate.isconfirmed
string entryMessage = practiceLabel + " | " + syminfo.ticker + " | long entry signal"
string exitMessage = practiceLabel + " | " + syminfo.ticker + " | long exit signal"

ta.crossunder(...) is the downward counterpart to ta.crossover(...). Both final signals require barstate.isconfirmed, so neither represents a brief intrabar move that disappeared before the close.

The preview’s entry row ends long entry signal. The exit row ends long exit signal. They are informational messages, not PineConnector commands. Later, you will map those intentions to the appropriate broker requests.

An Exit marker does not prove a position exists. This indicator cannot see your broker’s positions. It can show an exit condition even when no earlier entry qualified, or when a position has already closed for another reason.

3

Turn the trend filter back on

Open Inputs, set Trend EMA length to 100, and check Use trend filter. Keep Enable signal alerts off and click Ok.

Compare a previously visible exit marker. Its condition still uses the downward cross of the blue EMA; it does not require price to remain above the orange line.

What you should see

Some Entry markers may disappear, but the downward-cross Exit rule is unchanged.

Putting the entry filter around the entire script would also block exits when that filter is false. Here, each signal has its own condition so you can decide which requirements belong to which action.

At the bottom, if entrySignal and else if exitSignal choose the corresponding message. For this pair of rules, the same closed candle cannot be both above and below the same EMA at once.

Question: the trend filter blocks new entries. Does that also prevent a downward-cross exit in this version?

Show the answer

No. exitSignal does not include trendAllowed. Filtering entries does not disable this exit condition.

Question: you see Exit on the chart. Can you conclude that a broker position was closed?

Show the answer

No. It is a chart condition. This lesson has signal alerts off, no webhook, and no broker-position feedback. A broker outcome must be checked separately.

Leave Enable signal alerts off. In the next lesson, add a time window to entries while keeping exits separate.