A Pine Script tutorial.

Build your first TradingView indicator in Pine Script: the structure of a script, plotting, indicators versus strategies and alerts, with a working example you can paste in.

Sculptural head crowned by a glowing slab of market-green light

Pine Script is TradingView's language for building indicators and strategies right on the chart. This tutorial walks through the structure of a script, taking inputs, calculating values, plotting to the chart and adding alerts, and ends with a complete Pine v6 indicator you can paste into the Pine Editor and run.

What is Pine Script?

Pine Script is the programming language built into TradingView. You write it in the Pine Editor at the bottom of the chart, and it runs on TradingView's servers to draw indicators, backtest strategies and fire alerts. It is concise and purpose-built for charts, so you can get a working indicator on screen in a handful of lines. The current version is Pine Script v6.

Before you start

You need very little to begin:

The structure of a Pine script

Every script follows the same shape: a version annotation, a declaration of whether it is an indicator or a strategy, then your inputs, calculations and outputs. Understanding these four parts is most of the language:

Build an indicator, step by step

  1. Open the Pine Editor

    On any TradingView chart, open the Pine Editor tab, then choose to create a new indicator. You get a small starter script you can replace.

  2. Declare the version and type

    Start with //@version=6, then indicator("My Indicator", overlay=true). The overlay=true draws on the price chart rather than in a separate pane.

  3. Take inputs

    Expose the settings you want adjustable, such as moving-average lengths, with input.int(). Inputs appear in the indicator's settings dialog.

  4. Calculate your values

    Use the built-in technical-analysis library: ta.ema(), ta.sma(), ta.rsi() and so on. Each returns a series you can plot or test.

  5. Plot to the chart

    Draw lines with plot() and markers with plotshape(). Colour and style are arguments, so a signal can be a green triangle under the bar.

  6. Add alerts

    Use alertcondition() so users can set a TradingView alert on your signal, which can then be sent onward as a webhook.

  7. Save and add to the chart

    Save the script, then add it to the chart to see it run. Adjust inputs from the settings dialog and iterate.

A complete Pine v6 indicator

Here is a full, working EMA crossover indicator: it plots two moving averages and marks where they cross, and it exposes an alert. Paste it into the Pine Editor and add it to a chart.

EMA crossover · Pine Script v6
//@version=6
indicator("EMA Crossover", overlay=true)

fastLen = input.int(12, "Fast EMA")
slowLen = input.int(26, "Slow EMA")

fast = ta.ema(close, fastLen)
slow = ta.ema(close, slowLen)

plot(fast, "Fast EMA", color.aqua)
plot(slow, "Slow EMA", color.orange)

crossUp = ta.crossover(fast, slow)
crossDn = ta.crossunder(fast, slow)

plotshape(crossUp, "Buy",  shape.triangleup,   location.belowbar, color.green)
plotshape(crossDn, "Sell", shape.triangledown, location.abovebar, color.red)

alertcondition(crossUp, "EMA cross up", "Fast EMA crossed above slow EMA")

Indicators vs strategies

Swap indicator() for strategy() and you can simulate trades with strategy.entry() and strategy.close(), then backtest in TradingView's Strategy Tester. An indicator only draws; a strategy also models entries, exits and equity. TradingView does not place live orders itself, but a strategy's alerts can drive a broker or bot through webhooks.

Common mistakes to avoid

Want it built properly?

Robust, non-repainting Pine Script indicators, strategies and screeners take real care. If you would rather hand over an idea and get back clean, tested code, that is our TradingView Pine Script development service. Need it on MetaTrader instead? See strategy to Expert Advisor conversion.

Frequently asked

What is Pine Script?

Pine Script is TradingView's own programming language for building custom indicators, strategies and screeners directly on TradingView charts. Scripts are written in the built-in Pine Editor, and the current version is Pine Script v6.

Is Pine Script hard to learn?

Pine Script is one of the easier trading languages to start with. It is concise, purpose-built for charts, and you can plot a working indicator in a few lines. Complex, non-repainting strategies still take real care, but the basics are approachable.

What is the difference between an indicator and a strategy in Pine Script?

An indicator, declared with indicator(), only calculates and plots values on the chart. A strategy, declared with strategy(), can also simulate trades so you can backtest entries and exits in TradingView's Strategy Tester.

Can Pine Script place live trades?

TradingView does not execute orders itself. A Pine Script strategy backtests in the Strategy Tester, and its alerts can be sent as webhooks to a broker or trading bot for automated execution.

Which Pine Script version should I use?

Use the latest version, currently Pine Script v6, declared with the //@version=6 annotation at the top of the script. Newer versions are faster and have cleaner, namespaced functions such as ta.ema and ta.crossover.

Beyond the basics? We'll build it.

Bring the idea. We build a clean Pine Script indicator or strategy, source code included.

Pine Script development Book a call