Pine Script Tutorial for Beginners
On this page
Short answer: Pine Script is TradingView's own programming language for indicators and strategies. To start, open the Pine Editor at the bottom of a chart, create a new indicator, paste a short script that begins with //@version=6, save it and click Add to chart. The examples below take you from a single moving average to a crossover indicator with inputs in about 20 lines.
You can write and run Pine Script on the free Basic plan. Paid plans only change limits such as how many indicators fit on one chart.
What Pine Script can and can't do
| You can build | Declared with | Example |
|---|---|---|
| Indicators | indicator() | Moving averages, oscillators, custom signals |
| Strategies | strategy() | Rules that place simulated orders and show a backtest report |
| Libraries | library() | Reusable functions you import into other scripts |
Pine runs on TradingView's servers, on the bars of your chart. It cannot place real orders by itself, read files on your computer or call web pages. To act on signals outside TradingView you use alerts, for example webhook alerts.
Step 1: open the Pine Editor
- Open any chart on TradingView.
- Click Pine Editor at the bottom of the screen.
- Open the menu next to the script name at the top-left of the editor and choose to create a new indicator.
The editor fills in a small template. Select everything and replace it with the code in the next step.
Step 2: your first indicator
This script draws a 20-period simple moving average on the price chart:
//@version=6
indicator("My first MA", overlay = true)
length = input.int(20, "Length", minval = 1)
ma = ta.sma(close, length)
plot(ma, "SMA", color = color.orange, linewidth = 2)Line by line:
- //@version=6 tells TradingView which version of the language to use. Version 6 is the current one in the official documentation.
- indicator("My first MA", overlay = true) names the script and draws it on top of price instead of in a separate pane.
- input.int(20, "Length", minval = 1) creates a setting the user can change from the indicator's settings window.
- ta.sma(close, length) calculates a simple moving average of closing prices. Built-in technical functions live in the ta namespace.
- plot() draws the result.
Save with Ctrl + S (Cmd + S on Mac), give it a name, then click Add to chart. Scripts you save are stored in your TradingView account and are private unless you publish them.
Step 3: two moving averages and a crossover
Now add a second average and mark where they cross:
//@version=6
indicator("Two MA cross", overlay = true)
fastLen = input.int(9, "Fast length", minval = 1)
slowLen = input.int(21, "Slow length", minval = 1)
fast = ta.ema(close, fastLen)
slow = ta.ema(close, slowLen)
plot(fast, "Fast EMA", color.teal)
plot(slow, "Slow EMA", color.orange)
crossUp = ta.crossover(fast, slow)
crossDown = ta.crossunder(fast, slow)
plotshape(crossUp, "Cross up", shape.triangleup, location.belowbar, color.green, size = size.small)
plotshape(crossDown, "Cross down", shape.triangledown, location.abovebar, color.red, size = size.small)ta.crossover(a, b) is true on the bar where a moves from below b to above it; ta.crossunder is the opposite. plotshape draws a small marker on those bars.
Because both averages are inside one script, this counts as one indicator toward your plan's limit, even though it draws two lines. On the free plan, with its limit of two indicators per chart, that matters. Our free plan guide has more ways to save slots.
Step 4: get an alert from your script
Add one line at the end to make the crossover available in the alert dialog:
alertcondition(crossUp, "Fast EMA crossed up", "{{ticker}}: fast EMA crossed above slow EMA")Saving the script does not create an alert by itself. After adding it to the chart, open the alert dialog, choose your script as the condition and pick "Fast EMA crossed up". Alerts on indicators need a paid plan. The alertcondition() guide explains the options.
Core ideas that confuse beginners
| Idea | What it means |
|---|---|
| Series | Most values, like close, have one value per bar. Your script runs once per bar, from the oldest to the newest. |
| History operator | close[1] is the previous bar's close; close[5] is five bars back. |
| Realtime bar | On the live bar, the script recalculates as price moves, so values can change until the bar closes. |
| Inputs | input.int, input.float, input.bool and similar create settings users can change without editing code. |
| Namespaces | Functions are grouped: ta for technical analysis, math for maths, color for colors, strategy for orders. |
Common beginner errors
- Missing version line. Without //@version=6 at the top, the code may be compiled with an older version and fail.
- Mixing up = and :=. Use = to declare a variable the first time and := to change it later.
- Indentation. Code inside an if block must be indented by four spaces or a tab.
- Calling a strategy function in an indicator. strategy.entry() only works in scripts declared with strategy().
- Expecting historic signals to match live ones. Signals based on the unfinished live bar can appear and disappear. Wait for the bar to close, or use barstate.isconfirmed in your conditions.
Where to go next
The official Pine Script documentation and the language reference list every function with examples. When you want to test rules on past data, move on to backtesting a strategy in Pine Script.
FAQ
Is Pine Script free?
Yes. The Pine Editor and Pine Script are available on every TradingView plan, including the free Basic plan. Plans differ in limits such as indicators per chart and calculation time.
Is Pine Script hard to learn?
It is simpler than general-purpose languages because it only works with chart data. If you can read a spreadsheet formula, you can follow the examples above.
Can Pine Script trade automatically?
Not directly. A strategy only simulates orders. To act on signals, send an alert to a broker connected to TradingView or to a service through a webhook.
Can I convert an MT4 or MT5 indicator to Pine Script?
Not automatically. MQL and Pine are different languages, so the logic has to be rewritten.
Keep learning
- Pine Script alertcondition() with examples
- Backtest a strategy in Pine Script
- Best TradingView indicators for scalping
Last checked against TradingView's official pages: October 2026. Nothing here is financial advice.