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.
ZENKEI
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.
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.
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.
You need very little to begin:
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:
//@version=6 tells TradingView which Pine version to use.indicator() for a chart study, or strategy() for something backtestable.input.int() and friends expose settings users can change.ta.* functions compute values, and plot() draws them.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.
Start with //@version=6, then indicator("My Indicator", overlay=true). The overlay=true draws on the price chart rather than in a separate pane.
Expose the settings you want adjustable, such as moving-average lengths, with input.int(). Inputs appear in the indicator's settings dialog.
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.
Draw lines with plot() and markers with plotshape(). Colour and style are arguments, so a signal can be a green triangle under the bar.
Use alertcondition() so users can set a TradingView alert on your signal, which can then be sent onward as a webhook.
Save the script, then add it to the chart to see it run. Adjust inputs from the settings dialog and iterate.
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.
//@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")
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.
plot() must be called on every bar, not inside an if.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.
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.
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.
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.
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.
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.
Bring the idea. We build a clean Pine Script indicator or strategy, source code included.