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 botstrategy.entry: opening positions
The strategy.entry function opens (or reverses) a position. The essential signature is:
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.
strategy.close vs strategy.exit
Both close positions, but with different purposes:
id at market, when the condition is true. Use it for signal-based exits (e.g., an opposite crossover).
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:
commission_value and account for slippage. Backtests without costs look profitable and collapse when live.
Mistakes that inflate the backtest
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().
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.
