In TradingView’s Pine Script v5, every strategy you backtest depends on two core functions: strategy.entry to open positions and strategy.close (or strategy.exit) to close them. Understanding the difference between them — and how the automatic reverse works — is what separates a reliable backtest from numbers that mislead. This guide shows the real syntax, ready-to-use examples and the mistakes that falsely inflate your results.

Validated your strategy on TradingView and want to take it to automated execution? See how to connect it to a bot:

▶ See signal automation with a bot

strategy.entry: opening positions

The strategy.entry function opens (or reverses) a position. The essential signature is:

strategy.entry(id, direction, qty, limit, stop, when, comment)

The most used arguments are id (the order name, a string), direction (strategy.long or strategy.short) and when (a boolean condition). The crucial detail: if you are already long and call a strategy.entry with strategy.short, TradingView reverses the position automatically — it closes the long and opens the short in the same order.

Why this matters: this implicit reverse is the number one source of confusion. If you expected to simply close and ended up opening a position in the opposite direction, the backtest turns out different from what you had in mind.

strategy.close vs strategy.exit

Both close positions, but with different purposes:

strategy.close(id, when) → closes the position opened by that id at market, when the condition is true. Use it for signal-based exits (e.g., an opposite crossover).
strategy.exit(id, from_entry, profit, loss, limit, stop, trail_points) → closes with a stop loss, take profit or trailing stop. It is the correct method for price-based risk management.

Rule of thumb: strategy.close to exit on a signal; strategy.exit to exit on price (target/stop).

Complete example: moving average crossover

A simple, honest strategy — it goes long when the fast average crosses above the slow one, exits on the opposite crossover, with a stop and a target:

//@version=5 strategy(“Cruzamento MM — Exemplo”, overlay=true, initial_capital=1000, default_qty_type=strategy.percent_of_equity, default_qty_value=10, commission_type=strategy.commission.percent, commission_value=0.05) rapida = ta.sma(close, 9) lenta = ta.sma(close, 21) plot(rapida, color=color.aqua) plot(lenta, color=color.orange) compraSinal = ta.crossover(rapida, lenta) vendaSinal = ta.crossunder(rapida, lenta) // Long entry if compraSinal strategy.entry(“Long”, strategy.long) // Exit on signal (opposite crossover) if vendaSinal strategy.close(“Long”) // Exit by price: 2% stop and 4% target strategy.exit(“TP/SL”, from_entry=”Long”, stop=close * 0.98, limit=close * 1.04)
Keep it realistic: always set commission_value and account for slippage. Backtests without costs look profitable and collapse when live.

Mistakes that inflate the backtest

Repainting from using an unclosed candle → confirm signals with barstate.isconfirmed or evaluate at the close.
Lookahead in request.security → never use barmerge.lookahead_on in a strategy.
Ignoring commission and slippage → unrealistic results.
Confusing close with exit → your stop/target simply never triggers.

strategy.close_all and canceling orders

To close everything at once (e.g., a time-of-day or news filter), use strategy.close_all(). To remove pending orders that have not been filled yet, use strategy.cancel(id) or strategy.cancel_all().

// Closes all positions at the end of the session horaFim = (hour == 17 and minute >= 55) if horaFim strategy.close_all(comment=”Fim do dia”) strategy.cancel_all()

FAQ

Does strategy.entry open a new order if I already have a position? In the same direction with the same id, it normally does not add to the position (it depends on pyramiding). In the opposite direction, it reverses the position.

Can I use strategy.close and strategy.exit together? Yes — it is common: exit handles the stop/target and close exits on a signal. Whichever happens first closes the position.

Why doesn’t my stop work? You probably used strategy.close expecting a stop. Stops/targets only exist in strategy.exit.

Does a profitable backtest guarantee real profit? No. Overfitting, costs and real execution change everything. Validate in a forward test before committing any money.

Disclaimer: trading and binary options are high-risk activities and most retail traders lose money. This content is educational, does not constitute investment advice, and backtest results do not guarantee future performance. Always test on a demo account before trading with real capital.

Similar Posts