AI tools write clean Pine Script. They do not write code that survives live execution. Here is what breaks and how to catch it before it costs you.
AI is excellent at producing clean, compilable Pine Script. It is terrible at respecting the messy realities of live execution. That gap is where most traders are getting hurt.
Below is a checklist you can apply to any AI-generated strategy before you risk real money.
1. Your backtest is lying about fills
What happens in backtest
- Pine processes your signal at bar close.
- By default, the earliest fill is the open of the next bar.
- No slippage, no commission, perfect liquidity.
What happens live
- Alert fires at bar close.
- Webhook → execution layer → broker → exchange.
- Price moves during that entire chain.
On fast timeframes or thin markets, 0.5–2% slippage per trade is common. Any strategy with a small statistical edge is instantly destroyed.
What to do
- In TradingView, open the Strategy Tester, click the gear icon on your strategy, and go to the Properties tab. That's where slippage and commission live
- Set realistic slippage and commission before you trust any backtest.
- Typical starting point for retail:
- Slippage: 0.05% – 0.15% (5–15 bps) per side.
- Commission: your broker’s actual fee schedule.
- Typical starting point for retail:
- Rerun the backtest. If the edge disappears, the strategy was never viable.
2. Repainting: the silent backtest killer
Repainting means your strategy uses information that wasn’t actually known at the time the signal supposedly fired.
Common culprit: request.security() on higher timeframes.
- Non-repainting pattern requires:
- A
[1]offset on the data expression, and barmerge.lookahead_onused correctly.
- A
- Misusing lookahead or omitting the offset lets future data leak into the past, making the backtest fantasy.
AI models copy patterns from public code, including bad ones. They don’t know they’re introducing repainting.
What to do
- Use Bar Replay or paper trading on TradingView.
- Compare:
- Signals you see live / in replay, vs.
- Signals shown on the historical chart for the same period.
- If they don’t match, the strategy repaints. Discard or fix it before proceeding.
3. “Buy at close” vs. “next bar open”
Most AI scripts do something like:
if longCondition
strategy.entry("Long", strategy.long)By default, in backtest this fills at the next bar’s open. In a webhook setup, your alert also fires at close and your broker fills as soon as possible after that – which is roughly what the default backtest is modeling.
The trap: enabling process_orders_on_close = true.
- This makes the backtest behave as if you can fill at the close.
- In live trading, you still can’t guarantee that price.
- TradingView explicitly warns this can effectively cause repainting in live environments.
What to do
- Design strategies assuming fills at next-bar open.
- Avoid
process_orders_on_close = truefor webhook-based live trading unless you have a very specific, tested reason.
4. Position sizing that ignores real account state
AI-generated Pine often does:
qty = strategy.equity * 0.02 / close
strategy.entry(“Long”, strategy.long, qty)This assumes the script knows:
- Your true account balance.
- Your open positions across all symbols.
- Your margin usage and rejected orders.
In a webhook → broker setup, Pine knows none of that. It just keeps sending orders based on its own fantasy equity curve.
What to do
- Treat Pine as a signal generator, not a portfolio manager.
- Move all position sizing into your execution layer (e.g., RelayDesk):
- Use live broker data for equity, margin, and open positions.
- Apply per-bot risk rules (max position size, max daily loss, etc.).
- Pine alerts should say what to do (long/short/flat), not how big
5. Regime dependence: works great… until it doesn't
A strategy that crushes BTC from 2022–2024 might simply be overfit to:
- High volatility
- Strong directional trends
Once the market shifts to low-vol chop, the same logic bleeds out.
AI optimizes for the data you implicitly specify. "Works on BTC" usually means "overfit to BTC's recent regime."
What to do
- Test across at least three regimes:
- Strong uptrend / downtrend
- Sideways chop
- Sharp drawdown / crash
- If performance collapses in any one regime, you have a regime-specific strategy.
- That's not automatically bad.
- But you must have a regime filter or a clear rule for when the strategy is allowed to trade.
6. A practical pre-live validation workflow
Run every AI-generated Pine strategy through this pipeline:
- Slippage & commission -- Set realistic values in Strategy Properties. Rerun the backtest; discard strategies that lose their edge.
- Repainting check -- Use Bar Replay or paper trading. Confirm live/replay signals match historical chart signals.
- Order fill behavior -- Use default behavior (fills at next-bar open). Avoid
process_orders_on_close = truefor webhook live trading. - Regime robustness -- Test on at least three distinct market regimes. Understand where the strategy fails and when it should be off.
- Execution-layer sizing -- Strip sizing logic out of Pine. Let your execution layer (e.g., RelayDesk) size trades based on live account state.
- Paper trade end-to-end -- Run the exact webhook + broker setup you plan to use. Paper trade for at least two weeks. Compare live paper results vs. backtest; investigate any divergence.
Most AI strategies fail at step 1 or 2. That's a success: you just saved real money.
Where AI actually shines
AI is genuinely useful for:
- Turning a verbal idea into valid Pine v6 code.
- Refactoring or documenting existing scripts.
- Generating boilerplate for indicators, alerts, and plotting.
What it cannot know:
- Your broker's routing and fill behavior.
- Your execution layer's state handling.
- Real-world slippage, liquidity, and latency.
That's your job -- or your execution layer's.
If you're running AI-generated Pine through RelayDesk:
- Use paper trading bots with realistic slippage/commission.
- Let RelayDesk handle sizing, risk limits, and broker state.
- Promote a strategy to live only after it survives the full validation workflow above.
The edge isn't in having AI write Pine. The edge is in being one of the few traders who insists that the code survives real-world execution tests before it ever touches a live account.