ReviewsTrading concept
Pine Script: A Beginner's Guide to the TradingView Language, With Two Examples to Copy
What the language does, where its editor sits, how code is built and added to a chart, and two short v6 examples for pattern traders, each followed by what our test bench measured for the same rule.

On this page
- What is Pine Script, and what can it do?
- Pine Script v6: the current version
- Where is the Pine Editor in TradingView, and how to open it
- How to add a Pine Script to TradingView
- Script 1: a bullish engulfing indicator with an alert
- Script 2: a Pine Script strategy with commission and slippage
- Plans, the Pine Screener and two traps
- Pine Script and AI
- Where to go next
- Frequently asked questions
Pine Script is the programming language of TradingView: you write a few lines in the Pine Editor, the platform runs them on its servers, and the result shows on your chart as an indicator, or as a strategy that you can backtest. The current version is Pine Script v6, it is included on every plan, the free Basic plan too, and a useful script fits in 15 lines. Below are two short scripts to copy, each followed by what our own test bench measured for the same rule, because detecting a pattern says nothing about whether the pattern pays. Every fact about the platform comes from its documentation, its reference manual, its help center and its pricing page, read on December 6, 2026.
What is Pine Script, and what can it do?
The help center describes it as a lightweight language for creating custom indicators and trading strategies. Its documentation says it is kept accessible to the broadest audience. An indicator calculates on every bar and draws on the chart; a strategy also simulates orders and reports the backtest. Both can feed TradingView alerts. It is cloud-based, which is why limits apply to execution time and memory usage. Most built-in indicators of the platform are written in Pine Script, and the community has published more than 150,000 scripts, half of them open-source: you can search them from the Indicators, metrics, and strategies dialog.
What it cannot do is trade for you. Orders go to what the documentation calls a broker emulator, which simulates trades and shows hypothetical performance. Only an alert leaves the screen; on paid plans a webhook can forward it to an outside service, which some traders use for algorithmic trading, at their own risk: the help center warns that webhooks may occasionally fail to reach their URL.
Pine Script v6: the current version
The release notes date Pine Script v6 to November 2024 and state that future updates go exclusively to it. Older code can be upgraded with the converter of the Manage Script menu, sometimes with fixes by hand; new features need v6.
Where is the Pine Editor in TradingView, and how to open it
Click the Pine icon on the right toolbar of a chart: that is the wording of the help center. A floating panel opens by default, which you can move, resize, dock to the right side or detach into a separate browser window.
How to add a Pine Script to TradingView
- Start a new file. Click the arrow next to the current name, then Create new, then Indicator (or Strategy for script 2).
- Paste. Select everything and replace it with the script you copied.
- Save and choose a name. Your work is saved online, and only you can see it.
- Click Add to chart. An indicator draws on the chart or in a pane under it; a strategy also brings up its report.
The first steps on the platform are in our guide on how to use TradingView.
Script 1: a bullish engulfing indicator with an alert
A candlestick pattern is the easiest place to start coding, because its rule compares a few prices. This one marks a bullish engulfing candle with a green triangle under the bar.
//@version=6
indicator("Bullish engulfing marker", overlay = true)
// Candle 1 closes below its open, candle 2 closes above its open.
firstIsDown = close[1] < open[1]
secondIsUp = close > open
// The body of candle 2 covers the body of candle 1 and is larger on at least one side.
coversBody = open <= close[1] and close >= open[1]
isLarger = open < close[1] or close > open[1]
bullishEngulfing = firstIsDown and secondIsUp and coversBody and isLarger
plotshape(bullishEngulfing, title = "Bullish engulfing", style = shape.triangleup, location = location.belowbar, color = color.green, size = size.small)
alertcondition(bullishEngulfing, title = "Bullish engulfing", message = "Bullish engulfing candle")
| Part | Line | What it does |
|---|---|---|
| Annotation | //@version=6 | Tells the compiler which release to use |
| Declaration | indicator(...) | Sets the type and the name; overlay = true draws on the prices instead of a separate pane |
| Calculations | close[1] < open[1] | open and close belong to the current bar; [1] reads one bar back. On past bars the script runs once per bar, from the first to the last |
| Output | plotshape(...) | Draws a shape on every bar where the condition is true |
| Alert condition | alertcondition(...) | Adds the pattern to the Condition menu of the alert dialog; it does not set the alert itself |
What our bench found for this pattern
We counted every case. On daily stock data, our bench found 45,460 bullish engulfing patterns. Ten bars later, the price was higher 53.7% of the time, against 53.8% for random bars of the same stocks. Our verdict: No clear edge.
Our bench is stricter than the marker: it also requires a real downtrend first and a first candle that is not a doji. The lesson holds either way: writing the rule is the easy half, and whether it beats random entries is a separate question, answered in do chart patterns work.
Script 2: a Pine Script strategy with commission and slippage
This example simulates the trades of a common rule: buy when the relative strength index over 14 bars crosses under 30, sell when it crosses back over 50.
//@version=6
strategy("RSI under 30, exit over 50", overlay = false, initial_capital = 10000, default_qty_type = strategy.percent_of_equity, default_qty_value = 10, commission_type = strategy.commission.percent, commission_value = 0.1, slippage = 2)
rsiLength = input.int(14, "RSI length", minval = 1)
buyLevel = input.float(30, "Buy when RSI crosses under")
sellLevel = input.float(50, "Sell when RSI crosses over")
rsiValue = ta.rsi(close, rsiLength)
buySignal = ta.crossunder(rsiValue, buyLevel)
sellSignal = ta.crossover(rsiValue, sellLevel)
if buySignal
strategy.entry("Long", strategy.long)
if sellSignal
strategy.close("Long")
plot(rsiValue, title = "RSI")
hline(buyLevel, title = "Buy level")
hline(sellLevel, title = "Exit level")
The declaration carries the settings: a starting capital of 10,000, orders sized at 10% of equity, a commission of 0.1% and a slippage of 2 ticks. Those figures are placeholders: replace them with the rates of your broker (see the TradingView brokers that connect), but do not delete them. The documentation says that if no commission is specified, none is applied, and that the default slippage is 0.
When an order triggered at the close can fill

