AI-style ATR Breakout + EMA 200 — Pine Script v5 strategy
Example of a typical structure of an AI-generated strategy when asked for a "complete" Pine v5 strategy: a 200 EMA trend filter, an entry on a break of the 20-bar high and an ATR-based trailing stop.
Full code (free to copy)
//@version=5
strategy("AI Archetype EMA200 + Donchian + ATR", 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)
brkLen = input.int(20, "Breakout Bars", minval=10, maxval=55)
atrMult = input.float(2.5, "ATR Stop Mult", minval=1.2, maxval=4.5, step=0.25)
ema200 = ta.ema(close, 200)
upper = ta.highest(high[1], brkLen)
atrVal = ta.atr(14)
var float stopPx = na
if close > ema200 and close > upper and strategy.position_size == 0
strategy.entry("Long", strategy.long)
stopPx := close - atrMult * atrVal
if strategy.position_size > 0
stopPx := math.max(nz(stopPx), close - atrMult * atrVal)
strategy.exit("Exit", "Long", stop=stopPx)
plot(ema200, "EMA 200", color=color.blue)Judge this code on BTCUSDT 1D →Free, in your browser, on the same candles as the table below.
Online judge verdict on BTCUSDT (measured 29 November 2026)
| Timeframe | Sealed period | Trades | Return | Buy & hold | Verdict |
|---|---|---|---|---|---|
| 1d | 2024-12-17 → 2026-10-16 | 8 | -12.83 % | -23.25 % | Not enough trades to judge yet |
| 4h | 2026-03-12 → 2026-10-17 | 18 | -1.08 % | 15.83 % | No evidence of an edge, for now |
| 1h | 2026-08-05 → 2026-10-17 | 27 | 15.17 % | 27.14 % | No evidence of an edge, for now |
Past data, 10 bps per fill, last 20% of the bars sealed. Windows: full history on 1d, last 3 years on 4h, last year on 1h. Research, not advice: no result is promised.
This reading has not been compared with a real TradingView run.
Measured on 29 November 2026 with the online judge’s Pine v5 engine (sa-pine-vm 0.2.0), on Binance BTCUSDT candles up to 16/10/2026 on 1D and up to 17/10/2026 on 4h and 1h. Differences with TradingView’s Strategy Tester remain possible. Binance data, CC BY-NC-SA 4.0.
Open this code in the free judge (1 click) →
Replay each verdict yourself, in your browser: 1d · 4h · 1h. The script is filled in; nothing runs until you click “Judge”.
How the script works
ema200 = ta.ema(close, 200); upper = ta.highest(high[1], brkLen) with brkLen = 20, excluding the current bar; atrVal = ta.atr(14). The entry requires close > ema200, close > upper and no open position; the initial stop is close - atrMult × atrVal, with atrMult = 2.5.
While the position is open, stopPx := math.max(nz(stopPx), close - atrMult * atrVal) raises the stop without ever lowering it, and strategy.exit("Exit", "Long", stop=stopPx) places a stop order at that level.
Parameters in the code
| Parameter | Value | Role |
|---|---|---|
brkLen | 20 | Input "Breakout Bars" created with input.int, from 10 to 55. |
atrMult | 2.5 | Input "ATR Stop Mult" created with input.float, from 1.2 to 4.5. |
initial_capital | 10000 | Starting capital declared in strategy(). |
default_qty_type | percent_of_equity, 100 | Each entry commits 100% of available equity. |
commission_value | 0.1 (%) | 0.10% commission per order declared in the script; the judge applies it (10 bps per fill). |
process_orders_on_close | not set | Orders fill at the open of the next bar, the Pine default. |
Known limits of this rule
- This combination of round settings (200, 20, 2.5) is common; its presence in many examples says nothing about how it holds on data it has not seen.
- The stop is a real stop order: on a price gap, the fill can be below the level.
- Long only, and the 200 EMA needs 200 bars before the first signal.
Before you trust it: test it on data it never saw
A backtest in TradingView's Strategy Tester shows one past path with the settings you picked. Strategy Arena Lab (free, Windows and Linux) replays this Pine on your PC, compares its trades one by one with TradingView's for supported Pine, counts the variants you try, then judges the rule on a period kept sealed during the search: survives, no evidence of an edge or inconclusive.
FAQ
What does the online judge say about this script?
On BTCUSDT, with 10 bps fees per fill, measured on 29 November 2026: 1D: "Not enough trades to judge yet" (8 trades, -12.83% on the sealed period); 4h: "No evidence of an edge, for now" (18 trades, -1.08% on the sealed period); 1h: "No evidence of an edge, for now" (27 trades, 15.17% on the sealed period). No timeframe reaches "Survives". These verdicts concern past data and say nothing certain about the future.
How do I test the script my AI wrote?
Paste the AI's answer into the free online judge: it extracts the Pine block, replays it on BTCUSDT and judges it on a sealed period. This script serves as a starting example.
Can I change the script's settings?
Yes, the settings are inputs you can edit in TradingView. Each setting you try is one more trial: a setting chosen after seeing the sealed period is no longer a test on unseen data. The judge and the Lab count trials for this reason.
Other free Pine Script v5 strategies
More TradingView paths
Educational code on past data. No investment advice; no result is promised.