How to use AI to write Pine Script — and catch the mistakes it makes
AI can draft a TradingView indicator in seconds. It can also hand you old code, made-up functions and a script that quietly peeks into the future. Here's how to get the good part without the traps.
Why AI and Pine Script fit well
Pine Script is TradingView's small coding language for indicators, alerts and strategy tests. It's narrow and well documented, so AI chat assistants (like ChatGPT, Claude or Gemini) can often write a working draft.
That's a real time-saver. But "a draft" is the key word. You still have to read it, run it and test it. New to Pine? Start with Pine Script for beginners — 10 minutes there makes everything below easier.
You'll need a computer: the Pine Editor isn't in the TradingView phone app.
Step 1: Write a spec before you ask
Most bad AI code comes from vague requests. "Make me a breakout indicator" could mean a hundred things.
Write down, in plain words:
- What it marks. "A green triangle under a candle that closes above the highest high of the previous 20 candles."
- Which candles count. "Only finished candles — not the one still forming."
- Settings. "Let me change the 20."
- Alerts. "Give me an alert when it happens."
- Version. "Pine Script version 6."
Step 2: Use this prompt
Write a TradingView Pine Script version 6 indicator. Start with //@version=6 and use indicator(), not study().
What it does: [your spec in plain words].
Rules: only use functions that exist in Pine Script v6. No request.security with lookahead turned on. Signals must only appear on confirmed (closed) candles.
After the code, explain every line in one plain sentence each, and list anything you are unsure about.
The last line matters. Asking it to list what it's unsure about often surfaces the bug before you hit it.
An example AI-style draft, explained
Here's the kind of short Pine v6 script that prompt should produce:
//@version=6
indicator("Close above recent high", overlay=true)
length = input.int(20, "Lookback candles")
prevHigh = ta.highest(high, length)[1]
breakUp = close > prevHigh and barstate.isconfirmed
plot(prevHigh, "Recent high", color=color.gray)
plotshape(breakUp, "Close above", shape.triangleup, location.belowbar, color.green)
alertcondition(breakUp, "Close above recent high", "A candle closed above the recent high")
Line by line, in plain words:
- Line 1 says this is Pine Script version 6.
- Line 2 names it and puts it on the price chart.
- Line 3 makes "Lookback candles" a setting, starting at 20.
- Line 4 finds the highest high of the 20 candles before this one. The [1] means "one candle back", so the current candle isn't compared with itself.
- Line 5 is true when the candle closes above that high and the candle has finished. That second part stops signals flashing on and off while a candle is still forming.
- Line 6 draws the recent high as a grey line.
- Line 7 draws a green triangle under each candle where it happened.
- Line 8 lets you set an alert. See Pine Script alerts.
It's a body-close idea in code: only the close counts, not a wick poking through. It's a tool to show information — not a signal to trade.
The 6 mistakes AI makes most
1. Old versions
AI learned from years of old scripts. You may get study() (version 4) instead of indicator(), or security() instead of request.security(). If the first line isn't //@version=6, ask it to rewrite for v6.
2. Made-up functions
AI sometimes invents a function that sounds right but doesn't exist. If the editor says a function is unknown, look it up in TradingView's Pine Script reference manual. Real example: turning points are found with ta.pivothigh() and ta.pivotlow().
3. Peeking into the future
This is the dangerous one. Code that pulls data from another timeframe with lookahead turned on can use a candle's final value before that candle has closed. On past charts it looks amazing. Live, it can't do that. Read Pine Script request.security before using other timeframes.
4. Signals that vanish
A signal based on the candle that's still forming can appear, then disappear before it closes. On past charts you only see the final version, so the script looks better than it acts. The barstate.isconfirmed check in the example fixes this.
5. Silent logic errors
The code runs, but does the wrong thing — like > where you meant >=, or comparing a candle with itself. Only checking it on a chart catches these.
6. Too much at once
Ask for a "complete trading system" and you get 200 lines you can't check. Ask for one small feature at a time.
It compiled is not it works. No red error message only means the code is valid Pine. It doesn't mean it does what you meant, or that the idea is any good.
Step 3: Test it properly
- Paste and save in the Pine Editor. Fix errors one at a time — paste the exact error back to the AI and ask what it means.
- Check 10 signals by eye. Scroll back and look at each triangle. Did that candle really close above the line?
- Use bar replay. Step forward candle by candle. Do signals appear only when a candle closes, and stay put?
- Change one setting. Does it behave the way you expect at 10 and at 50?
If you turn it into a strategy test, remember those results are hypothetical. Small changes that make the past look great often fail on new data. See Pine Script strategy backtest.
Grow it in small steps
Once the basic version works, add one thing per prompt:
- "Make the triangle colour a setting." (See Pine Script inputs.)
- "Add a red triangle above candles that close below the recent low."
- "Only show signals between 9:30 am and 11:30 am ET."
Save a copy before each change. If the new version breaks, you can go back.
If you'd like a full, finished indicator to learn from, our course includes the complete source code of the StructureX chart tool to study and customize. It's source code, not a ready-made trading bot.
What AI can't do for your script
- Tell you if the idea behind it works.
- Guarantee it matches what you meant.
- Place trades safely on its own — automation needs a broker connection and careful testing, and losses are still possible.
Questions
Can ChatGPT write Pine Script?
Yes, it can draft working Pine Script, especially short indicators. It often uses old versions or made-up functions, so you need to run it, read it and test it.
Why does AI-written Pine Script give errors?
Common causes are old version syntax (study() instead of indicator()), functions that don't exist and small typos. Paste the exact error back and ask what it means.
Can I trust a backtest of AI-written code?
Be careful. Code that peeks at future data or signals that change while a candle forms can make past results look far better than live. Test on replay and treat results as hypothetical.
Do I need to learn Pine Script if AI can write it?
Learning the basics helps a lot. You can't catch AI's mistakes in code you can't read.
Want the next step? We'll walk you through it.
Four free lessons by email, one every few days — each with a 10-minute exercise on a real chart.
Send Me the Free Lessons 4 short emails · No spam · Unsubscribe anytimeOr see the whole method, step by step:
See What’s Inside the Course Try everything free first. Decide when you're ready. · Open the free chart tool (works in the TradingView phone app)One chart idea a day, in plain English. Follow along: