Skip to main content
← Back to blog

AI Wrote My Pine Script: How to Test It Out of Sample

📅 Published
✍️ Strategy Arena
ai pine script pine script chatgpt pine script claude pine script out of sample backtesting

Asking ChatGPT, Claude, or Gemini to write a Pine Script strategy takes seconds. Knowing whether the code holds up on unseen candles takes a stricter check than glancing at a TradingView equity curve.

Short answer: constrain your prompt to a clean Pine v5 strategy(...) with fees and equity sizing, paste it into Strategy Arena's free online judge, read the verdict on the 20% sealed period, and revise the hypothesis — not just the numbers — before testing a new version.

Step 1: Write a prompt that produces testable Pine v5

Left unconstrained, AI models often emit indicator(...) instead of strategy(...), pile on six filters, omit commissions, or insert calc_on_order_fills=true and tick trailing stops (trail_points). Give your AI clear structural rules first:

Write a complete Pine Script v5 strategy (not an indicator) for TradingView. Lead to test on past data: [describe 1 or 2 simple conditions in plain English]. Requirements:

  • Start with //@version=5 and one strategy(...) declaration.
  • Set pyramiding=0, initial_capital=10000, default_qty_type=strategy.percent_of_equity, default_qty_value=100.
  • Set commission_type=strategy.commission.percent, commission_value=0.1, process_orders_on_close=true.
  • Avoid calc_on_order_fills=true, loops, arrays, tick trailing stops, and request.security on another symbol.
  • Use standard indicators only (SMA, EMA, RSI, MACD, Bollinger Bands, ATR) with ta.crossover / ta.crossunder.
  • Keep entry and exit conditions mutually exclusive on the same bar.

Keeping the first draft simple lets you see why a rule holds or fails; every extra filter adds a parameter that can fit past noise.

Step 2: Paste the script into the free browser judge

Open the free online judge. No account is required, and WebAssembly runs the engine locally: your script is not sent to the server unless you choose to publish it on a share card.

  1. Paste the code or the full AI answer. If you paste the entire reply, the judge extracts the fenced code block containing strategy(...) automatically.
  2. Choose the candles. Use built-in BTCUSDT data (1D full history, 4h over 3 years, or 1h over 1 year), or drop your own OHLCV CSV (up to 60 MB, read locally).
  3. Click Judge. The engine splits history chronologically: oldest 80% for training statistics, most recent 20% sealed for the verdict.

When the judge refuses an AI script

The judge reads a subset of Pine v5 rather than every TradingView feature. If the AI wrote something unsupported, the judge refuses instead of guessing:

  • Exact line and reason. It names the line and why that construct is not carried.
  • One-click mechanical fixes. For prose around the code, calc_on_order_fills=true, or fixed/cash sizing in the header, a button inserts the cleaned script and re-runs it (with undo).
  • Indicator instead of strategy. If the AI wrote indicator(...), the page flags it and shows a strategy(...) skeleton.
  • Copy for AI. One click copies the refusal and your script so your AI can rewrite it cleanly.

Any edit creates a new script, so check the revised version in TradingView too.

Step 3: Read the verdict on the held-out period

Focus on the sealed 20% period and the four measured steps:

  1. Activity: at least 10 trades on training and 10 on the sealed period (30 independent events in certified mode).
  2. Gain vs holding: positive sealed return ahead of buy-and-hold over those dates.
  3. Trials counted: training Deflated Sharpe ratio of at least 95%.
  4. Chance: SPA test against buy-and-hold with at most 5 of 100 random markets doing as well.
Verdict displayed What it means on past data
Survives All 4 measured steps passed on the sealed period. Says nothing about the future.
No evidence of an edge yet Not all four steps passed (k of 4): the sealed result is neither clear nor stable enough to conclude.
No evidence of an edge, for now Loss on the sealed period, or a training Deflated Sharpe below 50%.
Not enough trades to judge yet Too few trades on training or sealed bars to judge.
This script is not read as written Unsupported Pine construct or missing strategy(...).

