How to convert a TradingView indicator into a strategy (and test it out of sample)
To convert a Pine Script indicator into a strategy, replace indicator() with strategy() and give it a size, a commission and a slippage; turn the conditions behind its plotshape or alertcondition signals into strategy.entry() and strategy.close() calls; add strategy.exit() for a stop or a target; and make sure nothing repaints. You then get real trades in the Strategy Tester, net of fees, which you can judge on data the rule has not seen.
indicator() and strategy(): what changes
An indicator() computes and draws: moving averages, arrows, background colours, alerts. It never decides a position size or an exit, so TradingView has nothing to count. A strategy() runs the same calculations and also places simulated orders. That is what unlocks the Strategy Tester: the list of trades, net profit after commission, maximum drawdown, and the List of trades export.
indicator() | strategy() | |
|---|---|---|
| Orders | No order: only plots, shapes and alerts | strategy.entry, strategy.close, strategy.exit |
| Size, fees, slippage | Not defined | Set in the strategy() line, editable in the properties |
| Strategy Tester and List of trades | No | Yes |
| Free judge | Gives you a 10-line template to turn it into a strategy | Replayed, verdict on a sealed period |
The 10-line template the judge shows
Paste an indicator into the judge and it gives you this template: it turns any indicator into a strategy you can backtest, without touching its code. It reads one of its outputs with input.source and leaves the two conditions to you, so the rule stays yours: the judge never turns an indicator’s signals into orders on your behalf.
//@version=5
strategy("My indicator as a strategy", overlay=true) // size, fees, capital: yours to define
// Add your indicator to the same chart, then pick one of its outputs (plot) in the settings.
signal = input.source(close, "Indicator output")
enterLong = false // yours to define: your entry condition, written with signal
exitLong = false // yours to define: your exit condition
if enterLong
strategy.entry("Long", strategy.long)
if exitLong
strategy.close("Long")
This template runs in TradingView, next to your indicator, and gives you a real List of trades: export it from the Strategy Tester and the free dossier preview shows the result before and after your cut date, with fees. The online judge replays scripts on its own candles, so it does not read another script’s output. If you can rewrite the indicator’s logic inside the strategy with built-in functions (ta.sma, ta.rsi, ta.crossover…), as in the example below, the judge can replay it directly.
The conversion, step by step
1. Replace indicator( with strategy(
Change the declaration line and give the strategy its settings: initial_capital, default_qty_type and default_qty_value for the size, commission_type and commission_value for the fees, slippage in ticks.
strategy("MA cross strategy", overlay=true, initial_capital=10000,
default_qty_type=strategy.percent_of_equity, default_qty_value=100,
commission_type=strategy.commission.percent, commission_value=0.1, slippage=2)
Without these settings TradingView trades 1 contract with zero commission: the curve then looks better than any account would. 0.1% per order and 2 ticks of slippage are examples; use your broker’s real costs (the fee calculator shows what they do to a result).
2. Turn the signals into orders
Keep the conditions that fed plotshape or alertcondition, and under each one call strategy.entry to open a position and strategy.close to leave it. Remove the alertcondition lines: they have no role in a strategy.
goLong = ta.crossover(fast, slow)
goFlat = ta.crossunder(fast, slow)
if goLong
strategy.entry("Long", strategy.long)
if goFlat
strategy.close("Long")
Compute the conditions at the top level of the script, as the indicator did, then use them under if. The id ("Long") links the entry to its exit. plotshape can stay to keep the arrows on the chart.
3. Add a stop and a target with strategy.exit
If the rule has a stop-loss or a take-profit, write it with strategy.exit, linked to the entry by its id (from_entry), with stop and limit prices.
strategy.exit("Stop / target", from_entry="Long", stop=strategy.position_avg_price * 0.95, limit=strategy.position_avg_price * 1.10)
Here a stop 5% below the entry price and a target 10% above. While no position is open, strategy.position_avg_price is na and no exit order is placed.
4. Remove what repaints
With request.security, read the higher timeframe on its last closed bar ([1] with lookahead_on). Keep orders on confirmed bars: leave calc_on_every_tick off, or test barstate.isconfirmed.
dailyMa = request.security(syminfo.tickerid, "D", ta.sma(close, 50)[1], lookahead=barmerge.lookahead_on)
lookahead_on without the [1] would let historical bars read the daily close before it exists: the backtest would use the future. By default a strategy computes once per closed bar; barstate.isconfirmed matters mostly if you turn on calc_on_every_tick or in the indicator’s alerts.
5. Judge it on a period it has not seen
Paste the strategy into the free judge: it replays the script and gives a verdict on the sealed last 20% of the candles, fees included. If the strategy already runs in TradingView, export its List of trades for the free dossier preview.
Full example: a moving-average crossover, indicator → strategy
Before: the indicator (Pine v6)
//@version=6
indicator("MA cross signals", overlay=true)
fastLen = input.int(20, "Fast MA length", minval=1)
slowLen = input.int(50, "Slow MA length", minval=2)
fast = ta.sma(close, fastLen)
slow = ta.sma(close, slowLen)
plot(fast, "Fast MA", color=color.teal)
plot(slow, "Slow MA", color=color.orange)
goLong = ta.crossover(fast, slow)
goFlat = ta.crossunder(fast, slow)
plotshape(goLong, title="Buy", style=shape.triangleup, location=location.belowbar, color=color.green, size=size.small)
plotshape(goFlat, title="Sell", style=shape.triangledown, location=location.abovebar, color=color.red, size=size.small)
alertcondition(goLong, "Buy", "Fast MA crossed above slow MA")
alertcondition(goFlat, "Sell", "Fast MA crossed below slow MA")
After: the strategy (Pine v6)
//@version=6
strategy("MA cross strategy", overlay=true, initial_capital=10000, default_qty_type=strategy.percent_of_equity, default_qty_value=100, commission_type=strategy.commission.percent, commission_value=0.1, slippage=2)
fastLen = input.int(20, "Fast MA length", minval=1)
slowLen = input.int(50, "Slow MA length", minval=2)
fast = ta.sma(close, fastLen)
slow = ta.sma(close, slowLen)
plot(fast, "Fast MA", color=color.teal)
plot(slow, "Slow MA", color=color.orange)
goLong = ta.crossover(fast, slow)
goFlat = ta.crossunder(fast, slow)
plotshape(goLong, title="Buy", style=shape.triangleup, location=location.belowbar, color=color.green, size=size.small)
plotshape(goFlat, title="Sell", style=shape.triangledown, location=location.abovebar, color=color.red, size=size.small)
if goLong
strategy.entry("Long", strategy.long)
if goFlat
strategy.close("Long")
What changed: the declaration line (size, commission, slippage), the two alertcondition lines replaced by strategy.entry and strategy.close. The calculations and the arrows are the same. Optional: add the strategy.exit line of step 3 at the end for a stop and a target. Both versions of the strategy are read by the free judge.
Mistakes that flatter the Strategy Tester curve
- Zero commission. A rule with many trades can go from gain to loss once each order pays its fee.
- Future data through request.security.
lookahead_onwithout an offset of one bar reads closes that did not exist yet. - calc_on_every_tick on historical bars. In history the script only sees closed bars; in real time it would react within the bar. The two runs then differ.
- Unintended pyramiding. With
pyramidingabove 1, every repeated signal adds to the position and multiplies the risk. - Entries without an exit. A position that never closes before the end of the chart makes one huge open trade, not a rule.
- Settings tuned on the whole history. The parameters were chosen on the same bars as the result. Only a period kept aside tells whether the rule holds outside them.
What to do next: two free checks
Paste the strategy into the free judge
The judge replays your Pine strategy in your browser on real BTCUSDT candles (1 hour, 4 hours or 1 day). The last 20% of the candles stay sealed, and the verdict rests on that period only, with the script’s fees, against simply holding. If something is not read, it says which line and why. No account, and your script is not sent.
What the judge reads in the strategy() line: a commission in percent (strategy.commission.percent), a size in percent of equity up to 100, one position per direction (no pyramiding). A slippage in ticks is counted as an extra fee.
Already running in TradingView? Export the List of trades
For any market TradingView charts, or a strategy that reads another indicator, open the Strategy Tester, the List of trades tab, and export the CSV. The free dossier preview shows the compounded result before and after the date up to which you tuned the strategy, and the whole period with 0.10% extra fees per order. No account and no Pine script needed.
The export button of the List of trades needs a paid TradingView plan; on the free plan, paste your Pine script into the free judge above.
What you gain: before risking money, you see whether the rule still holds on candles it has not seen, once fees are paid.
Questions
What is the difference between indicator() and strategy() in Pine Script?
An indicator() draws lines, shapes and alerts on the chart; it places no order, so TradingView has no trades to report. A strategy() places simulated orders with strategy.entry, strategy.close and strategy.exit, and the Strategy Tester then gives a list of trades, net profit with commission, drawdown and an exportable List of trades.
Can the free judge test my indicator?
Yes, once it places orders. Paste the indicator and the judge gives you a 10-line strategy() template: you write the entries and exits, so the size, the exit and the fees stay your rule, and the script can then be judged on a sealed period. The judge never reads an indicator’s arrows or alerts as orders on your behalf.
Can I convert an indicator whose source code is hidden?
You cannot edit its code, but if its outputs appear in the source list of an input, add it and your strategy to the same chart and read one of its plots with input.source, as the judge’s template does. The online judge does not see that other script, so test this kind of strategy from its List of trades export on the dossier page.
Why do the strategy’s trades differ from the indicator’s arrows?
Three usual reasons. By default a strategy fills its order at the open of the next bar, not at the close of the signal bar (process_orders_on_close changes that). It holds one position per direction unless pyramiding allows more, so repeated signals are ignored. And an indicator that repaints shows arrows that were not there in real time.
Pine v5 or v6: which one should I use?
Both work. The judge’s template is written in v5 and the full example on this page in v6; the judge reads both versions. TradingView converts a v5 script to v6 from the Pine Editor if you want the latest version.
Does a good backtest mean the strategy will work?
No. A backtest describes past data, often the same data the settings were chosen on. That is why the judge keeps the last 20% of the candles sealed and gives its verdict on that period only, fees included, against simply holding. Research on past data, not advice.
See also: how to run an out-of-sample test on TradingView
Research on past data. Strategy Arena gives no investment advice and does not trade for you. Past results do not predict future results. TradingView and Pine Script are trademarks of TradingView, Inc.