An indicator only shows signals on the chart. A strategy (strategy) in Pine Script v5 goes further: it opens and closes simulated positions and generates a backtest report with profit, drawdown and win rate. The heart of any strategy is two functions: strategy.entry, which opens the position, and strategy.exit, which defines the stop loss, target and exit. This guide shows the syntax, a complete moving average crossover example ready to copy, and — honestly — the precautions you need so you don’t fool yourself with backtest results that look too good to be true.
Validated your strategy in the backtest? The next step is running it on its own, with no emotion and no delay.
See the automation bot →Declaring a strategy
Everything starts by replacing indicator() with strategy() in the header. That is where you define initial capital, order size and commissions — the parameters that make a backtest realistic.
commission_value and, if possible, slippage. Backtests without costs look profitable and mislead you — in the real world, fees and spread eat a good part of the result.
strategy.entry: opening positions
strategy.entry() sends an entry order. The essential parameters are the id (a unique name for the order) and direction, which can be strategy.long or strategy.short.
If you call strategy.entry("Venda", strategy.short) while long, Pine reverses the position automatically — it closes the long and opens the short in the same order. This behavior is great for “always in the market” strategies.
strategy.exit: stop, target and trailing
strategy.exit() closes an open position. It needs to point to the from_entry (the id of the entry it will close) and accepts a stop loss and take profit in points (loss/profit) or in price (stop/limit).
strategy.close() closes the position at market (for example, on an opposite signal), while strategy.exit() places stop/target orders that stay active. Use both with care — mixing them carelessly creates duplicate exits.
Complete example: moving average crossover
A classic strategy: buy when the fast average crosses above the slow one, sell when it crosses below, with a defined stop and target.
Paste it into the Pine editor, open the Strategy Tester tab, and TradingView shows net profit, number of trades, win rate and maximum drawdown.
Watch out for overfitting
Here is the most important piece of advice — the one almost no tutorial states clearly: a beautiful backtest does not guarantee future profit. It is easy to tweak parameters until the past chart looks perfect — that is called overfitting, and the strategy usually falls apart on new data.
- Test on several different assets and periods, not just the one that “worked”.
- Set aside a portion of data you never used for tuning (out-of-sample validation).
- Be suspicious of win rates above 70% and equity curves that look too smooth.
- Always include commission and slippage in
strategy().
FAQ
What is the difference between strategy.exit and strategy.close?
strategy.exit registers stop loss and take profit orders that remain pending. strategy.close closes at market immediately, usually on an opposite signal.
Can I use more than one exit per entry?
Yes. You can scale out of a position using the qty_percent parameter in multiple strategy.exit calls pointing to the same entry.
Why doesn’t my order fill on the signal candle?
By default, orders are processed at the candle close and filled at the next candle’s open. That is more realistic. Enable calc_on_every_tick only for real-time testing.
Do backtest results apply to binary options?
Not directly. The Strategy Tester simulates buying/selling assets with stops and targets, not the fixed-payout format of binary options. Use it to validate the signal logic, not to project binary options returns.
Disclaimer: binary options and leveraged trading are very high-risk products and can result in the total loss of the invested capital. This content is strictly educational and does not constitute investment advice. Backtest results do not guarantee future performance. Always test any strategy on a demo account before trading with real money.