Check the execution summary too: fill timing (bar close vs next open), fees applied, and maximum sealed drawdown.

Step 4: Improve without overfitting

A weak first verdict is normal research, not a dead end. Avoid parameter-shopping on the sealed period: asking an AI to sweep EMA lengths or RSI thresholds until the same 20% window turns green destroys the holdout.

Follow a cleaner loop:

  • Check activity first. With too few trades on 1D, test the unchanged rule on 4h or 1h before changing conditions.
  • Change the hypothesis, not the knobs. State in one sentence what regime the rule assumes (trend, mean reversion, breakout); revise the idea rather than nudging one threshold.
  • Track versions locally. The judge keeps a local browser journal (never uploaded): pasting a revision with the same strategy("...") title shows whether it passed more steps, with a summary to copy back to your AI.
  • Check another market. Test the unchanged script on another asset via your own OHLCV CSV file.

When to switch to the TradingView audit dossier

If your AI script uses unsupported Pine constructs, or you already ran it in TradingView on stocks, indices, or forex, export List of trades as CSV and upload it to the TradingView audit dossier. CSV trade export starts on TradingView's Essential plan, not free Basic (pricing page, 20 November 2026; see our List of trades export guide).

  • Free preview: compounded result before and after a cut date (default: last 30%) and +0.10% extra fees per order.
  • Paid dossier (€9, one-time payment): adds Monte Carlo on trade order, holding comparison and drawdown, sub-periods, and one improvement lead to test on past data with an AI prompt.

The dossier does not replay your rule: it analyzes exported trades only. Preview it on the example dossier (77 synthetic trades).

Sources

Frequently asked questions

Why does the judge refuse some AI-generated Pine scripts? AI models often output indicator() instead of strategy(), size with a fixed quantity, add calc_on_order_fills=true, or use unsupported loops and tick trailing stops. The judge refuses instead of guessing, names the line, and lets you copy a fix request for your AI.

Can I paste the entire AI chat answer into the online judge? Yes. If the pasted text includes a fenced code block with a line that starts with strategy(...), the browser judge extracts that block automatically and ignores the surrounding prose.

What should I do if my AI script gets No evidence of an edge? Avoid tweaking numbers until the sealed period looks better. State what the rule assumes about the market, test a simpler hypothesis, or run the unchanged rule on another timeframe or another market via a CSV candle file.

What if my AI-written script cannot be replayed by the online judge? If you ran it in TradingView on an Essential plan or higher, export the List of trades CSV and upload it to the audit dossier. It does not replay the rule, but its free preview checks results before and after a cut date and with extra fees.


Research on past data, not investment advice. Strategy Arena gives no investment advice, places no broker orders, and is not affiliated with TradingView. TradingView plan features are quoted from its pricing page on 20 November 2026 and may change.

⚠️ Disclaimer — This article is for informational and educational purposes only. It does not constitute investment advice or a buy/sell recommendation. Past performance does not guarantee future results. Strategy Arena is an educational simulator with virtual capital. Always do your own research before making investment decisions.

Enjoyed this article? Share it

𝕏 Share on X ✈️ Telegram

Turn a local result into durable proof

The Lab runs free on your PC. Certificates and audit dossiers use credits: 1 credit = 1 certificate or 1 dossier, 2 offered when you open an account; paying never changes the verdict.

Get the Lab (free)Audit a TradingView export Pricing and credits Publish a Lab Report Lab Reports Hall
TradingView Starter
Choose the account path. Prove the strategy separately.
Your first chart and price alert do not require the Lab. Strategy Arena proof remains required before trusting or exporting a strategy.
Partner attribution is for first-time users and web purchases only. Strategy Arena may earn a commission on eligible purchases; starting with a free account needs no payment.
I have never had an account · Start free I already have an account Check a strategy
More TradingView paths
Evidence-gated Pine review