Reading the report, with its list of trades, is covered step by step in our guide to TradingView backtesting.
What our bench found for this RSI rule
RSI(14) on the daily S&P 500 ETF: script 2 buys under 30 and sells over 50

- Price, daily candles (hollow up, filled down)
- RSI(14), Wilder averages (solid line)
Our bench ran this rule on our daily data for stocks, ETFs, forex, crypto and commodities, up to October 30, 2026, with costs, and compared each trade with the other assets of the same market over the same bars. On 93,150 signals, it made +2.24% on average, 0.53% more than the market.
Plans, the Pine Screener and two traps
Writing and running both scripts costs nothing; the plan sets how many indicators fit on a chart, how much history they see and whether you get alerts. Our guide to the TradingView plans has the figures. The Pine Screener, which filters a watchlist with one indicator such as script 1, is covered with the TradingView screener; free users can open it but not run a scan.
| Trap | What the documentation says | What to do |
|---|---|---|
| Repainting | Behavior that makes historical and realtime calculations differ; a value fluctuates on the realtime bar until it closes | Act on finished bars |
| Future leak | request.security() with lookahead = barmerge.lookahead_on and no [1] offset returns future data on historical bars | Leave lookahead off |
Pine Script and AI
The help center is blunt: AI-generated code often mixes releases or invents functions, and what runs may still not do what you asked. Read every line; our guide to AI trading shows where an assistant helps, and our guide to the TradingView MCP server covers the official connection for chatbots.
Where to go next
After a script that compiles comes a check you can trust: see our pages on the strategy tester, on backtesting and on TradingView paper trading, or our TradingView alternatives and other trading reviews.
Frequently asked questions
What is Pine Script?
The programming language of TradingView, used to draw an indicator, simulate a strategy on past bars or define alert conditions. The current release is Pine Script v6, included on every plan, the free one too.
How do I get to the TradingView Pine Editor?
Click the Pine icon on the right toolbar. A floating panel opens by default, which you can dock, detach or move to the bottom.
Can I automate trading in TradingView?
Not with code alone: orders go to a broker emulator, which simulates them. An alert can fire on each simulated fill, and a webhook, listed from the Essential plan, can send it to an outside service that places the order.
Can ChatGPT write Pine Script?
It can write a draft. The help center warns that AI-generated scripts often mix releases or rely on functions that do not exist. Check every function in the reference manual.
Sources
- TradingView: Pine Script v6 user manual
- TradingView: Pine Script v6 reference manual
- Pine Script primer: First indicator
- Pine Script release notes
- Pine Script documentation: Strategies
- Pine Script documentation: Alerts
- Pine Script documentation: Repainting
- TradingView help: What is Pine Script
- TradingView help: How to work with Pine Editor
- TradingView help: How to create a new script
- TradingView help: Will AI help me write and publish Pine Script indicators?
- TradingView help: Pine Screener, key features and requirements
- TradingView help: How to configure webhook alerts
- TradingView help: Strategy Alerts
- TradingView pricing and plan comparison
